@@ -8,6 +8,8 @@ import gql from 'graphql-tag';
8
8
import { optionalChaining } from '../utils/helpers' ;
9
9
import { loginUser } from '../utils/auth' ;
10
10
import { handleMutationResponse , ApiErrors } from '../utils/errorHandling' ;
11
+ import UpvoteMutation from './UpvoteMutation' ;
12
+ import BookmarkMutation from './BookmarkMutation' ;
11
13
12
14
type TutorialListingProps = {
13
15
tutorial : Tutorial ;
@@ -20,19 +22,21 @@ type Tutorial = {
20
22
} ;
21
23
22
24
type FrontMatter = {
25
+ id : string ;
23
26
tutorialTitle : string ;
24
27
description : string ;
25
28
} ;
26
29
27
30
const TutorialListing : React . FunctionComponent < TutorialListingProps > = ( {
28
31
tutorial,
29
32
} ) => {
30
- const tutorialId = 'cjwi9iv2klfsb0b14sqc9wpuj' ;
33
+ const gatsbyID = tutorial . frontmatter . id ;
34
+ console . log ( gatsbyID ) ;
31
35
return (
32
36
< Query
33
37
query = { gql `
34
- query TutorialListing($id: ID !) {
35
- tutorial(id : $id ) {
38
+ query gatsbyTutorialQuery($gatsbyID: String !) {
39
+ gatsbyTutorialQuery(gatsbyID : $gatsbyID ) {
36
40
id
37
41
upvotes
38
42
numberOfStudents
@@ -44,95 +48,21 @@ const TutorialListing: React.FunctionComponent<TutorialListingProps> = ({
44
48
}
45
49
}
46
50
` }
47
- variables = { { id : tutorialId } }
51
+ variables = { { gatsbyID : gatsbyID } }
48
52
>
49
- { ( { data } ) => {
53
+ { ( { loading, error, data } ) => {
54
+ if ( error ) return `Error! ${ error . message } ` ;
55
+
50
56
return (
51
57
< Card width = { [ 1 ] } p = { 4 } my = { 4 } borderRadius = { 8 } boxShadow = "small" >
52
58
< Flex alignItems = "center" justifyContent = "center" >
53
59
< Box width = { 1 / 12 } >
54
- < Mutation
55
- mutation = { gql `
56
- mutation UpvoteTutorial($id: ID!) {
57
- upvoteTutorial(tutorialId: $id) {
58
- code
59
- success
60
- userTutorial {
61
- id
62
- upvoted
63
- tutorial {
64
- id
65
- upvotes
66
- }
67
- }
68
- }
69
- }
70
- ` }
71
- variables = { {
72
- id : tutorialId ,
73
- } }
74
- >
75
- { upvote => {
76
- return (
77
- < Upvote
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
- ) }
93
- count = { optionalChaining ( ( ) => data . tutorial . upvotes ) }
94
- />
95
- ) ;
96
- } }
97
- </ Mutation >
98
- < Mutation
99
- mutation = { gql `
100
- mutation BookmarkTutorial($id: ID!) {
101
- bookmarkTutorial(tutorialId: $id) {
102
- code
103
- success
104
- userTutorial {
105
- id
106
- bookmarked
107
- }
108
- }
109
- }
110
- ` }
111
- variables = { {
112
- id : tutorialId ,
113
- } }
114
- >
115
- { bookmark => {
116
- return (
117
- < Button
118
- onClick = { async ( ) => {
119
- const mutationRes = await handleMutationResponse (
120
- bookmark ( ) ,
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
- Bookmark
132
- </ Button >
133
- ) ;
134
- } }
135
- </ Mutation >
60
+ { data . gatsbyTutorialQuery && (
61
+ < UpvoteMutation tutorial = { data . gatsbyTutorialQuery } />
62
+ ) }
63
+ { data . gatsbyTutorialQuery && (
64
+ < BookmarkMutation tutorial = { data . gatsbyTutorialQuery } />
65
+ ) }
136
66
</ Box >
137
67
< Box width = { 11 / 12 } >
138
68
< Link to = { getTutorialOverviewSlug ( tutorial . fileAbsolutePath ) } >
0 commit comments