Skip to content

Commit 217a99e

Browse files
committed
Remove onReply and onLoadReply from Comments
1 parent e2737df commit 217a99e

File tree

9 files changed

+32
-86
lines changed

9 files changed

+32
-86
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"@types/react-dom": "16.8.0",
3030
"@types/react-icons": "^2.2.6",
3131
"@types/react-outside-click-handler": "^1.2.0",
32-
"@types/react-redux": "^7.0.9",
32+
"@types/react-redux": "^7.1.0",
3333
"@types/react-router-dom": "^4.3.1",
3434
"@types/react-textarea-autosize": "^4.3.3",
3535
"@types/redux-devtools-extension": "^2.13.2",

src/components/post/PostCommentItem.tsx

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,6 @@ const TogglerBlock = styled.div`
7979

8080
export interface PostCommentItemProps {
8181
comment: Comment;
82-
onLoadReplies: (id: string) => Promise<any>;
83-
onReply: (options: { commentId: string; text: string }) => any;
8482
}
8583

8684
interface TogglerProps {
@@ -100,28 +98,10 @@ const Toggler: React.FC<TogglerProps> = ({ open, onToggle, count }) => {
10098
);
10199
};
102100

103-
const PostCommentItem: React.FC<PostCommentItemProps> = ({
104-
comment,
105-
onLoadReplies,
106-
onReply,
107-
}) => {
108-
const { id, user, created_at, text, replies, replies_count } = comment;
101+
const PostCommentItem: React.FC<PostCommentItemProps> = ({ comment }) => {
102+
const { id, user, created_at, text, replies_count } = comment;
109103
const [open, onToggle] = useBoolean(false);
110104

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-
125105
return (
126106
<PostCommentItemBlock>
127107
<CommentHead>

src/components/post/PostCommentsList.tsx

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,13 @@ const PostCommentsListBlock = styled.div`
99

1010
export interface PostCommentsListProps {
1111
comments: Comment[];
12-
onLoadReplies: (id: string) => any;
13-
onReply: (options: { commentId: string; text: string }) => any;
1412
}
1513

