@@ -24,13 +24,13 @@ interface EditorProps {
24
24
type FormData = z . infer < typeof postPatchSchema >
25
25
26
26
export function Editor ( { post } : EditorProps ) {
27
- const { register, handleSubmit } = useForm < FormData > ( {
27
+ const { register, handleSubmit, formState : { errors , isSubmitting , isDirty } } = useForm < FormData > ( {
28
28
resolver : zodResolver ( postPatchSchema ) ,
29
29
} )
30
30
const ref = React . useRef < EditorJS > ( )
31
31
const router = useRouter ( )
32
- const [ isSaving , setIsSaving ] = React . useState < boolean > ( false )
33
32
const [ isMounted , setIsMounted ] = React . useState < boolean > ( false )
33
+ const [ editorContentChanged , setEditorContentChanged ] = React . useState < boolean > ( false ) ;
34
34
35
35
const initializeEditor = React . useCallback ( async ( ) => {
36
36
const EditorJS = ( await import ( "@editorjs/editorjs" ) ) . default
@@ -50,6 +50,9 @@ export function Editor({ post }: EditorProps) {
50
50
onReady ( ) {
51
51
ref . current = editor
52
52
} ,
53
+ onChange : ( ) => {
54
+ setEditorContentChanged ( true ) ;
55
+ } ,
53
56
placeholder : "Type here to write your post..." ,
54
57
inlineToolbar : true ,
55
58
data : body . content ,
@@ -84,7 +87,6 @@ export function Editor({ post }: EditorProps) {
84
87
} , [ isMounted , initializeEditor ] )
85
88
86
89
async function onSubmit ( data : FormData ) {
87
- setIsSaving ( true )
88
90
89
91
const blocks = await ref . current ?. save ( )
90
92
@@ -99,8 +101,6 @@ export function Editor({ post }: EditorProps) {
99
101
} ) ,
100
102
} )
101
103
102
- setIsSaving ( false )
103
-
104
104
if ( ! response ?. ok ) {
105
105
return toast ( {
106
106
title : "Something went wrong." ,
@@ -138,8 +138,8 @@ export function Editor({ post }: EditorProps) {
138
138
{ post . published ? "Published" : "Draft" }
139
139
</ p >
140
140
</ div >
141
- < button type = "submit" className = { cn ( buttonVariants ( ) ) } >
142
- { isSaving && (
141
+ < button disabled = { isSubmitting || ! ( isDirty || editorContentChanged ) } type = "submit" className = { cn ( buttonVariants ( ) ) } >
142
+ { isSubmitting && (
143
143
< Icons . spinner className = "mr-2 h-4 w-4 animate-spin" />
144
144
) }
145
145
< span > Save</ span >
@@ -154,6 +154,9 @@ export function Editor({ post }: EditorProps) {
154
154
className = "w-full resize-none appearance-none overflow-hidden bg-transparent text-5xl font-bold focus:outline-none"
155
155
{ ...register ( "title" ) }
156
156
/>
157
+ { errors . title && (
158
+ < p className = "text-red-500" > { `${ errors . title . message } ` } </ p >
159
+ ) }
157
160
< div id = "editor" className = "min-h-[500px]" />
158
161
< p className = "text-sm text-gray-500" >
159
162
Use{ " " }
0 commit comments