diff --git a/convex/posts.ts b/convex/posts.ts index 20ae560..ba9e15b 100644 --- a/convex/posts.ts +++ b/convex/posts.ts @@ -3,8 +3,9 @@ import { v } from 'convex/values'; import { mutation, query } from './_generated/server'; import { userQuery } from './users'; -export const createPost = mutation({ +export const createOrUpdatePost = mutation({ args: { + id: v.optional(v.id('posts')), clerkUserId: v.string(), content: v.array(v.string()), parentPostId: v.optional(v.id('posts')), @@ -25,6 +26,11 @@ export const createPost = mutation({ return 'CANNOT_POST_ON_BEHALF_OF_ANOTHER_USER'; } + if (args.id) { + await ctx.db.patch(args.id, { content: args.content }); + return; + } + return await ctx.db.insert('posts', { userId: user._id, content: args.content, @@ -43,3 +49,23 @@ export const getUsersPostById = query({ .paginate(args.paginationOpts); }, }); + +export const deletePostById = mutation({ + args: { id: v.id('posts') }, + handler: async (ctx, args) => { + await ctx.db.delete(args.id); + }, +}); + +export const getPostById = query({ + args: { id: v.id('posts') }, + handler: async (ctx, args) => { + const post = await ctx.db.get(args.id); + + if (post === null) { + return 'POST_NOT_FOUND'; + } + + return post; + }, +}); diff --git a/src/app/posts/[id]/edit/page.tsx b/src/app/posts/[id]/edit/page.tsx new file mode 100644 index 0000000..00563a8 --- /dev/null +++ b/src/app/posts/[id]/edit/page.tsx @@ -0,0 +1,26 @@ +import Editor from '@/components/Editor'; +import { getConvexClient } from '@/lib/convex'; +import { api } from '../../../../../convex/_generated/api'; +import { Id } from '../../../../../convex/_generated/dataModel'; + +export default async function EditPostPage({ + params, +}: { + params: { id: string }; +}) { + const client = await getConvexClient(); + const post = await client.query(api.posts.getPostById, { + id: params.id as Id<'posts'>, + }); + + if (post === 'POST_NOT_FOUND') { + // TODO: Return actual not found component + return

Not Found

; + } + + return ( +
+ +
+ ); +} diff --git a/src/components/Editor.tsx b/src/components/Editor.tsx index 8c6609f..e9b4eb6 100644 --- a/src/components/Editor.tsx +++ b/src/components/Editor.tsx @@ -10,22 +10,20 @@ import { Compartment, EditorState, Text } from '@codemirror/state'; import { EditorView, keymap } from '@codemirror/view'; import { basicSetup } from 'codemirror'; import { useMutation } from 'convex/react'; -import { useTheme } from 'next-themes'; import { useRouter } from 'next/navigation'; -import { FormEvent, useEffect, useState } from 'react'; +import { FormEvent, useState } from 'react'; import { api } from '../../convex/_generated/api'; +import { Doc } from '../../convex/_generated/dataModel'; import PostContent from './PostContent'; -export default function Editor() { +export default function Editor({ post }: { post?: Doc<'posts'> }) { const { user, isSignedIn } = useUser(); - const { theme } = useTheme(); const editorTheme = new Compartment(); - const [currentDoc, setCurrentDoc] = useState(['']); - const createPost = useMutation(api.posts.createPost); + const [currentDoc, setCurrentDoc] = useState(post?.content ?? ['']); + const createOrUpdatePost = useMutation(api.posts.createOrUpdatePost); const router = useRouter(); - const [creating, setCreating] = useState(false); + const [creatingOrUpdating, setCreatingOrUpdating] = useState(false); - const doc = ['']; const extensions = [ basicSetup, markdown({ base: markdownLanguage, codeLanguages: languages }), @@ -38,20 +36,7 @@ export default function Editor() { ]; const [mode, setMode] = useState<'write' | 'preview'>('write'); - const { editorRef, editorView } = useCodeMirror({ doc, extensions }); - - useEffect(() => { - if (editorView === null) { - return; - } - - if (theme === 'dark') { - editorView.dispatch({ - effects: editorTheme.reconfigure([]), - }); - } - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [theme, editorView]); + const { editorRef } = useCodeMirror({ doc: currentDoc, extensions }); async function handleSubmit(e: FormEvent) { e.preventDefault(); @@ -61,9 +46,10 @@ export default function Editor() { return; } - setCreating(true); + setCreatingOrUpdating(true); - const result = await createPost({ + const result = await createOrUpdatePost({ + id: post?._id, clerkUserId: user.id, content: currentDoc, }); @@ -80,7 +66,7 @@ export default function Editor() { } catch (error) { console.log(error); } finally { - setCreating(false); + setCreatingOrUpdating(false); } } @@ -123,10 +109,20 @@ export default function Editor() { -
+
+