Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit 3f4b7ed

Browse files
committed
display post completed status for lessons
1 parent 8e61df4 commit 3f4b7ed

File tree

9 files changed

+84
-31
lines changed

9 files changed

+84
-31
lines changed

src/components/LessonList.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { List } from '@material-ui/core';
2-
import { Post } from '@zoonk/models';
2+
import { ChapterProgress, Post } from '@zoonk/models';
3+
import { getLessonStatus } from '@zoonk/utils';
34
import LessonListItem from './LessonListItem';
45

56
interface LessonListProps {
7+
category: keyof ChapterProgress.Response;
68
items: Post.Summary[];
9+
progress?: ChapterProgress.Response;
710
}
811

9-
/**
10-
* Display a list of lessons.
11-
*/
12-
const LessonList = ({ items }: LessonListProps) => {
12+
const LessonList = ({ category, items, progress }: LessonListProps) => {
1313
return (
1414
<List disablePadding>
1515
{items.map((item, index) => (
@@ -18,6 +18,7 @@ const LessonList = ({ items }: LessonListProps) => {
1818
divider={index !== items.length - 1}
1919
index={index}
2020
item={item}
21+
status={getLessonStatus(category, item.id, progress)}
2122
/>
2223
))}
2324
</List>

src/components/LessonListItem.tsx

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,35 @@ import {
44
ListItem,
55
ListItemAvatar,
66
ListItemText,
7+
makeStyles,
78
} from '@material-ui/core';
89
import NextLink from 'next/link';
910
import { useRouter } from 'next/router';
1011
import { Post } from '@zoonk/models';
11-
import { theme } from '@zoonk/utils';
1212

1313
interface LessonListItemProps {
1414
divider?: boolean;
1515
index: number;
1616
item: Post.Summary;
17+
status: 'completed' | 'notStarted';
1718
}
1819

19-
/**
20-
* Display a single lesson as a list item.
21-
*/
22-
const LessonListItem = ({ divider, index, item }: LessonListItemProps) => {
20+
const useStyles = makeStyles((theme) => ({
21+
notStarted: {
22+
backgroundColor: 'white',
23+
color: theme.palette.primary.main,
24+
border: `1px solid ${theme.palette.primary.main}`,
25+
},
26+
completed: { backgroundColor: theme.palette.success.main },
27+
}));
28+
29+
const LessonListItem = ({
30+
divider,
31+
index,
32+
item,
33+
status,
34+
}: LessonListItemProps) => {
35+
const classes = useStyles();
2336
const { query } = useRouter();
2437
const { description, id, title } = item;
2538
const lessonId = String(query.id);
@@ -44,7 +57,7 @@ const LessonListItem = ({ divider, index, item }: LessonListItemProps) => {
4457
selected={lessonId === id}
4558
>
4659
<ListItemAvatar>
47-
<Avatar style={{ backgroundColor: theme.palette.primary.main }}>
60+
<Avatar variant="rounded" className={classes[status]}>
4861
{index + 1}
4962
</Avatar>
5063
</ListItemAvatar>

src/components/LessonsCard.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Card, CardContent } from '@material-ui/core';
2-
import { Post } from '@zoonk/models';
2+
import { ChapterProgress, Post } from '@zoonk/models';
33
import LessonList from './LessonList';
44
import LessonsHeader from './LessonsHeader';
55
import NoLessons from './NoLessons';
@@ -8,16 +8,15 @@ interface LessonsCardProps {
88
category: 'examples' | 'lessons';
99
chapterId: string;
1010
lessons: Post.Summary[];
11+
progress?: ChapterProgress.Response;
1112
topicId: string;
1213
}
1314

14-
/**
15-
* Cards for display a list of lessons.
16-
*/
1715
const LessonsCard = ({
1816
category,
1917
chapterId,
2018
lessons,
19+
progress,
2120
topicId,
2221
}: LessonsCardProps) => {
2322
return (
@@ -31,7 +30,7 @@ const LessonsCard = ({
3130
topicId={topicId}
3231
/>
3332
)}
34-
<LessonList items={lessons} />
33+
<LessonList category={category} items={lessons} progress={progress} />
3534
</CardContent>
3635
</Card>
3736
);

src/components/LessonsDrawer.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { Fragment } from 'react';
22
import { Button, Grid, makeStyles } from '@material-ui/core';
3-
import { Post } from '@zoonk/models';
3+
import { ChapterProgress, Post } from '@zoonk/models';
4+
import { getLessonStatus } from '@zoonk/utils';
45
import LessonListItem from './LessonListItem';
56
import useTranslation from './useTranslation';
67

78
interface LessonDrawerProps {
9+
category: keyof ChapterProgress.Response;
810
lessons: Post.Summary[];
11+
progress?: ChapterProgress.Response;
912
onReturn: () => void;
1013
}
1114

@@ -16,7 +19,12 @@ const useStyles = makeStyles((theme) => ({
1619
},
1720
}));
1821

19-
const LessonsDrawer = ({ lessons, onReturn }: LessonDrawerProps) => {
22+
const LessonsDrawer = ({
23+
category,
24+
lessons,
25+
progress,
26+
onReturn,
27+
}: LessonDrawerProps) => {
2028
const translate = useTranslation();
2129
const classes = useStyles();
2230

@@ -35,7 +43,11 @@ const LessonsDrawer = ({ lessons, onReturn }: LessonDrawerProps) => {
3543
<Grid container spacing={2} className={classes.grid}>
3644
{lessons.map((lesson, index) => (
3745
<Grid item key={lesson.id} xs={12} sm={6}>
38-
<LessonListItem item={lesson} index={index} />
46+
<LessonListItem
47+
item={lesson}
48+
index={index}
49+
status={getLessonStatus(category, lesson.id, progress)}
50+
/>
3951
</Grid>
4052
))}
4153
</Grid>

src/components/PostBar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const PostBar = () => {
1313
<Fragment>
1414
<BottomBar>
1515
<PostBarActions id={id} likes={likes} />
16-
{category === 'lessons' && chapterId && (
16+
{(category === 'lessons' || category === 'examples') && chapterId && (
1717
<PostBarLessons
1818
category={category}
1919
postId={id}

src/components/PostBarLessons.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ import { Fragment, useEffect, useState } from 'react';
22
import Router from 'next/router';
33
import { Button, Drawer, Hidden, makeStyles } from '@material-ui/core';
44
import { Apps } from '@material-ui/icons';
5-
import { Post } from '@zoonk/models';
5+
import { ChapterProgress, Post } from '@zoonk/models';
66
import { getChapterLive } from '@zoonk/services';
77
import LessonsDrawer from './LessonsDrawer';
88
import NextLesson from './NextLesson';
99
import PreviousLesson from './PreviousLesson';
10+
import useChapterProgress from './useChapterProgress';
1011
import useTranslation from './useTranslation';
1112

1213
interface PostBarLessonsProps {
13-
category: Post.Category;
14+
category: keyof ChapterProgress.Response;
1415
chapterId: string;
1516
postId: string;
1617
topicId: string;
@@ -41,6 +42,7 @@ const PostBarLessons = ({
4142
topicId,
4243
}: PostBarLessonsProps) => {
4344
const classes = useStyles();
45+
const { progress } = useChapterProgress({ chapterId });
4446
const [drawer, setDrawer] = useState<boolean>(false);
4547
const [lessons, setLessons] = useState<Post.Summary[]>([]);
4648
const translate = useTranslation();
@@ -85,7 +87,12 @@ const PostBarLessons = ({
8587
</div>
8688

8789
<Drawer open={drawer} onClose={() => setDrawer(false)} anchor="bottom">
88-
<LessonsDrawer lessons={lessons} onReturn={() => setDrawer(false)} />
90+
<LessonsDrawer
91+
category={category}
92+
lessons={lessons}
93+
progress={progress}
94+
onReturn={() => setDrawer(false)}
95+
/>
8996
</Drawer>
9097
</Fragment>
9198
);

src/components/useChapterProgress.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,29 @@ import { getChapterProgress } from '@zoonk/services';
44
import { getChapterCompleted } from '@zoonk/utils';
55
import useAuth from './useAuth';
66

7-
interface useChapterProgressProps {
7+
interface ProgressResponse {
88
completed: number;
99
progress: ChapterProgress.Response | undefined;
1010
}
1111

12-
const useChapterProgress = (
13-
chapter: Chapter.Get | null,
14-
): useChapterProgressProps => {
12+
interface ProgressProps {
13+
chapter?: Chapter.Get | null;
14+
chapterId?: string;
15+
}
16+
17+
const useChapterProgress = ({
18+
chapter,
19+
chapterId,
20+
}: ProgressProps): ProgressResponse => {
1521
const { user } = useAuth();
1622
const [progress, setProgress] = useState<ChapterProgress.Response>();
1723
const [completed, setCompleted] = useState<number>(0);
1824

1925
useEffect(() => {
20-
if (chapter?.id && user) {
21-
getChapterProgress(chapter?.id, user.uid).then(setProgress);
26+
if (chapterId && user) {
27+
getChapterProgress(chapterId, user.uid).then(setProgress);
2228
}
23-
}, [chapter, user]);
29+
}, [chapterId, user]);
2430

2531
useEffect(() => {
2632
if (chapter && progress) {

src/pages/chapters/[id]/index.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ interface ChapterProps {
1515
}
1616

1717
const ChapterPage: NextPage<ChapterProps> = ({ data }) => {
18-
const { completed } = useChapterProgress(data);
18+
const { completed, progress } = useChapterProgress({
19+
chapter: data,
20+
chapterId: data?.id,
21+
});
1922

2023
if (!data) return <Error statusCode={404} />;
2124

@@ -49,6 +52,7 @@ const ChapterPage: NextPage<ChapterProps> = ({ data }) => {
4952
topicId={topics[0]}
5053
lessons={lessonData}
5154
category="lessons"
55+
progress={progress}
5256
/>
5357
</Grid>
5458
<Grid item xs={12} sm={6}>
@@ -57,6 +61,7 @@ const ChapterPage: NextPage<ChapterProps> = ({ data }) => {
5761
topicId={topics[0]}
5862
lessons={exampleData}
5963
category="examples"
64+
progress={progress}
6065
/>
6166
</Grid>
6267
</Grid>

src/utils/progress.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,13 @@ export const getChapterCompleted = (
3131

3232
return (userPosts / chapterPosts) * 100;
3333
};
34+
35+
export const getLessonStatus = (
36+
category: keyof ChapterProgress.Response,
37+
itemId: string,
38+
progress?: ChapterProgress.Response,
39+
): 'completed' | 'notStarted' => {
40+
if (!progress) return 'notStarted';
41+
if (progress[category]?.includes(itemId)) return 'completed';
42+
return 'notStarted';
43+
};

0 commit comments

Comments
 (0)