From e29b46cdb135af2475e4278c1989e9cb3923af92 Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Mon, 11 Sep 2023 13:44:51 -0500 Subject: [PATCH] fix: when getting user don't return entire user to client, fix: pass user to post component as part of post --- convex/users.ts | 16 +++++----- src/app/profile/[id]/page.tsx | 2 +- src/components/Post.tsx | 36 ++++++++++------------ src/components/UserPosts.tsx | 55 ++++++++++++++++++++++++---------- src/components/UserProfile.tsx | 17 ++++++++--- 5 files changed, 76 insertions(+), 50 deletions(-) diff --git a/convex/users.ts b/convex/users.ts index 0a1f8f4..2166aa6 100644 --- a/convex/users.ts +++ b/convex/users.ts @@ -16,20 +16,18 @@ export async function userQuery(ctx: QueryCtx, clerkUserId: string) { export const getUserByClerkId = query({ args: { clerkUserId: v.string() }, async handler(ctx, args) { - return await userQuery(ctx, args.clerkUserId); - }, -}); - -export const getUserById = query({ - args: { id: v.id('users') }, - async handler(ctx, args) { - const user = await ctx.db.get(args.id); + const user = await userQuery(ctx, args.clerkUserId); if (user === null) { return 'USER_NOT_FOUND'; } - return user; + return { + _id: user._id, + _creationTime: user._creationTime, + clerkUsername: user.clerkUser.username, + clerkImageUrl: user.clerkUser.image_url, + }; }, }); diff --git a/src/app/profile/[id]/page.tsx b/src/app/profile/[id]/page.tsx index e85aabb..652eb76 100644 --- a/src/app/profile/[id]/page.tsx +++ b/src/app/profile/[id]/page.tsx @@ -13,7 +13,7 @@ export default async function UserProfilePage({ clerkUserId: params.id, }); - if (user === null) { + if (user === 'USER_NOT_FOUND') { // TODO: Show real not found component return
Not Found
; } diff --git a/src/components/Post.tsx b/src/components/Post.tsx index e0e7903..6073f4b 100644 --- a/src/components/Post.tsx +++ b/src/components/Post.tsx @@ -1,27 +1,21 @@ import { Text } from '@codemirror/state'; -import { useQuery } from 'convex/react'; import Image from 'next/image'; -import { api } from '../../convex/_generated/api'; -import { Doc } from '../../convex/_generated/dataModel'; +import { Doc, Id } from '../../convex/_generated/dataModel'; import PostContent from './PostContent'; -import SpinningLoader from './SpinningLoader'; -export default function Post({ post }: { post: Doc<'posts'> }) { - const user = useQuery(api.users.getUserById, { id: post.userId }); - - if (user === undefined) { - return ( -
- -
- ); - } - - if (user === 'USER_NOT_FOUND') { - return
User for post not found
; - } - - const username = user.clerkUser.username ?? user._id; +export default function Post({ + post, +}: { + post: Doc<'posts'> & { + user: { + _id: Id<'users'>; + _creationTime: number; + clerkUsername: string | null; + clerkImageUrl: string; + }; + }; +}) { + const username = post.user.clerkUsername ?? post.user._id; const createdPostDate = new Date(post._creationTime); const postYear = createdPostDate.getFullYear(); const postMonth = createdPostDate.toLocaleString(undefined, { @@ -34,7 +28,7 @@ export default function Post({ post }: { post: Doc<'posts'> }) {
user profile image }) { - const PAGE_SIZE = 10; +export default function UserPosts({ + user, +}: { + user: { + _id: Id<'users'>; + _creationTime: number; + clerkUsername: string | null; + clerkImageUrl: string; + }; +}) { + const PAGE_SIZE = 1; const pager = usePaginatedQuery( api.posts.getUsersPostById, { userId: user._id, }, - { initialNumItems: 10 } + { initialNumItems: PAGE_SIZE } ); + const postsWithUser = pager.results.map(post => ({ ...post, user })); + return ( -
- {pager.results.map(post => ( +
+ {postsWithUser.map(post => ( ))} {pager.isLoading ? ( -
Loading...
- ) : ( - - )} +
+ + Loading... +
+ ) : pager.status === 'CanLoadMore' ? ( +
+ +
+ ) : null}
); } diff --git a/src/components/UserProfile.tsx b/src/components/UserProfile.tsx index 44c2330..5994a76 100644 --- a/src/components/UserProfile.tsx +++ b/src/components/UserProfile.tsx @@ -1,9 +1,18 @@ import Image from 'next/image'; import { AiFillCalendar } from 'react-icons/ai'; -import { Doc } from '../../convex/_generated/dataModel'; +import { Doc, Id } from '../../convex/_generated/dataModel'; -export default function UserProfile({ user }: { user: Doc<'users'> }) { - const username = user.clerkUser.username ?? user._id; +export default function UserProfile({ + user, +}: { + user: { + _id: Id<'users'>; + _creationTime: number; + clerkUsername: string | null; + clerkImageUrl: string; + }; +}) { + const username = user.clerkUsername ?? user._id; const userCreatedDate = new Date(user._creationTime); const monthJoined = userCreatedDate.toLocaleString(undefined, { month: 'short', @@ -17,7 +26,7 @@ export default function UserProfile({ user }: { user: Doc<'users'> }) {