From 9f2bc2bedd3224b1a183af981886268aa1bba1e4 Mon Sep 17 00:00:00 2001 From: Tony Vi Date: Wed, 2 Aug 2023 21:53:41 +0300 Subject: [PATCH] fix(CommentForm): remove render props, pass values from parent --- .../CommentCreateForm/CommentCreateForm.tsx | 38 +++++++++- .../CommentEditForm/CommentEditForm.tsx | 43 +++++++++-- src/components/CommentForm/CommentForm.tsx | 76 +++++++------------ 3 files changed, 101 insertions(+), 56 deletions(-) diff --git a/src/components/CommentCreateForm/CommentCreateForm.tsx b/src/components/CommentCreateForm/CommentCreateForm.tsx index 0dbbf1fd2..f88a16058 100644 --- a/src/components/CommentCreateForm/CommentCreateForm.tsx +++ b/src/components/CommentCreateForm/CommentCreateForm.tsx @@ -31,19 +31,45 @@ const CommentCreateForm: React.FC = ({ goalId, states, o const { user, themeId } = usePageContext(); const { create } = useCommentResource(); const [pushState, setPushState] = useState(); + const [description, setDescription] = useState(); + const [focused, setFocused] = useState(false); + const [busy, setBusy] = useState(false); + const [currentGoal, setCurrentGoal] = useState(''); + const [prevGoal, setPrevGoal] = useState(''); + + if (goalId !== prevGoal) { + setCurrentGoal(goalId); + setPrevGoal(goalId); + setDescription(''); + setFocused(false); + } + + const onCommentFocus = useCallback(() => { + setFocused(true); + onFocus?.(); + }, [onFocus]); const createComment = useCallback( async (form: GoalCommentSchema) => { + setBusy(true); + setFocused(false); + await create(({ id }) => { - onSubmit?.(id); + setBusy(false); + setFocused(true); + setDescription(''); setPushState(undefined); + onSubmit?.(id); })(form); }, [create, onSubmit], ); const onCancelCreate = useCallback(() => { + setBusy(false); + setFocused(false); setPushState(undefined); + setDescription(''); onCancel?.(); }, [onCancel]); @@ -59,12 +85,16 @@ const CommentCreateForm: React.FC = ({ goalId, states, o + onFocus={onCommentFocus} + actionButton={ states ? (