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' ;
3
3
import {
4
4
Button , Container , Card , Row , Jumbotron , Spinner , Form ,
5
5
} from 'react-bootstrap' ;
6
6
import { get } from 'lodash' ;
7
7
import { DateTime } from 'luxon' ;
8
8
import PropTypes from 'prop-types' ;
9
9
10
+ import UserContext from '../Context/UserContext' ;
10
11
import queries from '../query' ;
11
12
12
13
const Post = ( { postId } ) => {
13
14
const [ commentContent , setCommentContent ] = useState ( '' ) ;
14
- const [ commentCreateIsLoading ] = useState ( false ) ;
15
+ const [ commentCreateIsLoading , setCommentCreateIsLoading ] = useState ( false ) ;
16
+ const { user } = useContext ( UserContext ) ;
17
+ const apolloClient = useApolloClient ( ) ;
15
18
16
19
const {
17
20
loading : postIsLoading ,
@@ -24,15 +27,44 @@ const Post = ({ postId }) => {
24
27
} ) ;
25
28
const post = get ( postsData , 'posts.edges[0].node' , { } ) ;
26
29
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 ) ;
31
49
32
- // TODO: TASK 4. EXTRA handleCreateComment
33
50
const handleCreateComment = ( event ) => {
34
51
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
+ }
36
68
} ;
37
69
38
70
return (
@@ -88,8 +120,26 @@ const Post = ({ postId }) => {
88
120
className = "comments-load-more"
89
121
disabled = { commentIsLoading || ! hasNextCommentPage }
90
122
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
+ ) }
93
143
>
94
144
{ ( commentIsLoading || hasNextCommentPage ) ? 'Load more' : 'Out of comments' }
95
145
</ Button >
0 commit comments