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

코멘트 section에 input 박스 추가 #215

Merged
merged 2 commits into from
Aug 5, 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
22 changes: 17 additions & 5 deletions frontend/src/apis/posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,21 @@ export const postChangeZzim = async (moimId: number) => {
});
await checkStatus(response);
};
export const postWriteComment = async (moimId: number) => {
const response = await ApiClient.postWithAuth(`moim/${moimId}/comment`, {
moimId,
});
await checkStatus(response);
export const postWriteComment = async (
moimId: number,
selectedComment: number,
message: string,
) => {
if (selectedComment === 0) {
const response = await ApiClient.postWithAuth(`moim/${moimId}`, {
content: message,
});
await checkStatus(response);
} else {
const response = await ApiClient.postWithAuth(`moim/${moimId}`, {
parentId: selectedComment,
content: message,
});
await checkStatus(response);
}
};
4 changes: 2 additions & 2 deletions frontend/src/components/CommentCard/CommentCard.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ export const Default: Story = {
content: 'content',
dateTime: '2023-04-04 14:00',
profile: '',
child: [
children: [
{
commentId: 0,
nickname: 'nickname',
content: 'content',
dateTime: '2023-04-04 14:00',
profile: '',
child: [],
children: [],
},
],
},
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/components/CommentCard/CommentCard.style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ export const commentHeaderRight = (props: { theme: Theme }) => css`
border: none;
}
`;
export const contentBox = (props: { theme: Theme }) => css`
${props.theme.typography.c2}
`;

export const timestamp = (props: { theme: Theme }) => css`
${props.theme.typography.c3}
Expand Down
10 changes: 4 additions & 6 deletions frontend/src/components/CommentCard/CommentCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface CommentCardProps extends HTMLProps<HTMLDivElement> {
export default function CommentCard(props: CommentCardProps) {
const theme = useTheme();
const {
comment: { profile, nickname, dateTime, content, child },
comment: { profile, nickname, dateTime, content, children },
onWriteClick,
} = props;

Expand All @@ -29,17 +29,15 @@ export default function CommentCard(props: CommentCardProps) {
<div css={S.timestamp({ theme })}>{dateTime}</div>
</div>
<div css={S.commentHeaderRight({ theme })}>
<button>수정</button>
<button>삭제</button>
<button onClick={onWriteClick}>답글쓰기</button>
</div>
</div>
<div>{content}</div>
<div css={S.contentBox({ theme })}>{content}</div>
</div>
</div>
{child && child.length > 0 && (
{children && (
<div css={S.commentChildBox()}>
{child.map((childComment) => (
{children.map((childComment) => (
<CommentCard key={childComment.commentId} comment={childComment} />
))}
</div>
Expand Down
10 changes: 5 additions & 5 deletions frontend/src/components/CommentList/CommentList.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,22 @@ type Story = StoryObj<typeof meta>;

export const Default: Story = {
args: {
moimId: 1,
comments: [
{
commentId: 0,
nickname: 'nickname',
content: 'content',
dateTime: '2023-04-04 14:00',
profile: '',
child: [
children: [
{
commentId: 0,
nickname: 'nickname',
content: 'content',
dateTime: '2023-04-04 14:00',
profile: '',
child: [],
children: [],
},
],
},
Expand All @@ -36,19 +37,18 @@ export const Default: Story = {
content: 'content',
dateTime: '2023-04-04 14:00',
profile: '',
child: [
children: [
{
commentId: 4,
nickname: 'nickname',
content: 'content',
dateTime: '2023-04-04 14:00',
profile: '',
child: [],
children: [],
},
],
},
],
onWriteClick: () => {},
},
render: (args) => <CommentList {...args} />,
};
18 changes: 14 additions & 4 deletions frontend/src/components/CommentList/CommentList.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import CommentCard from '@_components/CommentCard/CommentCard';
import * as S from '@_components/CommentList/ComentList.style';
import MessageInput from '@_components/Input/MessagInput/MessageInput';
import useWriteComment from '@_hooks/mutaions/useWriteComment';
import { Comment } from '@_types/index';
import { HTMLProps } from 'react';
import { HTMLProps, useState } from 'react';

interface CommentListProps extends HTMLProps<HTMLDivElement> {
moimId: number;
comments: Comment[];
onWriteClick: () => void;
}

export default function CommentList(props: CommentListProps) {
const { comments, onWriteClick } = props;
const { moimId, comments } = props;
const { mutate: writeComment } = useWriteComment();
const [selectedComment, setSelectedCommnet] = useState(0);

return (
<div css={S.commentListBox()}>
Expand All @@ -18,10 +22,16 @@ export default function CommentList(props: CommentListProps) {
<CommentCard
key={comment.commentId}
comment={comment}
onWriteClick={onWriteClick}
onWriteClick={() => setSelectedCommnet(comment.commentId)}
/>
);
})}
<MessageInput
placeHolder={'메세지를 입력해주세요'}
onSubmit={(message: string) =>
writeComment({ moimId, message, selectedComment })
}
></MessageInput>
</div>
);
}
16 changes: 11 additions & 5 deletions frontend/src/hooks/mutaions/useWriteComment.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';

import QUERY_KEYS from '@_constants/queryKeys';
import { postWriteComment } from '@_apis/posts';

interface WriteCommentParams {
moimId: number;
selectedComment: number;
message: string;
}

export default function useWriteComment() {
const queryClient = useQueryClient();

return useMutation<void, Error, number>({
mutationFn: postWriteComment,
onSuccess: (_, moimId) => {
return useMutation<void, Error, WriteCommentParams>({
mutationFn: ({ moimId, selectedComment, message }) =>
postWriteComment(moimId, selectedComment, message),
onSuccess: (_, { moimId }) => {
queryClient.invalidateQueries({
queryKey: [QUERY_KEYS.comment, moimId],
queryKey: [QUERY_KEYS.moim, moimId],
});
},
});
Expand Down
8 changes: 2 additions & 6 deletions frontend/src/pages/MoimDetailPage/MoimDetailPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import ZzimButton from '@_components/Zzim/ZzimButton';
import useChamyoMine from '@_hooks/queries/useChamyoMine';
import useZzimMine from '@_hooks/queries/useZzimMine';
import useChangeZzim from '@_hooks/mutaions/useChangeZzim';
import useWriteComment from '@_hooks/mutaions/useWriteComment';
import useCancelMoim from '@_hooks/mutaions/useCancelMoim';
import useReopenMoim from '@_hooks/mutaions/useReopenMoim';
import useCompleteMoin from '@_hooks/mutaions/useCompleteMoin';
Expand All @@ -36,7 +35,7 @@ export default function MoimDetailPage() {
const { mutate } = useJoinMoim(() => {
navigate(ROUTES.participationComplete);
});
const { mutate: writeComment } = useWriteComment();

const { mutate: cancelMoim } = useCancelMoim();

const { mutate: ReopenMoim } = useReopenMoim();
Expand Down Expand Up @@ -100,10 +99,7 @@ export default function MoimDetailPage() {
)}
{moim.comments && (
<MoimDescription title="코멘트" color="grey">
<CommentList
comments={moim.comments}
onWriteClick={() => writeComment(moimId)}
/>
<CommentList moimId={moimId} comments={moim.comments} />
</MoimDescription>
)}
</InformationLayout.ContentContainer>
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface Comment {
content: string;
dateTime: string;
profile: string;
child: Comment[];
children: Comment[];
}
export type MoimInputInfo = Omit<
MoimInfo,
Expand Down
Loading