Skip to content

Commit 6ebf214

Browse files
authored
fix: new zettel form issues (#93)
1 parent 64ef01b commit 6ebf214

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

apps/tereza/src/modules/Zettel/ZettelNoteForm.tsx

+13-8
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,10 @@ export const ZettelNoteForm = ({
4545
}: {
4646
note?: ZettelNoteFormValues & { id?: string };
4747
}) => {
48-
const { id, ...defaultValues } = note;
48+
const { id: noteId, ...defaultValues } = note;
4949

5050
const router = useRouter();
5151

52-
const [noteId, setNoteId] = React.useState<string | null>(id ?? null);
53-
5452
const { setNotifications } = useNotifications();
5553

5654
const formMethods = useForm<ZettelNoteFormValues>({
@@ -61,6 +59,7 @@ export const ZettelNoteForm = ({
6159
const {
6260
formState: { isSubmitting, isSubmitted, isSubmitSuccessful, isDirty },
6361
reset,
62+
getValues,
6463
} = formMethods;
6564

6665
const [saveStatus, setSaveStatus] = React.useState('');
@@ -202,14 +201,11 @@ export const ZettelNoteForm = ({
202201
});
203202

204203
if (note) {
205-
if (!noteId) {
206-
setNoteId(note.id);
207-
}
208-
209204
/**
210205
* Reset because API may have changed the values.
211206
*/
212207
reset({
208+
...getValues(),
213209
title: note.title ?? '',
214210
content: note.content ?? '',
215211
description: note.description ?? '',
@@ -220,6 +216,15 @@ export const ZettelNoteForm = ({
220216
return reference.reference;
221217
}),
222218
});
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+
}
223228
}
224229
};
225230

@@ -297,7 +302,7 @@ export const ZettelNoteForm = ({
297302
<Button
298303
type="button"
299304
onClick={() => {
300-
const href = id ? `../${id}` : '../';
305+
const href = noteId ? `/my/zettel/${noteId}` : '/my/zettel';
301306
router.push(href);
302307
}}
303308
>

0 commit comments

Comments
 (0)