feat: support replies

This commit is contained in:
Stevan Freeborn
2023-09-14 21:47:40 -05:00
parent 0dc2934e05
commit 820ba6b1e8
14 changed files with 344 additions and 97 deletions
+1 -2
View File
@@ -45,8 +45,7 @@ export default function EditPostPage({ params }: { params: { id: string } }) {
case 'CANNOT_POST_ON_BEHALF_OF_ANOTHER_USER':
throw Error('Unable to update post');
default:
'use server';
router.push('/');
router.push(`/posts/${post?._id}`);
return;
}
}
+19 -3
View File
@@ -2,11 +2,13 @@
import Editor, { SubmitActionParams } from '@/components/Editor';
import Post from '@/components/Post';
import PostReplies from '@/components/PostReplies';
import SpinningLoader from '@/components/SpinningLoader';
import { useRouter } from '@/hooks';
import { useUser } from '@clerk/nextjs';
import { useMutation, useQuery } from 'convex/react';
import Image from 'next/image';
import { useEffect, useRef } from 'react';
import { api } from '../../../../convex/_generated/api';
import { Id } from '../../../../convex/_generated/dataModel';
@@ -15,14 +17,22 @@ export default function PostPage({
searchParams,
}: {
params: { id: string };
searchParams: { reply?: boolean };
searchParams: { [key: string]: string | string[] | undefined };
}) {
const replyRef = useRef<HTMLDivElement>(null);
const user = useUser();
const router = useRouter();
const post = useQuery(api.posts.getPostById, {
id: params.id as Id<'posts'>,
});
const createOrUpdatePost = useMutation(api.posts.createOrUpdatePost);
const isReply = searchParams.reply === 'true';
useEffect(() => {
if (replyRef.current !== null && isReply) {
replyRef.current.scrollIntoView();
}
}, [isReply, post]);
async function submitAction({
clerkUserId,
@@ -69,6 +79,9 @@ export default function PostPage({
showReply={false}
/>
</div>
<div className='w-full'>
<PostReplies parentId={post._id} />
</div>
<div className='flex w-full h-80 gap-4'>
<div>
<Image
@@ -79,12 +92,15 @@ export default function PostPage({
className='rounded-full object-cover border-4 border-primary-accent'
/>
</div>
{/* TODO: Display replies as paged query. Should show latest replies with option to load more */}
<div className='flex w-full'>
<div
ref={replyRef}
className='flex w-full'
>
<Editor
clerkUserId={user.user.id}
parentPostId={post._id}
submitAction={submitAction}
autofocus={isReply}
/>
</div>
</div>