@@ -45,12 +45,10 @@ export const ZettelNoteForm = ({
45
45
} : {
46
46
note ?: ZettelNoteFormValues & { id ?: string } ;
47
47
} ) => {
48
- const { id, ...defaultValues } = note ;
48
+ const { id : noteId , ...defaultValues } = note ;
49
49
50
50
const router = useRouter ( ) ;
51
51
52
- const [ noteId , setNoteId ] = React . useState < string | null > ( id ?? null ) ;
53
-
54
52
const { setNotifications } = useNotifications ( ) ;
55
53
56
54
const formMethods = useForm < ZettelNoteFormValues > ( {
@@ -61,6 +59,7 @@ export const ZettelNoteForm = ({
61
59
const {
62
60
formState : { isSubmitting, isSubmitted, isSubmitSuccessful, isDirty } ,
63
61
reset,
62
+ getValues,
64
63
} = formMethods ;
65
64
66
65
const [ saveStatus , setSaveStatus ] = React . useState ( '' ) ;
@@ -202,14 +201,11 @@ export const ZettelNoteForm = ({
202
201
} ) ;
203
202
204
203
if ( note ) {
205
- if ( ! noteId ) {
206
- setNoteId ( note . id ) ;
207
- }
208
-
209
204
/**
210
205
* Reset because API may have changed the values.
211
206
*/
212
207
reset ( {
208
+ ...getValues ( ) ,
213
209
title : note . title ?? '' ,
214
210
content : note . content ?? '' ,
215
211
description : note . description ?? '' ,
@@ -220,6 +216,15 @@ export const ZettelNoteForm = ({
220
216
return reference . reference ;
221
217
} ) ,
222
218
} ) ;
219
+
220
+ /**
221
+ * We've kept reset even if we're changing route because we want to
222
+ * get the form state updated. Else, it'd be as "dirty" as before, and
223
+ * thus, "unsaved changes" would be displayed.
224
+ */
225
+ if ( ! noteId ) {
226
+ router . push ( `/my/zettel/${ note . id } /editor` ) ;
227
+ }
223
228
}
224
229
} ;
225
230
@@ -297,7 +302,7 @@ export const ZettelNoteForm = ({
297
302
< Button
298
303
type = "button"
299
304
onClick = { ( ) => {
300
- const href = id ? `../ ${ id } ` : '../ ' ;
305
+ const href = noteId ? `/my/zettel/ ${ noteId } ` : '/my/zettel ' ;
301
306
router . push ( href ) ;
302
307
} }
303
308
>
0 commit comments