11import React , { Component } from 'react' ;
22import { Mutation } from 'react-apollo' ;
33
4+ import { GET_COMMENTS_OF_ISSUE } from '../CommentList/queries' ;
45import { ADD_COMMENT } from './mutations' ;
56
67import TextArea from '../../TextArea' ;
78import Button from '../../Button' ;
89import ErrorMessage from '../../Error' ;
910
11+ const updateComments = ( {
12+ repositoryOwner,
13+ repositoryName,
14+ issue
15+ } ,
16+ client ,
17+ {
18+ data : {
19+ addComment : {
20+ commentEdge
21+ }
22+ }
23+ }
24+ ) => {
25+ const data = client . readQuery ( {
26+ query : GET_COMMENTS_OF_ISSUE ,
27+ variables : {
28+ repositoryOwner,
29+ repositoryName,
30+ number : issue . number
31+ }
32+ } ) ;
33+
34+ client . writeQuery ( {
35+ query : GET_COMMENTS_OF_ISSUE ,
36+ variables : {
37+ repositoryOwner,
38+ repositoryName,
39+ number : issue . number
40+ } ,
41+ data : {
42+ ...data ,
43+ repository : {
44+ ...data . repository ,
45+ issue : {
46+ ...data . repository . issue ,
47+ comments : {
48+ ...data . repository . issue . comments ,
49+ edges : [
50+ ...data . repository . issue . comments . edges ,
51+ commentEdge
52+ ]
53+ }
54+ }
55+ }
56+ }
57+ } )
58+ } ;
59+
1060class CommentAdd extends Component {
1161 state = {
1262 value : '' ,
@@ -23,13 +73,18 @@ class CommentAdd extends Component {
2373 } ;
2474
2575 render ( ) {
26- const { issue } = this . props ;
76+ const { issue, repositoryOwner , repositoryName } = this . props ;
2777 const { value } = this . state ;
2878
2979 return (
3080 < Mutation
3181 mutation = { ADD_COMMENT }
3282 variables = { { body : value , subjectId : issue . id } }
83+ update = { ( client , data ) => updateComments ( {
84+ repositoryOwner,
85+ repositoryName,
86+ issue
87+ } , client , data ) }
3388 >
3489 { ( addComment , { data, loading, error } ) => (
3590 < div >
0 commit comments