Skip to content

Commit e9e639d

Browse files
committed
inital set up for tracking tutorial progress
1 parent 08d7fa3 commit e9e639d

File tree

6 files changed

+229
-41
lines changed

6 files changed

+229
-41
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import * as React from 'react';
2+
import gql from 'graphql-tag';
3+
import { Mutation } from 'react-apollo';
4+
import { loginUser } from '../utils/auth';
5+
import { handleMutationResponse, ApiErrors } from '../utils/errorHandling';
6+
import { Button } from './shared/base';
7+
8+
type ChapterMutationProps = {
9+
gatsbyID: any;
10+
currentChapter: any;
11+
};
12+
13+
const ChapterMutation: React.FunctionComponent<ChapterMutationProps> = ({
14+
gatsbyID,
15+
currentChapter,
16+
}) => (
17+
<Mutation
18+
mutation={gql`
19+
mutation upsertCurrentChapter($gatsbyID: String!, $chapter: Int!) {
20+
upsertCurrentChapter(gatsbyID: $gatsbyID, chapter: $chapter) {
21+
code
22+
success
23+
userTutorial {
24+
id
25+
currentChapter
26+
}
27+
}
28+
}
29+
`}
30+
variables={{
31+
gatsbyID: gatsbyID,
32+
chapter: currentChapter,
33+
}}
34+
>
35+
{currentChapter => {
36+
return (
37+
<Button
38+
onClick={async () => {
39+
const mutationRes = await handleMutationResponse(currentChapter());
40+
if (mutationRes.error) {
41+
if (mutationRes.error === ApiErrors.AUTHORIZATION) {
42+
loginUser();
43+
} else {
44+
console.log(mutationRes.error);
45+
}
46+
}
47+
}}
48+
>
49+
Next Chapter
50+
</Button>
51+
);
52+
}}
53+
</Mutation>
54+
);
55+
56+
export default ChapterMutation;

packages/gatsby-theme/src/components/templates/Tutorial.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { TutorialMdxQuery } from '../../graphqlTypes';
88
import { HideOnTablet, ShowOnTablet } from '../../utils/responsive';
99
import { Flex, Box } from '../shared/base';
1010
import { optionalChaining } from '../../utils/helpers';
11-
import { Button } from '../shared/base';
11+
import ChapterMutation from '../ChapterMutation';
1212

1313
type TutorialLayoutProps = { data: TutorialMdxQuery } & RouterProps;
1414

@@ -20,6 +20,10 @@ const TutorialLayout: React.FunctionComponent<TutorialLayoutProps> = ({
2020
return null;
2121
}
2222
const { pageTitle } = data!.mdx!.frontmatter!;
23+
const gatsbyID = optionalChaining(
24+
() => data!.tutorialTitle!.frontmatter!.id!,
25+
);
26+
2327
const tutorialTitle = optionalChaining(
2428
() => data!.tutorialTitle!.frontmatter!.tutorialTitle!,
2529
);
@@ -28,8 +32,7 @@ const TutorialLayout: React.FunctionComponent<TutorialLayoutProps> = ({
2832
data!.pageTitles!.edges!.map(a => a.node!.frontmatter!.pageTitle!),
2933
) || [];
3034
const { location } = props;
31-
32-
const currentChapterNumber = chapters.indexOf(pageTitle) + 1;
35+
const currentChapter = chapters.indexOf(pageTitle) + 1;
3336

3437
return (
3538
<Layout location={location}>
@@ -57,10 +60,7 @@ const TutorialLayout: React.FunctionComponent<TutorialLayoutProps> = ({
5760
<ShowOnTablet>
5861
<MDXRenderer>{data!.mdx!.code!.body}</MDXRenderer>
5962
</ShowOnTablet>
60-
<Button onClick={() => console.log(currentChapterNumber)}>
61-
{' '}
62-
Next Chapter
63-
</Button>
63+
<ChapterMutation gatsbyID={gatsbyID} currentChapter={currentChapter} />
6464
</Layout>
6565
);
6666
};
@@ -100,6 +100,7 @@ export const pageQuery = graphql`
100100
fileAbsolutePath: { regex: $folderRegex }
101101
) {
102102
frontmatter {
103+
id
103104
tutorialTitle
104105
}
105106
}

packages/gatsby-theme/src/pages/profile.tsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ type Tutorial = {
4949
};
5050

5151
const ProfilePage: React.FunctionComponent<ProfileProps> = ({ user }) => {
52-
console.log(user);
5352
return (
5453
<Layout>
5554
<Flex flexDirection="column">
@@ -89,6 +88,17 @@ const ProfilePage: React.FunctionComponent<ProfileProps> = ({ user }) => {
8988
),
9089
)}
9190
</ul>
91+
<Heading> In Progress Tutorials </Heading>
92+
<ul>
93+
{user.progress.map(
94+
a =>
95+
a.tutorial && (
96+
<li key={a.tutorial.id}>
97+
<span>{a.tutorial.name}</span>
98+
</li>
99+
),
100+
)}
101+
</ul>
92102
</Layout>
93103
);
94104
};
@@ -115,6 +125,12 @@ const PROFILE_QUERY = gql`
115125
name
116126
}
117127
}
128+
progress: userTutorials(where: { currentChapter_not: 0 }) {
129+
tutorial {
130+
id
131+
name
132+
}
133+
}
118134
}
119135
}
120136
}

packages/server/.yoga/nexus.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ export interface NexusGenFieldTypes {
344344
Mutation: { // field return type
345345
authenticate: NexusGenRootTypes['AuthenticateUserPayload'] | null; // AuthenticateUserPayload
346346
bookmarkTutorial: NexusGenRootTypes['UserTutorialPayload']; // UserTutorialPayload!
347+
upsertCurrentChapter: NexusGenRootTypes['UserTutorialPayload']; // UserTutorialPayload!
347348
upsertTutorial: NexusGenRootTypes['Tutorial']; // Tutorial!
348349
upvoteTutorial: NexusGenRootTypes['UserTutorialPayload']; // UserTutorialPayload!
349350
}
@@ -415,6 +416,10 @@ export interface NexusGenArgTypes {
415416
bookmarkTutorial: { // args
416417
tutorialId: string; // ID!
417418
}
419+
upsertCurrentChapter: { // args
420+
chapter: number; // Int!
421+
gatsbyID: string; // String!
422+
}
418423
upsertTutorial: { // args
419424
gatsbyID: string; // String!
420425
name: string; // String!

0 commit comments

Comments
 (0)