@@ -3,6 +3,9 @@ import { Heading, Text, Card, Flex, Box } from './shared/base';
33import { getTutorialOverviewSlug } from '../utils/getTutorialSlug' ;
44import Upvote from './Upvote' ;
55import { Link } from 'gatsby' ;
6+ import { Query , Mutation } from 'react-apollo' ;
7+ import gql from 'graphql-tag' ;
8+ import { optionalChaining } from '../utils/helpers' ;
69
710type TutorialListingProps = {
811 tutorial : Tutorial ;
@@ -22,20 +25,73 @@ type FrontMatter = {
2225const TutorialListing : React . FunctionComponent < TutorialListingProps > = ( {
2326 tutorial,
2427} ) => {
28+ const tutorialId = "cjwb6f2hy7e4f0b14bxh1mar2" ;
2529 return (
26- < Card width = { [ 1 ] } p = { 4 } my = { 4 } borderRadius = { 8 } boxShadow = "small" >
27- < Flex alignItems = "center" justifyContent = "center" >
28- < Box width = { 1 / 12 } >
29- < Upvote />
30- </ Box >
31- < Box width = { 11 / 12 } >
32- < Link to = { getTutorialOverviewSlug ( tutorial . fileAbsolutePath ) } >
33- < Heading > { tutorial . frontmatter . tutorialTitle } </ Heading >
34- </ Link >
35- < Text > { tutorial . frontmatter . description } </ Text >
36- </ Box >
37- </ Flex >
38- </ Card >
30+ < Query
31+ query = { gql `
32+ query TutorialListing($id: ID!) {
33+ tutorial(id: $id) {
34+ id
35+ upvotes
36+ numberOfStudents
37+ viewerUserTutorial {
38+ id
39+ upvoted
40+ saved
41+ }
42+ }
43+ }
44+ ` }
45+ variables = { { id : tutorialId } }
46+ >
47+ { ( { data } ) => {
48+ return (
49+ < Card width = { [ 1 ] } p = { 4 } my = { 4 } borderRadius = { 8 } boxShadow = "small" >
50+ < Flex alignItems = "center" justifyContent = "center" >
51+ < Box width = { 1 / 12 } >
52+ < Mutation
53+ mutation = { gql `
54+ mutation UpvoteTutorial($id: ID!) {
55+ upvoteTutorial(tutorialId: $id) {
56+ code
57+ success
58+ userTutorial {
59+ id
60+ upvoted
61+ tutorial {
62+ id
63+ upvotes
64+ }
65+ }
66+ }
67+ }
68+ ` }
69+ variables = { {
70+ id : tutorialId
71+ } }
72+ >
73+ { ( upvote ) => {
74+ return (
75+ < Upvote
76+ onClick = { ( ) => upvote ( ) }
77+ active = { optionalChaining ( ( ) => data . tutorial . viewerUserTutorial . upvoted ) }
78+ count = { optionalChaining ( ( ) => data . tutorial . upvotes ) }
79+ />
80+ )
81+ } }
82+ </ Mutation >
83+ </ Box >
84+ < Box width = { 11 / 12 } >
85+ < Link to = { getTutorialOverviewSlug ( tutorial . fileAbsolutePath ) } >
86+ < Heading > { tutorial . frontmatter . tutorialTitle } </ Heading >
87+ </ Link >
88+ < Text > { tutorial . frontmatter . description } </ Text >
89+ </ Box >
90+ </ Flex >
91+ </ Card >
92+ )
93+ } }
94+ </ Query >
3995 ) ;
4096} ;
4197
0 commit comments