Skip to content

Commit

Permalink
Refactor form submission logic and add tag
Browse files Browse the repository at this point in the history
functionality
  • Loading branch information
thebkht committed Nov 27, 2023
1 parent f72fbc9 commit 00ed4d5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
20 changes: 15 additions & 5 deletions components/editor/post-editor-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -211,19 +215,22 @@ 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
}
}
}

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
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -557,7 +567,7 @@ export function PostEditorForm(props: { post: any, user: any }) {
}
}}
>
Add Topic
Add Tag
</Button>
)}

Expand Down
2 changes: 1 addition & 1 deletion lib/insert-tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 00ed4d5

Please sign in to comment.