diff --git a/components/editor/post-editor-form.tsx b/components/editor/post-editor-form.tsx index fd6bb4d..950e4c2 100644 --- a/components/editor/post-editor-form.tsx +++ b/components/editor/post-editor-form.tsx @@ -152,7 +152,11 @@ export function PostEditorForm(props: { post: any, user: any }) { } setIsPublishing(true); - uploadCover(); + form.setValue('published', true); + const coverUrl = await uploadCover(); + if (coverUrl) { + form.setValue('coverImage', coverUrl); + } const result = await fetch(`/api/post/${props.post?.id}`, { method: "PATCH", @@ -211,11 +215,11 @@ export function PostEditorForm(props: { post: any, user: any }) { }); // get the image url const { data: coverUrl } = await res.json() - form.setValue('coverImage', coverUrl.url); - + return coverUrl.url; } catch (e: any) { // Handle errors here console.error(e); + return null } } } @@ -223,7 +227,10 @@ export function PostEditorForm(props: { post: any, user: any }) { const saveDraft = async () => { if (!isPublishing) { setIsSaving(true); - uploadCover(); + const coverUrl = await uploadCover(); + if (coverUrl) { + form.setValue('coverImage', coverUrl); + } if (form.getValues('title') && form.getValues('content')) { try { // Submit the form @@ -247,6 +254,9 @@ export function PostEditorForm(props: { post: any, user: any }) { console.error(error) } } + if (!form.getValues('published') && previousStatus == false) { + await onSubmit(form.getValues()); + } setIsSaving(false); } } @@ -557,7 +567,7 @@ export function PostEditorForm(props: { post: any, user: any }) { } }} > - Add Topic + Add Tag )} diff --git a/lib/insert-tag.ts b/lib/insert-tag.ts index b09311c..9b3c9b6 100644 --- a/lib/insert-tag.ts +++ b/lib/insert-tag.ts @@ -20,7 +20,7 @@ export async function insertTag(tags: any, postid: string) { ); for (const tag of uniqueTags) { const tagExists = await postgres.tag.findFirst({ - where: { name: tag }, + where: { name: { equals: tag } }, select: { id: true }, }); if (!tagExists) {