Skip to content

Commit c65ce81

Browse files
authored
Merge pull request #33 from howtographql/add-optional-chaining
add optional chaining to bookmark, current chapter and upvotes
2 parents 3acb477 + 03b11cf commit c65ce81

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

packages/gatsby-theme/src/components/community/BookmarkMutation.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { Mutation } from 'react-apollo';
33
import { loginUser } from '../../utils/auth';
44
import { BookmarkButton } from '../shared/buttons';
55
import { BookmarkTutorial } from '../../utils/queries';
6+
import { optionalChaining } from '../../utils/helpers';
7+
68
import { handleMutationResponse, ApiErrors } from '../../utils/errorHandling';
79

810
const BookmarkMutation = ({ tutorial }) => (
@@ -13,7 +15,9 @@ const BookmarkMutation = ({ tutorial }) => (
1315
}}
1416
>
1517
{bookmark => {
16-
let bookmarked = tutorial.viewerUserTutorial.bookmarked;
18+
let bookmarked = optionalChaining(
19+
() => tutorial.viewerUserTutorial.bookmarked,
20+
);
1721
return (
1822
<BookmarkButton
1923
active={bookmarked}

packages/gatsby-theme/src/components/community/UpvoteMutation.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import { loginUser } from '../../utils/auth';
44
import { handleMutationResponse, ApiErrors } from '../../utils/errorHandling';
55
import { VoteButton } from '../shared/buttons';
66
import { Heading, Flex } from '../shared/base';
7+
import { optionalChaining } from '../../utils/helpers';
78
import { UpvoteTutorial } from '../../utils/queries';
89

910
const UpvoteMutation = ({ tutorial }) => (
1011
<Mutation mutation={UpvoteTutorial} variables={{ id: tutorial.id }}>
1112
{upvote => {
12-
let active = tutorial.viewerUserTutorial.upvoted;
13-
let upvotes = tutorial.upvotes;
13+
let active = optionalChaining(() => tutorial.viewerUserTutorial.upvoted);
14+
let upvotes = optionalChaining(() => tutorial.upvotes);
1415
return (
1516
<Flex
1617
flexDirection="column"

packages/gatsby-theme/src/components/shared/Percentage.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import * as React from 'react';
2+
import { optionalChaining } from '../../utils/helpers';
23
import { Text } from './base';
34

45
const Percentage = ({ tutorial }) => {
5-
let progress = tutorial.viewerUserTutorial.currentChapter;
6+
let progress = optionalChaining(
7+
() => tutorial.viewerUserTutorial.currentChapter,
8+
);
69
let percentage = progress
710
? Math.floor((progress / tutorial.numberofChapters) * 100)
811
: 0;

0 commit comments

Comments
 (0)