Skip to content

Commit f283191

Browse files
committed
add progress to fullstack courses page
1 parent da06ec1 commit f283191

File tree

8 files changed

+79
-35
lines changed

8 files changed

+79
-35
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import * as React from 'react';
2+
import { Text } from '../shared/base';
3+
import { percent } from '../../utils/helpers';
4+
5+
const ProgressBanner = ({ tutorial }) => {
6+
let currentChapter = tutorial.viewerUserTutorial.currentChapter;
7+
let numberofChapters = tutorial.numberofChapters;
8+
let percentage = currentChapter
9+
? percent(numberofChapters, currentChapter)
10+
: 0;
11+
return (
12+
<div>
13+
{percentage ? <Text>{percentage}%</Text> : <Text> No Progress </Text>}
14+
</div>
15+
);
16+
};
17+
18+
export default ProgressBanner;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Link } from 'gatsby';
55
import { Query } from 'react-apollo';
66
import UpvoteMutation from './UpvoteMutation';
77
import BookmarkMutation from './BookmarkMutation';
8-
import Percentage from '../shared/Percentage';
8+
import ProgressBanner from './ProgressBanner';
99
import { getTutorialbyGatsbyID } from '../../utils/queries';
1010

1111
type TutorialListingProps = {
@@ -47,7 +47,7 @@ const TutorialListing: React.FunctionComponent<TutorialListingProps> = ({
4747
<div>
4848
<UpvoteMutation tutorial={data.getTutorialbyGatsbyID} />
4949
<BookmarkMutation tutorial={data.getTutorialbyGatsbyID} />
50-
<Percentage tutorial={data.getTutorialbyGatsbyID} />
50+
<ProgressBanner tutorial={data.getTutorialbyGatsbyID} />
5151
</div>
5252
)}
5353
</Box>

packages/gatsby-theme/src/components/courses/CourseCard.tsx

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
import * as React from 'react';
22
import { Card, Flex, Image } from '../shared/base';
3+
import { Query } from 'react-apollo';
34
import ProgressBar from '../shared/ProgressBar';
45
import { TutorialButton } from '../shared/buttons';
6+
import { getTutorialbyGatsbyID } from '../../utils/queries';
57
import { getTutorialOverviewSlug } from '../../utils/getTutorialSlug';
8+
import { optionalChaining, percent } from '../../utils/helpers';
69

710
type CourseCardProps = {
811
tutorialTitle: string;
912
fileAbsolutePath: string;
13+
gatsbyID: string;
1014
};
1115

1216
const CourseCard: React.FunctionComponent<CourseCardProps> = ({
17+
gatsbyID,
1318
tutorialTitle,
1419
fileAbsolutePath,
1520
}) => {
21+
let path = getTutorialOverviewSlug(fileAbsolutePath);
1622
return (
1723
<Card m={[1, 1, 1]} p={[2, 2, 2]}>
1824
<Flex flexDirection="column" alignItems="center" justifyContent="center">
@@ -21,10 +27,38 @@ const CourseCard: React.FunctionComponent<CourseCardProps> = ({
2127
src="https://i.ibb.co/TcKwmwR/Icons.png"
2228
/>
2329
<h3>{tutorialTitle}</h3>
24-
<ProgressBar percentage={Math.floor(Math.random() * 100)} width={80} />
25-
<a href={getTutorialOverviewSlug(fileAbsolutePath)}>
26-
<TutorialButton>Start Tutorial</TutorialButton>
27-
</a>
30+
<Query query={getTutorialbyGatsbyID} variables={{ gatsbyID: gatsbyID }}>
31+
{({ data }) => {
32+
let buttonText = 'Continue Tutorial';
33+
if (
34+
optionalChaining(
35+
() =>
36+
data.getTutorialbyGatsbyID.viewerUserTutorial.currentChapter,
37+
)
38+
) {
39+
let percentage = percent(
40+
data.getTutorialbyGatsbyID.numberofChapters,
41+
data.getTutorialbyGatsbyID.viewerUserTutorial.currentChapter,
42+
);
43+
if (percentage === 100) {
44+
buttonText = 'Take Again';
45+
}
46+
return (
47+
<div>
48+
<ProgressBar percentage={percentage} />
49+
<a href={path}>
50+
<TutorialButton>{buttonText}</TutorialButton>
51+
</a>
52+
</div>
53+
);
54+
} else
55+
return (
56+
<a href={path}>
57+
<TutorialButton>Start Tutorial</TutorialButton>
58+
</a>
59+
);
60+
}}
61+
</Query>
2862
</Flex>
2963
</Card>
3064
);

packages/gatsby-theme/src/components/courses/CourseSection.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type Node = {
1919
};
2020

2121
type Frontmatter = {
22+
id: string;
2223
tutorialTitle: string;
2324
description: string;
2425
};
@@ -39,6 +40,7 @@ const CourseSection: React.FunctionComponent<CourseSectionProps> = ({
3940
{data.map(tutorial => (
4041
<Box width={[1, 0.8, 0.4]} key={tutorial.node.id}>
4142
<CourseCard
43+
gatsbyID={tutorial.node.frontmatter.id}
4244
tutorialTitle={tutorial.node.frontmatter.tutorialTitle}
4345
fileAbsolutePath={tutorial.node.fileAbsolutePath}
4446
/>

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

Lines changed: 0 additions & 19 deletions
This file was deleted.

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
import * as React from 'react';
22
import { styled } from '../../styles';
33
import { Text } from './base';
4+
import {percent} from "../../utils/helpers"
45

5-
interface ProgressBarProps extends FillerProps, ContainerProps {}
6+
interface ProgressBarProps extends FillerProps {
7+
numberofChapters?: number;
8+
currentChapter?: number;
9+
}
610

711
const ProgressBar: React.FunctionComponent<ProgressBarProps> = ({
812
percentage,
9-
width,
13+
numberofChapters,
14+
currentChapter,
1015
}) => {
16+
if (!percentage && numberofChapters && currentChapter) {
17+
percentage = percent(currentChapter, numberofChapters)
18+
}
1119
return (
12-
<Container width={width}>
20+
<Container>
1321
<Outside>
1422
<Filler percentage={percentage} />
1523
</Outside>
@@ -20,12 +28,8 @@ const ProgressBar: React.FunctionComponent<ProgressBarProps> = ({
2028
);
2129
};
2230

23-
interface ContainerProps {
24-
width?: number;
25-
}
26-
27-
const Container = styled.div<ContainerProps>`
28-
width: ${props => props.width || '100'}%;
31+
const Container = styled.div`
32+
width: 100%;
2933
`;
3034

3135
const Outside = styled.div`
@@ -38,7 +42,7 @@ const Outside = styled.div`
3842
`;
3943

4044
interface FillerProps {
41-
percentage: number;
45+
percentage?: number;
4246
}
4347

4448
const Filler = styled.div<FillerProps>`

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export const query = graphql`
5656
id
5757
fileAbsolutePath
5858
frontmatter {
59+
id
5960
tutorialTitle
6061
description
6162
}
@@ -73,6 +74,7 @@ export const query = graphql`
7374
id
7475
fileAbsolutePath
7576
frontmatter {
77+
id
7678
tutorialTitle
7779
description
7880
}

packages/gatsby-theme/src/utils/helpers.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ export function optionalChaining(func: Function) {
55
return undefined;
66
}
77
}
8+
9+
export const percent = (numberofChapters, currentChapter) =>
10+
currentChapter ? Math.floor((currentChapter / numberofChapters) * 100) : 0;

0 commit comments

Comments
 (0)