@@ -10,7 +10,7 @@ const axiosGitHubGraphQL = axios.create({
1010 } ,
1111} ) ;
1212
13- const title = 'Simple React GraphQL GitHub Client' ;
13+ const title = 'React GraphQL GitHub Client' ;
1414
1515const getIssuesOfRepositoryQuery = ( organization , repository ) => `
1616 {
@@ -57,7 +57,7 @@ const getAddReactionToIssueMutation = issueId => `
5757` ;
5858
5959const addReactionToIssue = issueId => {
60- axiosGitHubGraphQL . post ( '/graphql' , {
60+ return axiosGitHubGraphQL . post ( '/graphql' , {
6161 query : getAddReactionToIssueMutation ( issueId ) ,
6262 } ) ;
6363} ;
@@ -70,6 +70,51 @@ const getIssuesOfRepository = path => {
7070 } ) ;
7171} ;
7272
73+ const updatedIssue = ( issue , newReaction ) => {
74+ return {
75+ ...issue ,
76+ node : {
77+ ...issue . node ,
78+ reactions : {
79+ ...issue . node . reactions ,
80+ edges : [ ...issue . node . reactions . edges , { node : newReaction } ] ,
81+ } ,
82+ } ,
83+ } ;
84+ } ;
85+
86+ const updatedIssueInState = mutationResult => state => {
87+ const { issues } = state . result . organization . repository ;
88+ const { reaction, subject } = mutationResult . data . data . addReaction ;
89+
90+ const newReaction = { content : reaction . content , id : subject . id } ;
91+
92+ const updatedIssues = issues . edges . map ( issue => {
93+ if ( issue . node . id === subject . id ) {
94+ return updatedIssue ( issue , newReaction ) ;
95+ } else {
96+ return issue ;
97+ }
98+ } ) ;
99+
100+ return {
101+ ...state ,
102+ result : {
103+ ...state . result ,
104+ organization : {
105+ ...state . result . organization ,
106+ repository : {
107+ ...state . result . organization . repository ,
108+ issues : {
109+ ...state . result . organization . repository . issues ,
110+ edges : updatedIssues ,
111+ } ,
112+ } ,
113+ } ,
114+ } ,
115+ } ;
116+ } ;
117+
73118class App extends Component {
74119 state = {
75120 input : 'the-road-to-learn-react/the-road-to-learn-react' ,
@@ -100,6 +145,12 @@ class App extends Component {
100145 ) ;
101146 } ;
102147
148+ onAddReactionToIssue = issueId => {
149+ addReactionToIssue ( issueId ) . then ( mutationResult =>
150+ this . setState ( updatedIssueInState ( mutationResult ) ) ,
151+ ) ;
152+ } ;
153+
103154 render ( ) {
104155 const { input, result, errors } = this . state ;
105156
@@ -127,6 +178,7 @@ class App extends Component {
127178 < Organization
128179 organization = { result . organization }
129180 errors = { errors }
181+ onAddReactionToIssue = { this . onAddReactionToIssue }
130182 />
131183 ) : (
132184 < p > No information yet ...</ p >
@@ -136,7 +188,11 @@ class App extends Component {
136188 }
137189}
138190
139- const Organization = ( { organization, errors } ) => {
191+ const Organization = ( {
192+ organization,
193+ errors,
194+ onAddReactionToIssue,
195+ } ) => {
140196 if ( errors ) {
141197 return (
142198 < p >
@@ -152,12 +208,15 @@ const Organization = ({ organization, errors }) => {
152208 < strong > Issues from Organization:</ strong >
153209 { organization . name } ({ organization . url } )
154210 </ p >
155- < Repository repository = { organization . repository } />
211+ < Repository
212+ repository = { organization . repository }
213+ onAddReactionToIssue = { onAddReactionToIssue }
214+ />
156215 </ div >
157216 ) ;
158217} ;
159218
160- const Repository = ( { repository } ) => (
219+ const Repository = ( { repository, onAddReactionToIssue } ) => (
161220 < div >
162221 < p >
163222 < strong > In Repository:</ strong >
@@ -171,7 +230,7 @@ const Repository = ({ repository }) => (
171230
172231 < button
173232 type = "button"
174- onClick = { ( ) => addReactionToIssue ( issue . node . id ) }
233+ onClick = { ( ) => onAddReactionToIssue ( issue . node . id ) }
175234 >
176235 Say "Horray"
177236 </ button >
0 commit comments