From 78c92df58cda5618a93b53dbc1903c0b09641ed2 Mon Sep 17 00:00:00 2001 From: draculola1 Date: Mon, 24 Jul 2023 20:59:51 +0300 Subject: [PATCH 1/2] fix(CommentForm): implemented receiving a response based on which change the local state busy --- src/components/CommentCreateForm/CommentCreateForm.tsx | 6 ++++-- src/components/CommentForm/CommentForm.tsx | 10 +++++++--- src/hooks/useCommentResource.ts | 1 + 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/components/CommentCreateForm/CommentCreateForm.tsx b/src/components/CommentCreateForm/CommentCreateForm.tsx index 2c6854c3c..0a04d3d88 100644 --- a/src/components/CommentCreateForm/CommentCreateForm.tsx +++ b/src/components/CommentCreateForm/CommentCreateForm.tsx @@ -33,12 +33,14 @@ const CommentCreateForm: React.FC = ({ goalId, states, o const [pushState, setPushState] = useState(); const createComment = useCallback( - (form: GoalCommentSchema) => { + async (form: GoalCommentSchema) => { // FIXME: maybe async/await would be better API - create(({ id }) => { + const response = await create(({ id }) => { onSubmit?.(id); setPushState(undefined); })(form); + + return Boolean(response?.id); }, [create, onSubmit], ); diff --git a/src/components/CommentForm/CommentForm.tsx b/src/components/CommentForm/CommentForm.tsx index 64f9fa6dc..3285cadbe 100644 --- a/src/components/CommentForm/CommentForm.tsx +++ b/src/components/CommentForm/CommentForm.tsx @@ -21,7 +21,7 @@ interface CommentFormProps { description?: string; renderActionButton: (props: { busy?: boolean }) => React.ReactNode; - onSubmit?: (form: GoalCommentSchema) => void; + onSubmit?: (form: GoalCommentSchema) => void | Promise; onFocus?: () => void; onCancel?: () => void; } @@ -98,11 +98,15 @@ export const CommentForm: React.FC = ({ }, [onCancel, reset]); const onCommentSubmit = useCallback( - (form: GoalCommentSchema) => { + async (form: GoalCommentSchema) => { setBusy(true); setCommentFocused(false); - onSubmit?.(form); reset(); + + const isSuccess = await onSubmit?.(form); + if (!isSuccess) return; + + setBusy(false); }, [onSubmit, reset], ); diff --git a/src/hooks/useCommentResource.ts b/src/hooks/useCommentResource.ts index 7923182a6..6c8b80124 100644 --- a/src/hooks/useCommentResource.ts +++ b/src/hooks/useCommentResource.ts @@ -22,6 +22,7 @@ export const useCommentResource = () => { const [data] = await notifyPromise(promise, 'commentCreate'); data && cb(data); + return data; }; const update = From 5c233e228300446a5d1881b91d9f2420eb1d577e Mon Sep 17 00:00:00 2001 From: draculola1 Date: Mon, 24 Jul 2023 23:10:08 +0300 Subject: [PATCH 2/2] fix(CommentForm): removed unnecessary --- src/components/CommentCreateForm/CommentCreateForm.tsx | 4 +--- src/components/CommentForm/CommentForm.tsx | 5 ++--- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/components/CommentCreateForm/CommentCreateForm.tsx b/src/components/CommentCreateForm/CommentCreateForm.tsx index 0a04d3d88..1d6219c7d 100644 --- a/src/components/CommentCreateForm/CommentCreateForm.tsx +++ b/src/components/CommentCreateForm/CommentCreateForm.tsx @@ -35,12 +35,10 @@ const CommentCreateForm: React.FC = ({ goalId, states, o const createComment = useCallback( async (form: GoalCommentSchema) => { // FIXME: maybe async/await would be better API - const response = await create(({ id }) => { + await create(({ id }) => { onSubmit?.(id); setPushState(undefined); })(form); - - return Boolean(response?.id); }, [create, onSubmit], ); diff --git a/src/components/CommentForm/CommentForm.tsx b/src/components/CommentForm/CommentForm.tsx index 3285cadbe..4d90141b7 100644 --- a/src/components/CommentForm/CommentForm.tsx +++ b/src/components/CommentForm/CommentForm.tsx @@ -21,7 +21,7 @@ interface CommentFormProps { description?: string; renderActionButton: (props: { busy?: boolean }) => React.ReactNode; - onSubmit?: (form: GoalCommentSchema) => void | Promise; + onSubmit?: (form: GoalCommentSchema) => void | Promise; onFocus?: () => void; onCancel?: () => void; } @@ -103,8 +103,7 @@ export const CommentForm: React.FC = ({ setCommentFocused(false); reset(); - const isSuccess = await onSubmit?.(form); - if (!isSuccess) return; + await onSubmit?.(form); setBusy(false); },