From 2fd6ddf0e020a3091d4b7a67984f0d0a1d4341cb Mon Sep 17 00:00:00 2001 From: Osama Malik Date: Mon, 29 Jan 2024 23:33:00 +0530 Subject: [PATCH] fix: disabled button on save and before any changes, handled errors --- components/editor.tsx | 17 ++++++++++------- lib/validations/post.ts | 2 +- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/components/editor.tsx b/components/editor.tsx index a683e750..2724b1df 100644 --- a/components/editor.tsx +++ b/components/editor.tsx @@ -24,13 +24,13 @@ interface EditorProps { type FormData = z.infer export function Editor({ post }: EditorProps) { - const { register, handleSubmit } = useForm({ + const { register, handleSubmit, formState: {errors, isSubmitting, isDirty} } = useForm({ resolver: zodResolver(postPatchSchema), }) const ref = React.useRef() const router = useRouter() - const [isSaving, setIsSaving] = React.useState(false) const [isMounted, setIsMounted] = React.useState(false) + const [editorContentChanged, setEditorContentChanged] = React.useState(false); const initializeEditor = React.useCallback(async () => { const EditorJS = (await import("@editorjs/editorjs")).default @@ -50,6 +50,9 @@ export function Editor({ post }: EditorProps) { onReady() { ref.current = editor }, + onChange: () => { + setEditorContentChanged(true); + }, placeholder: "Type here to write your post...", inlineToolbar: true, data: body.content, @@ -84,7 +87,6 @@ export function Editor({ post }: EditorProps) { }, [isMounted, initializeEditor]) async function onSubmit(data: FormData) { - setIsSaving(true) const blocks = await ref.current?.save() @@ -99,8 +101,6 @@ export function Editor({ post }: EditorProps) { }), }) - setIsSaving(false) - if (!response?.ok) { return toast({ title: "Something went wrong.", @@ -138,8 +138,8 @@ export function Editor({ post }: EditorProps) { {post.published ? "Published" : "Draft"}

-