Skip to content

Commit c496a46

Browse files
implemented frontend of upvote
1 parent f91b970 commit c496a46

File tree

11 files changed

+176
-635
lines changed

11 files changed

+176
-635
lines changed

packages/gatsby-theme/src/components/TutorialListing.tsx

Lines changed: 69 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import { Heading, Text, Card, Flex, Box } from './shared/base';
33
import { getTutorialOverviewSlug } from '../utils/getTutorialSlug';
44
import Upvote from './Upvote';
55
import { Link } from 'gatsby';
6+
import { Query, Mutation } from 'react-apollo';
7+
import gql from 'graphql-tag';
8+
import { optionalChaining } from '../utils/helpers';
69

710
type TutorialListingProps = {
811
tutorial: Tutorial;
@@ -22,20 +25,73 @@ type FrontMatter = {
2225
const 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

packages/gatsby-theme/src/components/Upvote.tsx

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,32 @@ import WithCurrentUser from '../utils/auth/WithCurrentUser';
99
// Create a way to store which user has upvoted the tutorial so that they can only
1010
// upvote it once
1111

12-
const Upvote = () => {
13-
return (
14-
<WithCurrentUser>
15-
{({ user }) => {
16-
if (user) {
17-
return <UpvoteData event={() => console.log('upvoted!')} />;
18-
}
19-
return <UpvoteData event={() => loginUser()} />;
20-
}}
21-
</WithCurrentUser>
22-
);
23-
};
12+
// const Upvote = ({active}) => {
13+
// return (
14+
// <WithCurrentUser>
15+
// {({ user }) => {
16+
// if (user) {
17+
// return <UpvoteData active={active} event={() => console.log('upvoted!')} />;
18+
// }
19+
// return <UpvoteData active={active} event={() => loginUser()} />;
20+
// }}
21+
// </WithCurrentUser>
22+
// );
23+
// };
2424

2525
type UpvoteDataProps = {
26-
event: string;
26+
onClick: () => any;
27+
count: number;
28+
active: boolean;
2729
};
2830

2931
// place holder until we have a backend that stores the number of upvotes
3032
// and can keep track of which tutorials a user upvotes
31-
const UpvoteData: React.FunctionComponent<UpvoteDataProps> = ({ event }) => {
33+
const Upvote: React.FunctionComponent<UpvoteDataProps> = ({ onClick: event, active, count = "..." }) => {
3234
return (
3335
<Flex flexDirection="column" alignItems="center" justifyContent="center">
34-
<VoteButton onClick={event} />
35-
<Heading>{Math.floor(Math.random() * 100)}</Heading>
36+
<VoteButton active={active} onClick={event} />
37+
<Heading>{count}</Heading>
3638
</Flex>
3739
);
3840
};

0 commit comments

Comments
 (0)