@@ -18,8 +18,13 @@ const issuesOfRepositoryQuery = `
1818 name
1919 url
2020 repository(name: $repository) {
21+ id
2122 name
2223 url
24+ stargazers {
25+ totalCount
26+ }
27+ viewerHasStarred
2328 issues(last: 5, states: [OPEN]) {
2429 edges {
2530 cursor
@@ -56,13 +61,30 @@ const addReactionToIssueMutation = `
5661 }
5762` ;
5863
64+ const addStarToRepositoryMutation = `
65+ mutation ($repositoryId: ID!) {
66+ addStar(input:{starrableId:$repositoryId}) {
67+ starrable {
68+ viewerHasStarred
69+ }
70+ }
71+ }
72+ ` ;
73+
5974const addReactionToIssue = issueId => {
6075 return axiosGitHubGraphQL . post ( '' , {
6176 query : addReactionToIssueMutation ,
6277 variables : { issueId } ,
6378 } ) ;
6479} ;
6580
81+ const addStarToRepository = repositoryId => {
82+ return axiosGitHubGraphQL . post ( '' , {
83+ query : addStarToRepositoryMutation ,
84+ variables : { repositoryId } ,
85+ } ) ;
86+ } ;
87+
6688const getIssuesOfRepository = path => {
6789 const [ organization , repository ] = path . split ( '/' ) ;
6890
@@ -114,6 +136,23 @@ const updatedIssueInState = mutationResult => state => {
114136 } ;
115137} ;
116138
139+ const resolveAddStarMutation = mutationResult => state => {
140+ const {
141+ viewerHasStarred,
142+ } = mutationResult . data . data . addStar . starrable ;
143+
144+ return {
145+ ...state ,
146+ organization : {
147+ ...state . organization ,
148+ repository : {
149+ ...state . organization . repository ,
150+ viewerHasStarred,
151+ } ,
152+ } ,
153+ } ;
154+ } ;
155+
117156class App extends Component {
118157 state = {
119158 path : 'the-road-to-learn-react/the-road-to-learn-react' ,
@@ -150,6 +189,12 @@ class App extends Component {
150189 ) ;
151190 } ;
152191
192+ onAddStarToRepository = repositoryId => {
193+ addStarToRepository ( repositoryId ) . then ( mutationResult =>
194+ this . setState ( resolveAddStarMutation ( mutationResult ) ) ,
195+ ) ;
196+ } ;
197+
153198 render ( ) {
154199 const { path, organization, errors } = this . state ;
155200
@@ -178,6 +223,7 @@ class App extends Component {
178223 organization = { organization }
179224 errors = { errors }
180225 onAddReactionToIssue = { this . onAddReactionToIssue }
226+ onAddStarToRepository = { this . onAddStarToRepository }
181227 />
182228 ) : (
183229 < p > No information yet ...</ p >
@@ -191,6 +237,7 @@ const Organization = ({
191237 organization,
192238 errors,
193239 onAddReactionToIssue,
240+ onAddStarToRepository,
194241} ) => {
195242 if ( errors ) {
196243 return (
@@ -210,18 +257,30 @@ const Organization = ({
210257 < Repository
211258 repository = { organization . repository }
212259 onAddReactionToIssue = { onAddReactionToIssue }
260+ onAddStarToRepository = { onAddStarToRepository }
213261 />
214262 </ div >
215263 ) ;
216264} ;
217265
218- const Repository = ( { repository, onAddReactionToIssue } ) => (
266+ const Repository = ( {
267+ repository,
268+ onAddReactionToIssue,
269+ onAddStarToRepository,
270+ } ) => (
219271 < div >
220272 < p >
221273 < strong > In Repository:</ strong >
222274 < a href = { repository . url } > { repository . name } </ a >
223275 </ p >
224276
277+ < button
278+ type = "button"
279+ onClick = { ( ) => onAddStarToRepository ( repository . id ) }
280+ >
281+ { repository . viewerHasStarred ? 'Unstar' : 'Star' }
282+ </ button >
283+
225284 < ul >
226285 { repository . issues . edges . map ( issue => (
227286 < li key = { issue . node . id } >
0 commit comments