Skip to content

Commit 59cbbd3

Browse files
committed
Commit before facing unfixable bug
1 parent e711323 commit 59cbbd3

File tree

6 files changed

+120
-26
lines changed

6 files changed

+120
-26
lines changed

src/components/post/PostCommentItem.tsx

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import * as React from 'react';
1+
import React, { useEffect, useState } from 'react';
22
import styled from 'styled-components';
33
import { Comment } from '../../lib/graphql/post';
44
import palette from '../../lib/styles/palette';
55
import { formatDate } from '../../lib/utils';
66
import Typography from '../common/Typography';
7-
import { PlusBoxIcon } from '../../static/svg';
7+
import { PlusBoxIcon, MinusBoxIcon } from '../../static/svg';
88
import { userThumbnail } from '../../static/images';
9+
import useBoolean from '../../lib/hooks/useBoolean';
910

1011
const PostCommentItemBlock = styled.div`
1112
padding-top: 1.5rem;
@@ -61,23 +62,66 @@ const CommentHead = styled.div`
6162
const CommentText = styled.p``;
6263
const CommentFoot = styled.div`
6364
margin-top: 2rem;
64-
.toggler {
65-
display: flex;
66-
align-items: center;
67-
color: ${palette.teal6};
68-
font-weight: bold;
69-
svg {
70-
margin-right: 0.5rem;
71-
}
65+
`;
66+
const TogglerBlock = styled.div`
67+
display: flex;
68+
align-items: center;
69+
color: ${palette.teal6};
70+
font-weight: bold;
71+
svg {
72+
margin-right: 0.5rem;
73+
}
74+
cursor: pointer;
75+
&:hover {
76+
color: ${palette.teal5};
7277
}
7378
`;
7479

7580
export interface PostCommentItemProps {
7681
comment: Comment;
82+
onLoadReplies: (id: string) => Promise<any>;
83+
onReply: (options: { commentId: string; text: string }) => any;
84+
}
85+
86+
interface TogglerProps {
87+
open: boolean;
88+
count: number;
89+
onToggle: () => any;
7790
}
7891

