@@ -18,8 +18,13 @@ const GET_ISSUES_OF_REPOSITORY = `
18
18
name
19
19
url
20
20
repository(name: $repository) {
21
+ id
21
22
name
22
23
url
24
+ stargazers {
25
+ totalCount
26
+ }
27
+ viewerHasStarred
23
28
issues(first: 5, after: $cursor, states: [OPEN]) {
24
29
edges {
25
30
node {
@@ -47,6 +52,26 @@ const GET_ISSUES_OF_REPOSITORY = `
47
52
}
48
53
` ;
49
54
55
+ const ADD_STAR = `
56
+ mutation ($repositoryId: ID!) {
57
+ addStar(input:{starrableId:$repositoryId}) {
58
+ starrable {
59
+ viewerHasStarred
60
+ }
61
+ }
62
+ }
63
+ ` ;
64
+
65
+ const REMOVE_STAR = `
66
+ mutation ($repositoryId: ID!) {
67
+ removeStar(input:{starrableId:$repositoryId}) {
68
+ starrable {
69
+ viewerHasStarred
70
+ }
71
+ }
72
+ }
73
+ ` ;
74
+
50
75
const getIssuesOfRepository = ( path , cursor ) => {
51
76
const [ organization , repository ] = path . split ( '/' ) ;
52
77
@@ -85,6 +110,64 @@ const resolveIssuesQuery = (queryResult, cursor) => state => {
85
110
} ;
86
111
} ;
87
112
113
+ const addStarToRepository = repositoryId => {
114
+ return axiosGitHubGraphQL . post ( '' , {
115
+ query : ADD_STAR ,
116
+ variables : { repositoryId } ,
117
+ } ) ;
118
+ } ;
119
+
120
+ const resolveAddStarMutation = mutationResult => state => {
121
+ const {
122
+ viewerHasStarred,
123
+ } = mutationResult . data . data . addStar . starrable ;
124
+
125
+ const { totalCount } = state . organization . repository . stargazers ;
126
+
127
+ return {
128
+ ...state ,
129
+ organization : {
130
+ ...state . organization ,
131
+ repository : {
132
+ ...state . organization . repository ,
133
+ viewerHasStarred,
134
+ stargazers : {
135
+ totalCount : totalCount + 1 ,
136
+ } ,
137
+ } ,
138
+ } ,
139
+ } ;
140
+ } ;
141
+
142
+ const removeStarFromRepository = repositoryId => {
143
+ return axiosGitHubGraphQL . post ( '' , {
144
+ query : REMOVE_STAR ,
145
+ variables : { repositoryId } ,
146
+ } ) ;
147
+ } ;
148
+
149
+ const resolveRemoveStarMutation = mutationResult => state => {
150
+ const {
151
+ viewerHasStarred,
152
+ } = mutationResult . data . data . removeStar . starrable ;
153
+
154
+ const { totalCount } = state . organization . repository . stargazers ;
155
+
156
+ return {
157
+ ...state ,
158
+ organization : {
159
+ ...state . organization ,
160
+ repository : {
161
+ ...state . organization . repository ,
162
+ viewerHasStarred,
163
+ stargazers : {
164
+ totalCount : totalCount - 1 ,
165
+ } ,
166
+ } ,
167
+ } ,
168
+ } ;
169
+ } ;
170
+
88
171
class App extends Component {
89
172
state = {
90
173
path : 'the-road-to-learn-react/the-road-to-learn-react' ,
@@ -120,6 +203,18 @@ class App extends Component {
120
203
this . onFetchFromGitHub ( this . state . path , endCursor ) ;
121
204
} ;
122
205
206
+ onStarRepository = ( repositoryId , viewerHasStarred ) => {
207
+ if ( viewerHasStarred ) {
208
+ removeStarFromRepository ( repositoryId ) . then ( mutationResult =>
209
+ this . setState ( resolveRemoveStarMutation ( mutationResult ) ) ,
210
+ ) ;
211
+ } else {
212
+ addStarToRepository ( repositoryId ) . then ( mutationResult =>
213
+ this . setState ( resolveAddStarMutation ( mutationResult ) ) ,
214
+ ) ;
215
+ }
216
+ } ;
217
+
123
218
render ( ) {
124
219
const { path, organization, errors } = this . state ;
125
220
@@ -148,6 +243,7 @@ class App extends Component {
148
243
organization = { organization }
149
244
errors = { errors }
150
245
onFetchMoreIssues = { this . onFetchMoreIssues }
246
+ onStarRepository = { this . onStarRepository }
151
247
/>
152
248
) : (
153
249
< p > No information yet ...</ p >
@@ -161,6 +257,7 @@ const Organization = ({
161
257
organization,
162
258
errors,
163
259
onFetchMoreIssues,
260
+ onStarRepository,
164
261
} ) => {
165
262
if ( errors ) {
166
263
return (
@@ -180,18 +277,33 @@ const Organization = ({
180
277
< Repository
181
278
repository = { organization . repository }
182
279
onFetchMoreIssues = { onFetchMoreIssues }
280
+ onStarRepository = { onStarRepository }
183
281
/>
184
282
</ div >
185
283
) ;
186
284
} ;
187
285
188
- const Repository = ( { repository, onFetchMoreIssues } ) => (
286
+ const Repository = ( {
287
+ repository,
288
+ onFetchMoreIssues,
289
+ onStarRepository,
290
+ } ) => (
189
291
< div >
190
292
< p >
191
293
< strong > In Repository:</ strong >
192
294
< a href = { repository . url } > { repository . name } </ a >
193
295
</ p >
194
296
297
+ < button
298
+ type = "button"
299
+ onClick = { ( ) =>
300
+ onStarRepository ( repository . id , repository . viewerHasStarred )
301
+ }
302
+ >
303
+ { repository . stargazers . totalCount }
304
+ { repository . viewerHasStarred ? ' Unstar' : ' Star' }
305
+ </ button >
306
+
195
307
< ul >
196
308
{ repository . issues . edges . map ( issue => (
197
309
< li key = { issue . node . id } >
0 commit comments