16-
const PostCommentsList: React.FC<PostCommentsListProps> = ({
17-
comments,
18-
onLoadReplies,
19-
onReply,
20-
}) => {
14+
const PostCommentsList: React.FC<PostCommentsListProps> = ({ comments }) => {
2115
return (
2216
<PostCommentsListBlock>
2317
{comments.map(comment => (
24-
<PostCommentItem
25-
comment={comment}
26-
key={comment.id}
27-
onLoadReplies={onLoadReplies}
28-
onReply={onReply}
29-
/>
18+
<PostCommentItem comment={comment} key={comment.id} />
3019
))}
3120
</PostCommentsListBlock>
3221
);

src/components/post/PostReplies.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
import React from 'react';
22
import styled from 'styled-components';
33
import { Comment } from '../../lib/graphql/post';
4+
import PostCommentsList from './PostCommentsList';
45

56
const PostRepliesBlock = styled.div``;
67

78
export interface PostRepliesProps {
89
comments: Comment[];
910
}
1011

11-
const PostReplies: React.FC<PostRepliesProps> = props => {
12-
return <PostRepliesBlock />;
12+
const PostReplies: React.FC<PostRepliesProps> = ({ comments }) => {
13+
return (
14+
<PostRepliesBlock>
15+
<PostCommentsList comments={comments} />
16+
</PostRepliesBlock>
17+
);
1318
};
1419

1520
export default PostReplies;

src/containers/post/PostComments.tsx

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,44 +14,7 @@ const PostComments: React.FC<PostCommentsProps> = ({ comments, postId }) => {
1414
return (
1515
<PostCommentsTemplate count={comments.length}>
1616
<PostCommentsWriteContainer postId={postId} />
17-
<Mutation mutation={WRITE_COMMENT}>
18-
{(writeComment, { client }: MutationResult) => {
19-
type ReplyOptions = {
20-
commentId: string;
21-
text: string;
22-
};
23-
const onLoadReplies = (id: string) => {
24-
// return client.query({
25-
// query: RELOAD_REPLIES,
26-
// variables: {
27-
// id: id,
28-
// },
29-
// fetchPolicy: 'network-only',
30-
// });
31-
};
32-
const onReply = async ({ commentId, text }: ReplyOptions) => {
33-
try {
34-
await writeComment({
35-
variables: {
36-
post_id: postId,
37-
comment_id: commentId,
38-
text,
39-
},
40-
});
41-
await onLoadReplies(commentId);
42-
} catch (e) {
43-
console.log(e);
44-
}
45-
};
46-
return (
47-
<PostCommentsList
48-
comments={comments}
49-
onLoadReplies={onLoadReplies}
50-
onReply={onReply}
51-
/>
52-
);
53-
}}
54-
</Mutation>
17+
<PostCommentsList comments={comments} />;
5518
</PostCommentsTemplate>
5619
);
5720
};

src/containers/post/PostRepliesContainer.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@ import React from 'react';
22
import { Query, QueryResult } from 'react-apollo';
33
import { GET_REPLIES, CommentWithReplies } from '../../lib/graphql/post';
44
import PostReplies from '../../components/post/PostReplies';
5+
import { useSelector } from 'react-redux';
6+
import { RootState } from '../../modules';
57

68
export interface PostRepliesProps {
7-
postId: string;
89
commentId: string;
910
}
1011

11-
const PostRepliesContainer: React.FC<PostRepliesProps> = ({
12-
postId,
13-
commentId,
14-
}) => {
12+
const PostRepliesContainer: React.FC<PostRepliesProps> = ({ commentId }) => {
13+
const postId = useSelector((state: RootState) => state.post.id);
1514
return (
1615
<Query query={GET_REPLIES} variables={{ id: commentId }}>
1716
{({

src/containers/post/PostViewer.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
import * as React from 'react';
2+
import { useDispatch, useSelector } from 'react-redux';
23
import { Query, QueryResult } from 'react-apollo';
34
import { READ_POST, SinglePost } from '../../lib/graphql/post';
45
import PostHead from '../../components/post/PostHead';
56
import PostContent from '../../components/post/PostContent';
67
import PostComments from './PostComments';
8+
import { RootState } from '../../modules';
9+
import { postActions } from '../../modules/post';
710

811
export interface PostViewerProps {
912
username: string;
1013
urlSlug: string;
1114
}
1215

1316
const PostViewer: React.FC<PostViewerProps> = ({ username, urlSlug }) => {
17+
const postId = useSelector((state: RootState) => state.post.id);
18+
const dispatch = useDispatch();
1419
return (
1520
<Query
1621
query={READ_POST}
@@ -27,6 +32,10 @@ const PostViewer: React.FC<PostViewerProps> = ({ username, urlSlug }) => {
2732
if (loading) return null; // TODO: show placeholder
2833
if (!data || !data.post) return null;
2934
const { post } = data;
35+
36+
if (postId !== post.id) {
37+
dispatch(postActions.setPostId(post.id));
38+
}
3039
return (
3140
<>
3241
<PostHead

src/containers/post/__tests__/PostViewer.test.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import PostViewer, { PostViewerProps } from '../PostViewer';
44
import { MemoryRouter } from 'react-router-dom';
55
import { MockedProvider } from 'react-apollo/test-utils';
66
import { READ_POST } from '../../../lib/graphql/post';
7+
import renderWithRedux from '../../../lib/renderWithRedux';
78

89
describe('PostViewer', () => {
910
const setup = (props: Partial<PostViewerProps> = {}, overrideMocks?: any) => {
@@ -160,7 +161,7 @@ describe('PostViewer', () => {
160161
},
161162
},
162163
];
163-
const utils = render(
164+
const utils = renderWithRedux(
164165
<MockedProvider mocks={overrideMocks || mocks} addTypename={false}>
165166
<MemoryRouter>
166167
<PostViewer {...initialProps} {...props} />

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,10 +1264,10 @@
12641264
dependencies:
12651265
"@types/react" "*"
12661266

1267-
"@types/react-redux@^7.0.9":
1268-
version "7.0.9"
1269-
resolved "https://registry.yarnpkg.com/@types/react-redux/-/react-redux-7.0.9.tgz#4825ee2872c44768916304b6bb8df5b46d443b88"
1270-
integrity sha512-fMVX9SneWWw68d/JoeNUh6hj42kx2G30YhPdCYJTOv3xqbJ1xzIz6tEM/xzi7nBvpNbwZkSa9TMsV06kWOFIIg==
1267+
"@types/react-redux@^7.1.0":
1268+
version "7.1.0"
1269+
resolved "https://registry.yarnpkg.com/@types/react-redux/-/react-redux-7.1.0.tgz#ed2c90ff192433251bd8a264195529f3613d53e2"
1270+
integrity sha512-SBJd6oNACDU/taMcDzFfj0mbVa+OI42L8WNvgPsCWiWUNIavPLeX5oA22V64tYIDUc7cGmJjDyLlWeCrqpZu1w==
12711271
dependencies:
12721272
"@types/hoist-non-react-statics" "^3.3.0"
12731273
"@types/react" "*"

0 commit comments

Comments
 (0)