79-
const PostCommentItem: React.FC<PostCommentItemProps> = ({ comment }) => {
80-
const { user, created_at, text } = comment;
92+
const Toggler: React.FC<TogglerProps> = ({ open, onToggle, count }) => {
93+
const openText = count ? `${count}개의 답글` : `답글 달기`;
94+
95+
return (
96+
<TogglerBlock onClick={onToggle}>
97+
{open ? <MinusBoxIcon /> : <PlusBoxIcon />}
98+
<span>{open ? '숨기기' : openText}</span>
99+
</TogglerBlock>
100+
);
101+
};
102+
103+
const PostCommentItem: React.FC<PostCommentItemProps> = ({
104+
comment,
105+
onLoadReplies,
106+
onReply,
107+
}) => {
108+
const { id, user, created_at, text, replies, replies_count } = comment;
109+
const [open, onToggle] = useBoolean(false);
110+
111+
console.log(comment);
112+
useEffect(() => {
113+
const fetch = async () => {
114+
try {
115+
await onLoadReplies(id);
116+
} catch (e) {
117+
console.log(e);
118+
}
119+
};
120+
if (open && !replies) {
121+
fetch();
122+
}
123+
}, [id, onLoadReplies, open, replies]);
124+
81125
return (
82126
<PostCommentItemBlock>
83127
<CommentHead>
@@ -100,10 +144,7 @@ const PostCommentItem: React.FC<PostCommentItemProps> = ({ comment }) => {
100144
<CommentText>{text}</CommentText>
101145
</Typography>
102146
<CommentFoot>
103-
<div className="toggler">
104-
<PlusBoxIcon />
105-
<span>답글 달기</span>
106-
</div>
147+
<Toggler open={open} onToggle={onToggle} count={replies_count} />
107148
</CommentFoot>
108149
</PostCommentItemBlock>
109150
);

src/components/post/PostCommentsList.tsx

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,23 @@ const PostCommentsListBlock = styled.div`
1010
export interface PostCommentsListProps {
1111
comments: Comment[];
1212
onLoadReplies: (id: string) => any;
13-
onReply: (options: {
14-
commentId: string;
15-
postId: string;
16-
text: string;
17-
}) => any;
13+
onReply: (options: { commentId: string; text: string }) => any;
1814
}
1915

20-
const PostCommentsList: React.FC<PostCommentsListProps> = ({ comments }) => {
16+
const PostCommentsList: React.FC<PostCommentsListProps> = ({
17+
comments,
18+
onLoadReplies,
19+
onReply,
20+
}) => {
2121
return (
2222
<PostCommentsListBlock>
2323
{comments.map(comment => (
24-
<PostCommentItem comment={comment} key={comment.id} />
24+
<PostCommentItem
25+
comment={comment}
26+
key={comment.id}
27+
onLoadReplies={onLoadReplies}
28+
onReply={onReply}
29+
/>
2530
))}
2631
</PostCommentsListBlock>
2732
);

src/components/post/__tests__/PostCommentItem.test.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ describe('PostCommentItem', () => {
2020
};
2121

2222
const setup = (props: Partial<PostCommentItemProps> = {}) => {
23-
const initialProps: PostCommentItemProps = { comment: sampleComment };
23+
const initialProps: PostCommentItemProps = {
24+
comment: sampleComment,
25+
onLoadReplies: () => Promise.resolve(),
26+
onReply: () => {},
27+
};
2428
const utils = render(<PostCommentItem {...initialProps} {...props} />);
2529
return {
2630
...utils,

src/containers/post/PostComments.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ const PostComments: React.FC<PostCommentsProps> = ({ comments, postId }) => {
1818
{(writeComment, { client }: MutationResult) => {
1919
type ReplyOptions = {
2020
commentId: string;
21-
postId: string;
2221
text: string;
2322
};
2423
const onLoadReplies = (id: string) => {
@@ -30,7 +29,7 @@ const PostComments: React.FC<PostCommentsProps> = ({ comments, postId }) => {
3029
fetchPolicy: 'network-only',
3130
});
3231
};
33-
const onReply = async ({ commentId, postId, text }: ReplyOptions) => {
32+
const onReply = async ({ commentId, text }: ReplyOptions) => {
3433
try {
3534
await writeComment({
3635
variables: {

src/lib/graphql/post.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,24 @@ export const RELOAD_COMMENTS = gql`
166166
}
167167
`;
168168

169+
export const GET_COMMENT = gql`
170+
query GetComment($id: ID!) {
171+
comment(comment_id: $id) {
172+
id
173+
user {
174+
id
175+
username
176+
profile {
177+
thumbnail
178+
}
179+
}
180+
text
181+
replies_count
182+
created_at
183+
}
184+
}
185+
`;
186+
169187
export const RELOAD_REPLIES = gql`
170188
query ReloadReplies($id: ID!) {
171189
comment(comment_id: $id) {
@@ -180,6 +198,19 @@ export const RELOAD_REPLIES = gql`
180198
text
181199
replies_count
182200
created_at
201+
replies {
202+
id
203+
user {
204+
id
205+
username
206+
profile {
207+
thumbnail
208+
}
209+
}
210+
text
211+
replies_count
212+
created_at
213+
}
183214
}
184215
}
185216
`;

src/lib/hooks/useBoolean.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { useState, useCallback } from 'react';
2+
3+
const useBoolean = (initialValue: boolean) => {
4+
const [value, setValue] = useState(initialValue);
5+
const toggle = useCallback(() => setValue(value => !value), []);
6+
7+
return [value, toggle, setValue] as [
8+
typeof value,
9+
typeof toggle,
10+
typeof setValue
11+
];
12+
};
13+
14+
export default useBoolean;

0 commit comments

Comments
 (0)