-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #354 from MovieReviewComment/feature/issue-327/cre…
…ate-comment-button [#327] Implement CreateCommentButton
- Loading branch information
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
ui/src/components/comment/client/create-comment-button.tsx
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,23 @@ | ||
'use client'; | ||
|
||
import clsx from 'clsx'; | ||
|
||
import Text from '@/components/common/server/text'; | ||
|
||
import { useCreateCommentButton } from '@/hooks/comment/use-create-comment-button'; | ||
|
||
export function CreateCommentButton() { | ||
const { disabled, handleClick } = useCreateCommentButton(); | ||
|
||
return ( | ||
<button | ||
onClick={handleClick} | ||
className={clsx('h-fit w-fit rounded-full border bg-white px-2 py-1', { | ||
'pointer-events-none': disabled, | ||
})} | ||
aria-disabled={disabled} | ||
> | ||
<Text nowrap>등록하기</Text> | ||
</button> | ||
); | ||
} |
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,47 @@ | ||
'use client'; | ||
|
||
import { useRouter } from 'next/navigation'; | ||
|
||
import { useComment } from '@/context/comment/comment-context'; | ||
import { useToast } from '@/context/common/toast-context'; | ||
|
||
import { useApiError } from '@/hooks/common/use-api-error'; | ||
|
||
import { createComment } from '@/lib/apis/comment/client'; | ||
|
||
export function useCreateCommentButton() { | ||
const router = useRouter(); | ||
const { emitToast } = useToast(); | ||
const { handleApiError } = useApiError(); | ||
|
||
const { disabled, setDisabled, setContent, setMovieName, validateAndGetData } = useComment(); | ||
|
||
const handleCreateComment = async () => { | ||
const data = validateAndGetData(); | ||
|
||
if (!data) { | ||
return; | ||
} | ||
|
||
try { | ||
await createComment(data); | ||
|
||
emitToast('코멘트가 등록되었습니다.', 'success'); | ||
|
||
setContent(''); | ||
setMovieName(''); | ||
router.push('/comment'); // to reset query | ||
router.refresh(); | ||
} catch (error) { | ||
handleApiError(error); | ||
} | ||
}; | ||
|
||
const handleClick = async () => { | ||
setDisabled(true); | ||
await handleCreateComment(); | ||
setDisabled(false); | ||
}; | ||
|
||
return { disabled, handleClick: () => void handleClick() }; | ||
} |