-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: on new comment input re-renders only comment form
- Loading branch information
1 parent
1a91635
commit b24bfae
Showing
7 changed files
with
90 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { ComponentProps, FC, useCallback } from 'react'; | ||
import type { Comment } from '@prisma/client'; | ||
|
||
import { useLSDraft } from '../hooks/useLSDraft'; | ||
|
||
import CommentCreateForm from './CommentCreateForm/CommentCreateForm'; | ||
|
||
interface GoalCommentCreateFormProps { | ||
states: ComponentProps<typeof CommentCreateForm>['states']; | ||
goalId: string; | ||
onSubmit: (comment?: { description: string; stateId?: string }) => Promise<Comment | null | undefined>; | ||
} | ||
|
||
const GoalCommentCreateForm: FC<GoalCommentCreateFormProps> = ({ states, goalId, onSubmit }) => { | ||
const { | ||
saveDraft: saveGoalCommentDraft, | ||
resolveDraft: resolveGoalCommentDraft, | ||
removeDraft: removeGoalCommentDraft, | ||
} = useLSDraft('draftGoalComment', {}); | ||
|
||
const onGoalCommentChange = useCallback( | ||
(comment?: { stateId?: string; description?: string }) => { | ||
if (!comment?.description) { | ||
removeGoalCommentDraft(goalId); | ||
return; | ||
} | ||
|
||
saveGoalCommentDraft(goalId, comment); | ||
}, | ||
[goalId, removeGoalCommentDraft, saveGoalCommentDraft], | ||
); | ||
|
||
const onGoalCommentCancel = useCallback(() => { | ||
removeGoalCommentDraft(goalId); | ||
}, [goalId, removeGoalCommentDraft]); | ||
|
||
const commentDraft = resolveGoalCommentDraft(goalId); | ||
|
||
const onGoalCommentSubmit = useCallback( | ||
async (comment?: { description: string; stateId?: string }) => { | ||
const data = await onSubmit(comment); | ||
if (data) { | ||
removeGoalCommentDraft(goalId); | ||
} | ||
}, | ||
[goalId, onSubmit, removeGoalCommentDraft], | ||
); | ||
|
||
return ( | ||
<CommentCreateForm | ||
states={states} | ||
stateId={commentDraft?.stateId} | ||
description={commentDraft?.description} | ||
onSubmit={onGoalCommentSubmit} | ||
onCancel={onGoalCommentCancel} | ||
onChange={onGoalCommentChange} | ||
/> | ||
); | ||
}; | ||
|
||
export default GoalCommentCreateForm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { useLayoutEffect, useRef } from 'react'; | ||
|
||
export function useLatest<T>(value: T) { | ||
const ref = useRef(value); | ||
|
||
useLayoutEffect(() => { | ||
ref.current = value; | ||
}, [value]); | ||
|
||
return ref; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters