Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#327] Implement CommentCard #360

Merged
merged 2 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions ui/src/components/comment/client/comment-card-body.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use client';

import clsx from 'clsx';

import { CommentEditable } from '@/components/comment/client/comment-editable';

import { useCommentCardActions } from '@/hooks/comment/use-comment-card-actions';

export function CommentCardBody() {
return <CommentEditable actions={<Actions />} />;
}

function Actions() {
const { handleClickCancel, handleClickUpdate, disabled } = useCommentCardActions();

return (
<>
<button
className={clsx('h-fit w-fit rounded-full border bg-white px-2 py-1', {
'pointer-events-none': disabled,
})}
aria-disabled={disabled}
onClick={handleClickCancel}
>
취소
</button>

<button
className={clsx('h-fit w-fit rounded-full border bg-white px-2 py-1', {
'pointer-events-none': disabled,
})}
aria-disabled={disabled}
onClick={handleClickUpdate}
>
저장
</button>
</>
);
}
36 changes: 36 additions & 0 deletions ui/src/components/comment/client/comment-card.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use client';

import { CommentCardBody } from '@/components/comment/client/comment-card-body';
import { CommentCardFooter } from '@/components/comment/client/comment-card-footer';
import { CommentCardHeader } from '@/components/comment/client/comment-card-header';
import Card from '@/components/common/server/card';

import { CommentProvider } from '@/context/comment/comment-context';

import type { Comment } from '@/lib/definitions/comment';

export function CommentCard({ comment }: { comment: Comment }) {
return (
<CommentProvider prepopulated={comment}>
<Card>
<Card.Header>
<CommentCardHeader />
</Card.Header>

<Card.Body>
<CommentCardBody />
</Card.Body>

<Card.Footer>
<CommentCardFooter
nickname={comment.nickname}
tag={comment.tag}
createdAt={comment.createdAt}
updatedAt={comment.updatedAt}
authorId={comment.userId}
/>
</Card.Footer>
</Card>
</CommentProvider>
);
}
66 changes: 66 additions & 0 deletions ui/src/hooks/comment/use-comment-card-actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
'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 { updateComment } from '@/lib/apis/comment/client';

export const useCommentCardActions = () => {
const {
disabled,
setDisabled,
setContent,
setMovieName,
prepopulated,
setIsEditing,
validateAndGetData,
} = useComment();

if (prepopulated === undefined) {
throw new Error('content should be prepopulated');
}

const handleClickCancel = () => {
setDisabled(true);

setContent(prepopulated.content);
setMovieName(prepopulated.movieName);

setIsEditing(false);
setDisabled(false);
};

const router = useRouter();
const { emitToast } = useToast();
const { handleApiError } = useApiError();

const handleUpdateComment = async () => {
const data = validateAndGetData();

if (data === undefined) {
return;
}

try {
await updateComment(prepopulated.id, data);

setIsEditing(false);
emitToast('코멘트가 수정되었습니다.', 'success');
router.refresh();
} catch (error) {
handleApiError(error);
}
};

const handleClickUpdate = async () => {
setDisabled(true);
await handleUpdateComment();
setDisabled(false);
};

return { disabled, handleClickCancel, handleClickUpdate: () => void handleClickUpdate() };
};