diff --git a/convex/posts.ts b/convex/posts.ts index 2b04cb7..c96b296 100644 --- a/convex/posts.ts +++ b/convex/posts.ts @@ -160,3 +160,46 @@ export const getUserPostCount = query({ return posts.length; }, }); + +export const getAllPostsWithUser = query({ + args: { paginationOpts: paginationOptsValidator }, + handler: async (ctx, args) => { + const posts = await ctx.db + .query('posts') + .filter(q => q.eq(q.field('parentPostId'), undefined)) + .order('desc') + .paginate(args.paginationOpts); + + const postsWithUser = await Promise.all( + posts.page.map(async post => { + const user = await ctx.db.get(post.userId); + + if (user === null) { + return { + ...post, + user: { + _id: '' as Id<'users'>, + _creationTime: 0, + clerkUsername: null, + clerkImageUrl: '', + clerkUserId: '', + }, + }; + } + + return { + ...post, + user: { + _id: user._id, + _creationTime: user._creationTime, + clerkUsername: user.clerkUser.username, + clerkImageUrl: user.clerkUser.image_url, + clerkUserId: user.clerkUser.id, + }, + }; + }) + ); + + return { ...posts, page: postsWithUser }; + }, +}); diff --git a/src/app/page.tsx b/src/app/page.tsx index 1cdf8fa..0f8b653 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -1,3 +1,12 @@ +import AllPosts from '@/components/AllPosts'; + export default function Home() { - return
; + return ( +
+
+

Home

+ +
+
+ ); } diff --git a/src/components/AllPosts.tsx b/src/components/AllPosts.tsx new file mode 100644 index 0000000..87ef44a --- /dev/null +++ b/src/components/AllPosts.tsx @@ -0,0 +1,39 @@ +'use client'; + +import { usePaginatedQuery } from 'convex/react'; +import { api } from '../../convex/_generated/api'; +import Loader from './Loader'; +import Post from './Post'; + +export default function AllPosts() { + const PAGE_SIZE = 10; + const pager = usePaginatedQuery( + api.posts.getAllPostsWithUser, + {}, + { initialNumItems: PAGE_SIZE } + ); + + function handleLoadMoreClick() { + pager.loadMore(PAGE_SIZE); + } + + return ( +
+
+ {pager.results.map(post => ( +
+ +
+ ))} +
+ +
+ ); +} diff --git a/src/components/Navbar.tsx b/src/components/Navbar.tsx index 5d15516..8c49641 100644 --- a/src/components/Navbar.tsx +++ b/src/components/Navbar.tsx @@ -3,6 +3,7 @@ import { SignedIn, SignedOut, useUser } from '@clerk/nextjs'; import Link from 'next/link'; import { useEffect, useState } from 'react'; +import { AiFillHome } from 'react-icons/ai'; import { BiLogIn, BiPlus } from 'react-icons/bi'; import { BsPersonPlusFill } from 'react-icons/bs'; import { ImProfile } from 'react-icons/im'; @@ -64,6 +65,19 @@ export default function Navbar() { /> +
  • +
    +
    + +
    + setNavOpen(false)} + href='/' + > + Home + +
    +
  • diff --git a/src/components/UserButton.tsx b/src/components/UserButton.tsx index 9323c32..b6dd0e7 100644 --- a/src/components/UserButton.tsx +++ b/src/components/UserButton.tsx @@ -1,8 +1,9 @@ 'use client'; import { useMountedEffect } from '@/hooks'; import { UserButton as ClerkUserButton } from '@clerk/nextjs'; +import { ReactNode } from 'react'; -export default function UserButton() { +export default function UserButton({ children }: { children?: ReactNode }) { const { mounted } = useMountedEffect(); if (mounted === false) { diff --git a/src/components/UserPosts.tsx b/src/components/UserPosts.tsx index 2f6c32f..ef35256 100644 --- a/src/components/UserPosts.tsx +++ b/src/components/UserPosts.tsx @@ -8,7 +8,7 @@ import Loader from './Loader'; import Post from './Post'; export default function UserPosts({ user }: { user: UserDto }) { - const PAGE_SIZE = 1; + const PAGE_SIZE = 10; const pager = usePaginatedQuery( api.posts.getUsersPostById, {