Files
conve-x/src/app/posts/[id]/edit/page.tsx
T

70 lines
1.9 KiB
TypeScript
Raw Normal View History

'use client';
import Editor, { SubmitActionParams } from '@/components/Editor';
import SpinningLoader from '@/components/SpinningLoader';
import { useRouter } from '@/hooks';
import { useUser } from '@clerk/nextjs';
import { useMutation, useQuery } from 'convex/react';
import { api } from '../../../../../convex/_generated/api';
import { Id } from '../../../../../convex/_generated/dataModel';
export default function EditPostPage({ params }: { params: { id: string } }) {
const user = useUser();
const post = useQuery(api.posts.getPostById, {
id: params.id as Id<'posts'>,
});
const createOrUpdatePost = useMutation(api.posts.createOrUpdatePost);
const router = useRouter();
if (user.isSignedIn === false) {
router.push('/login');
return;
}
2023-09-11 22:05:12 -05:00
if (post === 'POST_NOT_FOUND' || post === 'USER_FOR_POST_NOT_FOUND') {
// TODO: Return actual not found component
return <h1>Not Found</h1>;
}
async function submitAction({
clerkUserId,
parentPostId,
post,
currentDoc,
}: SubmitActionParams) {
const result = await createOrUpdatePost({
parentPostId: parentPostId,
id: post?._id,
clerkUserId: clerkUserId,
content: currentDoc,
});
switch (result) {
case 'USER_NOT_FOUND':
case 'USER_NOT_AUTHORIZED':
case 'CANNOT_POST_ON_BEHALF_OF_ANOTHER_USER':
throw Error('Unable to update post');
default:
2023-09-14 21:47:40 -05:00
router.push(`/posts/${post?._id}`);
return;
}
}
return (
<main className='flex flex-col flex-1 w-full items-center'>
{user.isLoaded && post !== undefined ? (
<Editor
clerkUserId={user.user.id}
post={post}
submitAction={submitAction}
/>
) : (
<div className='flex gap-2 items-center'>
<SpinningLoader className='animate-spin w-5 h-5' />
Loading...
</div>
)}
</main>
);
}