@@ -58,19 +58,36 @@ const addStarToRepositoryMutation = `
5858 }
5959` ;
6060
61+ const removeStarToRepositoryMutation = `
62+ mutation ($repositoryId: ID!) {
63+ removeStar(input:{starrableId:$repositoryId}) {
64+ starrable {
65+ viewerHasStarred
66+ }
67+ }
68+ }
69+ ` ;
70+
71+ const getIssuesOfRepository = path => {
72+ const [ organization , repository ] = path . split ( '/' ) ;
73+
74+ return axiosGitHubGraphQL . post ( '' , {
75+ query : issuesOfRepositoryQuery ,
76+ variables : { organization, repository } ,
77+ } ) ;
78+ } ;
79+
6180const addStarToRepository = repositoryId => {
6281 return axiosGitHubGraphQL . post ( '' , {
6382 query : addStarToRepositoryMutation ,
6483 variables : { repositoryId } ,
6584 } ) ;
6685} ;
6786
68- const getIssuesOfRepository = path => {
69- const [ organization , repository ] = path . split ( '/' ) ;
70-
87+ const removeStarFromRepository = repositoryId => {
7188 return axiosGitHubGraphQL . post ( '' , {
72- query : issuesOfRepositoryQuery ,
73- variables : { organization , repository } ,
89+ query : removeStarToRepositoryMutation ,
90+ variables : { repositoryId } ,
7491 } ) ;
7592} ;
7693
@@ -79,13 +96,40 @@ const resolveAddStarMutation = mutationResult => state => {
7996 viewerHasStarred,
8097 } = mutationResult . data . data . addStar . starrable ;
8198
99+ const { totalCount } = state . organization . repository . stargazers ;
100+
82101 return {
83102 ...state ,
84103 organization : {
85104 ...state . organization ,
86105 repository : {
87106 ...state . organization . repository ,
88107 viewerHasStarred,
108+ stargazers : {
109+ totalCount : totalCount + 1 ,
110+ } ,
111+ } ,
112+ } ,
113+ } ;
114+ } ;
115+
116+ const resolveRemoveStarMutation = mutationResult => state => {
117+ const {
118+ viewerHasStarred,
119+ } = mutationResult . data . data . removeStar . starrable ;
120+
121+ const { totalCount } = state . organization . repository . stargazers ;
122+
123+ return {
124+ ...state ,
125+ organization : {
126+ ...state . organization ,
127+ repository : {
128+ ...state . organization . repository ,
129+ viewerHasStarred,
130+ stargazers : {
131+ totalCount : totalCount - 1 ,
132+ } ,
89133 } ,
90134 } ,
91135 } ;
@@ -121,10 +165,16 @@ class App extends Component {
121165 ) ;
122166 } ;
123167
124- onStarToRepository = repositoryId => {
125- addStarToRepository ( repositoryId ) . then ( mutationResult =>
126- this . setState ( resolveAddStarMutation ( mutationResult ) ) ,
127- ) ;
168+ onStarToRepository = ( repositoryId , viewerHasStarred ) => {
169+ if ( viewerHasStarred ) {
170+ removeStarFromRepository ( repositoryId ) . then ( mutationResult =>
171+ this . setState ( resolveRemoveStarMutation ( mutationResult ) ) ,
172+ ) ;
173+ } else {
174+ addStarToRepository ( repositoryId ) . then ( mutationResult =>
175+ this . setState ( resolveAddStarMutation ( mutationResult ) ) ,
176+ ) ;
177+ }
128178 } ;
129179
130180 render ( ) {
@@ -201,9 +251,12 @@ const Repository = ({ repository, onStarToRepository }) => (
201251
202252 < button
203253 type = "button"
204- onClick = { ( ) => onStarToRepository ( repository . id ) }
254+ onClick = { ( ) =>
255+ onStarToRepository ( repository . id , repository . viewerHasStarred )
256+ }
205257 >
206- { repository . viewerHasStarred ? 'Unstar' : 'Star' }
258+ { repository . stargazers . totalCount }
259+ { repository . viewerHasStarred ? ' Unstar' : ' Star' }
207260 </ button >
208261
209262 < ul >
0 commit comments