Skip to content

Commit 4926673

Browse files
authored
Merge pull request #279 from topcoder-platform/TCA-376_refetch-course-progress
TCA-376 - refetch course progress data when user opens sidebar nav
2 parents e9531e5 + f944b9a commit 4926673

File tree

4 files changed

+43
-23
lines changed

4 files changed

+43
-23
lines changed

src-ts/tools/learn/free-code-camp/FreeCodeCamp.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ const FreeCodeCamp: FC<{}> = () => {
6262
certificationProgress: certificateProgress,
6363
setCertificateProgress,
6464
ready: progressReady,
65+
refetch: refetchProgress,
6566
}: UserCertificationProgressProviderData = useUserCertificationProgress(
6667
profile?.userId,
6768
routeParams.provider,
@@ -362,7 +363,10 @@ const FreeCodeCamp: FC<{}> = () => {
362363
{lesson && (
363364
<div className={styles['main-wrap']}>
364365
<div className={styles['course-outline-pane']}>
365-
<CollapsiblePane title='Course Outline'>
366+
<CollapsiblePane
367+
title='Course Outline'
368+
onToggle={(isOpen) => isOpen && refetchProgress()}
369+
>
366370
<div className={styles['course-outline-wrap']}>
367371
<div className={styles['course-outline-title']}>
368372
{courseData?.title}

src-ts/tools/learn/learn-lib/collapsible-pane/CollapsiblePane.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import classNames from 'classnames'
2+
import { noop } from 'lodash'
23
import { Dispatch, FC, ReactNode, SetStateAction, useCallback, useState } from 'react'
34

45
import { IconSolid } from '../../../../lib'
@@ -7,16 +8,19 @@ import styles from './CollapsiblePane.module.scss'
78

89
interface CollapsiblePaneProps {
910
children: ReactNode
11+
onToggle?: (isOpen: boolean) => void
1012
position?: 'to-left'|'to-right'
1113
title: string
1214
}
1315

1416
const CollapsiblePane: FC<CollapsiblePaneProps> = (props: CollapsiblePaneProps) => {
17+
const {onToggle = noop}: CollapsiblePaneProps = props
1518
const [isOpen, setIsOpen]: [boolean, Dispatch<SetStateAction<boolean>>] = useState<boolean>(false)
1619

1720
const toggle: () => void = useCallback(() => {
18-
setIsOpen(open => !open)
19-
}, [])
21+
setIsOpen(!isOpen)
22+
onToggle(!isOpen)
23+
}, [isOpen, onToggle])
2024

2125
return (
2226
<div className={

src-ts/tools/learn/learn-lib/user-certifications-provider/user-certification-progress-provider-data.model.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ export interface UserCertificationProgressProviderData {
44
certificationProgress?: LearnUserCertificationProgress
55
loading: boolean
66
ready: boolean
7+
refetch: () => void,
78
setCertificateProgress: (progess: LearnUserCertificationProgress) => void,
89
}
Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,60 @@
1-
import { Dispatch, SetStateAction, useEffect, useState } from 'react'
1+
import { Dispatch, MutableRefObject, SetStateAction, useCallback, useEffect, useRef, useState } from 'react'
22

33
import { UserCertificationProgressProviderData } from './user-certification-progress-provider-data.model'
44
import { LearnUserCertificationProgress, userCertificationProgressGetAsync } from './user-certifications-functions'
55

66
export function useUserCertificationProgress(userId?: number, provider?: string, certification?: string):
77
UserCertificationProgressProviderData {
8+
const callCounter: MutableRefObject<number> = useRef(0)
89

910
function setCertificateProgress(progress: LearnUserCertificationProgress): void {
1011
setState((prevState) => ({ ...prevState, certificationProgress: progress }))
12+
callCounter.current++
1113
}
1214

15+
const fetchProgress: () => void = useCallback(() => {
16+
if (!userId) {
17+
return
18+
}
19+
20+
const currentCallCounter: number = ++callCounter.current
21+
22+
userCertificationProgressGetAsync(userId, provider, certification)
23+
.then((myCertifications) => {
24+
// if another call to fetchProgress or to setCertificateProgress
25+
// was made before we got the api response
26+
// return, and do not update state
27+
if (callCounter.current !== currentCallCounter) {
28+
return
29+
}
30+
31+
setState((prevState) => ({
32+
...prevState,
33+
certificationProgress: myCertifications.find(c => c.certification === certification),
34+
loading: false,
35+
ready: true,
36+
}))
37+
})
38+
}, [certification, provider, userId])
39+
1340
const [state, setState]:
1441
[UserCertificationProgressProviderData, Dispatch<SetStateAction<UserCertificationProgressProviderData>>]
1542
= useState<UserCertificationProgressProviderData>({
1643
certificationProgress: undefined,
1744
loading: false,
1845
ready: false,
46+
refetch: fetchProgress,
1947
setCertificateProgress,
2048
})
2149

2250
useEffect(() => {
23-
2451
setState((prevState) => ({
2552
...prevState,
2653
loading: true,
2754
}))
2855

29-
if (!userId) {
30-
return
31-
}
32-
33-
userCertificationProgressGetAsync(userId, provider, certification)
34-
.then((myCertifications) => {
35-
setState((prevState) => ({
36-
...prevState,
37-
certificationProgress: myCertifications.find(c => c.certification === certification),
38-
loading: false,
39-
ready: true,
40-
}))
41-
})
42-
}, [
43-
certification,
44-
provider,
45-
userId,
46-
])
56+
fetchProgress()
57+
}, [certification, fetchProgress])
4758

4859
return state
4960
}

0 commit comments

Comments
 (0)