Skip to content

Commit

Permalink
WIP: Fix clear note form after successful edit
Browse files Browse the repository at this point in the history
  • Loading branch information
pkirilin committed Jul 3, 2024
1 parent baa05dd commit 92b7f20
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/frontend/src/entities/note/lib/useFormValues.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
import { useMemo, useState } from 'react';
import { useCallback, useMemo, useState } from 'react';
import { type FormValues } from '../model';

interface Result {
values: FormValues;
setValues: (values: FormValues) => void;
clearValues: () => void;
}

export const useFormValues = (initialValues: FormValues): Result => {
const [values, setValues] = useState<FormValues>(initialValues);

const clearValues = useCallback(() => {
setValues(initialValues);
}, [initialValues]);

return useMemo<Result>(
() => ({
values,
setValues,
clearValues,
}),
[values],
[clearValues, values],
);
};
6 changes: 4 additions & 2 deletions src/frontend/src/features/note/addEdit/ui/EditNote.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export const EditNote: FC<Props> = ({ note, pageId, renderTrigger }) => {
const addProductIfNotExists = useAddProductIfNotExists();

const notes = noteLib.useNotes(pageId);
const noteForm = noteLib.useFormValues({

const { clearValues: clearNoteForm, ...noteForm } = noteLib.useFormValues({
pageId,
mealType: note.mealType,
displayOrder: note.displayOrder,
Expand All @@ -39,7 +40,8 @@ export const EditNote: FC<Props> = ({ note, pageId, renderTrigger }) => {

const handleSubmitSuccess = useCallback(() => {
reset();
}, [reset]);
clearNoteForm();
}, [clearNoteForm, reset]);

return (
<AddOrEditNoteFlow
Expand Down

0 comments on commit 92b7f20

Please sign in to comment.