Skip to content

Commit 826e4bb

Browse files
committed
Task_4_Done
1 parent ec5a56b commit 826e4bb

File tree

2 files changed

+79
-13
lines changed

2 files changed

+79
-13
lines changed

src/Components/Post.jsx

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
import React, { useState } from 'react';
2-
import { useQuery } from '@apollo/react-hooks';
1+
import React, { useState, useContext } from 'react';
2+
import { useQuery, useApolloClient } from '@apollo/react-hooks';
33
import {
44
Button, Container, Card, Row, Jumbotron, Spinner, Form,
55
} from 'react-bootstrap';
66
import { get } from 'lodash';
77
import { DateTime } from 'luxon';
88
import PropTypes from 'prop-types';
99

10+
import UserContext from '../Context/UserContext';
1011
import queries from '../query';
1112

1213
const Post = ({ postId }) => {
1314
const [commentContent, setCommentContent] = useState('');
14-
const [commentCreateIsLoading] = useState(false);
15+
const [commentCreateIsLoading, setCommentCreateIsLoading] = useState(false);
16+
const { user } = useContext(UserContext);
17+
const apolloClient = useApolloClient();
1518

1619
const {
1720
loading: postIsLoading,
@@ -24,15 +27,44 @@ const Post = ({ postId }) => {
2427
});
2528
const post = get(postsData, 'posts.edges[0].node', {});
2629

27-
// TODO: TASK 4. query comments
28-
const commentIsLoading = false;
29-
const comments = [];
30-
const hasNextCommentPage = false;
30+
const {
31+
loading: commentIsLoading,
32+
data: commentsData,
33+
fetchMore: fetchMoreComment,
34+
refetch: refetchComments,
35+
} = useQuery(queries.comment.GET_COMMENTS_QUERY, {
36+
variables: {
37+
filters: [{ field: 'post', operation: 'eq', value: postId }],
38+
order: {
39+
field: 'timestamp',
40+
direction: 'desc',
41+
},
42+
offset: 0,
43+
limit: 3,
44+
},
45+
fetchPolicy: 'cache-and-network',
46+
});
47+
const comments = get(commentsData, 'comments.edges', []);
48+
const hasNextCommentPage = get(commentsData, 'comments.pageInfo.hasNextPage', false);
3149

32-
// TODO: TASK 4. EXTRA handleCreateComment
3350
const handleCreateComment = (event) => {
3451
event.preventDefault();
35-
console.log('handleCreateComment');
52+
if (commentContent && user && !commentCreateIsLoading) {
53+
setCommentCreateIsLoading(true);
54+
apolloClient.mutate({
55+
mutation: queries.comment.CREATE_COMMENT_MUTATION,
56+
variables: {
57+
content: commentContent, author: user.email, post: postId,
58+
},
59+
}).then(() => {
60+
setCommentCreateIsLoading(false);
61+
setCommentContent('');
62+
refetchComments();
63+
}).catch(() => {
64+
alert('Something went wrong...');
65+
setCommentCreateIsLoading(false);
66+
});
67+
}
3668
};
3769

3870
return (
@@ -88,8 +120,26 @@ const Post = ({ postId }) => {
88120
className="comments-load-more"
89121
disabled={commentIsLoading || !hasNextCommentPage}
90122
variant="primary"
91-
// TODO: TASK 4. fetch more comments
92-
onClick={() => (console.log('fetchMoreComment'))}
123+
onClick={() => (
124+
fetchMoreComment({
125+
variables: { offset: comments.length },
126+
updateQuery: (prev, { fetchMoreResult }) => (
127+
fetchMoreResult
128+
? ({
129+
...prev,
130+
comments: {
131+
...prev.comments,
132+
pageInfo: fetchMoreResult.comments.pageInfo,
133+
edges: [
134+
...prev.comments.edges,
135+
...fetchMoreResult.comments.edges,
136+
],
137+
},
138+
})
139+
: prev
140+
),
141+
})
142+
)}
93143
>
94144
{(commentIsLoading || hasNextCommentPage) ? 'Load more' : 'Out of comments'}
95145
</Button>

src/query/comment.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,24 @@ const GET_COMMENTS_QUERY = gql`
2929
${fragments.comment}
3030
`;
3131

32-
// TODO: TASK 4. EXTRA write comment creation mutation
33-
const CREATE_COMMENT_MUTATION = null;
32+
const CREATE_COMMENT_MUTATION = gql`
33+
mutation createComment(
34+
$content: String!
35+
$author: String!
36+
$post: ID!
37+
) {
38+
comment {
39+
createComment(input: {
40+
content: $content
41+
author: $author
42+
post: $post
43+
}) {
44+
...Comment
45+
}
46+
}
47+
}
48+
${fragments.comment}
49+
`;
3450

3551
export default {
3652
GET_COMMENTS_QUERY,

0 commit comments

Comments
 (0)