11import * as React from 'react' ;
2- import { Heading , Text , Card , Flex , Box } from './shared/base' ;
2+ import { Heading , Text , Card , Flex , Box , Button } from './shared/base' ;
33import { getTutorialOverviewSlug } from '../utils/getTutorialSlug' ;
44import Upvote from './Upvote' ;
55import { Link } from 'gatsby' ;
66import { Query , Mutation } from 'react-apollo' ;
77import gql from 'graphql-tag' ;
88import { optionalChaining } from '../utils/helpers' ;
9+ import { loginUser } from '../utils/auth' ;
10+ import { handleMutationResponse , ApiErrors } from '../utils/errorHandling' ;
911
1012type TutorialListingProps = {
1113 tutorial : Tutorial ;
@@ -25,7 +27,7 @@ type FrontMatter = {
2527const TutorialListing : React . FunctionComponent < TutorialListingProps > = ( {
2628 tutorial,
2729} ) => {
28- const tutorialId = " cjwb6f2hy7e4f0b14bxh1mar2" ;
30+ const tutorialId = ' cjwb6f2hy7e4f0b14bxh1mar2' ;
2931 return (
3032 < Query
3133 query = { gql `
@@ -67,17 +69,68 @@ const TutorialListing: React.FunctionComponent<TutorialListingProps> = ({
6769 }
6870 ` }
6971 variables = { {
70- id : tutorialId
72+ id : tutorialId ,
7173 } }
7274 >
73- { ( upvote ) => {
75+ { upvote => {
7476 return (
7577 < Upvote
76- onClick = { ( ) => upvote ( ) }
77- active = { optionalChaining ( ( ) => data . tutorial . viewerUserTutorial . upvoted ) }
78+ onClick = { async ( ) => {
79+ const mutationRes = await handleMutationResponse (
80+ upvote ( ) ,
81+ ) ;
82+ if ( mutationRes . error ) {
83+ if ( mutationRes . error === ApiErrors . AUTHORIZATION ) {
84+ loginUser ( ) ;
85+ } else {
86+ console . log ( mutationRes . error ) ;
87+ }
88+ }
89+ } }
90+ active = { optionalChaining (
91+ ( ) => data . tutorial . viewerUserTutorial . upvoted ,
92+ ) }
7893 count = { optionalChaining ( ( ) => data . tutorial . upvotes ) }
7994 />
80- )
95+ ) ;
96+ } }
97+ </ Mutation >
98+ < Mutation
99+ mutation = { gql `
100+ mutation SaveTutorial($id: ID!) {
101+ saveTutorial(tutorialId: $id) {
102+ code
103+ success
104+ userTutorial {
105+ id
106+ saved
107+ }
108+ }
109+ }
110+ ` }
111+ variables = { {
112+ id : tutorialId ,
113+ } }
114+ >
115+ { save => {
116+ return (
117+ < Button
118+ onClick = { async ( ) => {
119+ const mutationRes = await handleMutationResponse (
120+ save ( ) ,
121+ ) ;
122+ if ( mutationRes . error ) {
123+ if ( mutationRes . error === ApiErrors . AUTHORIZATION ) {
124+ loginUser ( ) ;
125+ } else {
126+ console . log ( mutationRes . error ) ;
127+ }
128+ }
129+ } }
130+ >
131+ Save
132+ </ Button >
133+ ) ;
81134 } }
82135 </ Mutation >
83136 </ Box >
@@ -89,7 +142,7 @@ const TutorialListing: React.FunctionComponent<TutorialListingProps> = ({
89142 </ Box >
90143 </ Flex >
91144 </ Card >
92- )
145+ ) ;
93146 } }
94147 </ Query >
95148 ) ;
0 commit comments