Skip to content

Commit 7591244

Browse files
authored
Merge pull request #601 from topcoder-platform/dedupe-api-call-for-fcc-cert-completion
Deduplicate api call for FCC Course completion -> dev
2 parents 0208323 + f00bf5a commit 7591244

File tree

2 files changed

+73
-41
lines changed

2 files changed

+73
-41
lines changed

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

+6-41
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ import {
3939
useGetUserCertificationProgress,
4040
useLearnBreadcrumb,
4141
userCertificationProgressAutocompleteCourse,
42-
userCertificationProgressCompleteCourseAsync,
4342
UserCertificationProgressProviderData,
4443
userCertificationProgressStartAsync,
4544
UserCertificationProgressStatus,
@@ -54,6 +53,7 @@ import {
5453
} from '../learn.routes'
5554
import { LearnConfig } from '../learn-config'
5655

56+
import { useCheckAndMarkCourseCompleted } from './hooks/use-mark-course-completed'
5757
import { FccFrame } from './fcc-frame'
5858
import { FccSidebar } from './fcc-sidebar'
5959
import { TitleNav } from './title-nav'
@@ -419,48 +419,13 @@ const FreeCodeCamp: FC<{}> = () => {
419419
location.state,
420420
])
421421

422-
useEffect(() => {
423-
424-
// if we don't yet have the user's handle,
425-
// or if the cert isn't complete,
426-
// or the cert isn't in progress,
427-
// there's nothing to do
428-
if (
429-
!profile?.handle
430-
|| certificateProgress?.certificationProgressPercentage !== 100
431-
|| certificateProgress?.status !== UserCertificationProgressStatus.inProgress
432-
) {
433-
return
434-
}
435-
436-
// it's safe to complete the course
437-
userCertificationProgressCompleteCourseAsync(
438-
certificateProgress.id,
439-
certificationParam,
440-
profile.handle,
441-
providerParam,
442-
)
443-
.then(setCertificateProgress)
444-
.then(() => {
445-
const completedPath: string = getCertificationCompletedPath(
446-
providerParam,
447-
certificationParam,
448-
)
449-
navigate(completedPath, {
450-
state: {
451-
tcaCertInfo: location.state?.tcaCertInfo,
452-
},
453-
})
454-
})
455-
// eslint-disable-next-line react-hooks/exhaustive-deps
456-
}, [
422+
useCheckAndMarkCourseCompleted(
423+
!!profile?.userId,
424+
providerParam,
457425
certificateProgress,
458-
certificationParam,
459426
profile?.handle,
460-
profile?.userId,
461-
providerParam,
462-
location.state,
463-
])
427+
setCertificateProgress,
428+
)
464429

465430
useEffect(() => {
466431
if (courseDataReady && courseData) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { noop } from 'lodash'
2+
import { MutableRefObject, useEffect, useRef } from 'react'
3+
import { NavigateFunction, useLocation, useNavigate } from 'react-router-dom'
4+
5+
import {
6+
LearnUserCertificationProgress,
7+
userCertificationProgressCompleteCourseAsync,
8+
UserCertificationProgressStatus,
9+
} from '../../learn-lib'
10+
import { getCertificationCompletedPath } from '../../learn.routes'
11+
12+
export const useCheckAndMarkCourseCompleted: (
13+
isLoggedIn: boolean,
14+
providerName: string,
15+
certificateProgress?: LearnUserCertificationProgress,
16+
userHandle?: string,
17+
setCertificateProgress?: (progess: LearnUserCertificationProgress) => void
18+
) => void = (isLoggedIn, providerName, certificateProgress, userHandle, setCertificateProgress = noop) => {
19+
const navigate: NavigateFunction = useNavigate()
20+
const location: any = useLocation()
21+
const isUpdating: MutableRefObject<boolean> = useRef(false)
22+
23+
useEffect(() => {
24+
// if we don't yet have the user's handle,
25+
// or if the cert isn't complete,
26+
// or the cert isn't in progress,
27+
// there's nothing to do
28+
if (
29+
isUpdating.current
30+
|| !isLoggedIn
31+
|| certificateProgress?.certificationProgressPercentage !== 100
32+
|| certificateProgress?.status !== UserCertificationProgressStatus.inProgress
33+
) {
34+
return
35+
}
36+
37+
// Prevent further calls to the backend until this one is completed
38+
isUpdating.current = true
39+
// it's safe to complete the course
40+
userCertificationProgressCompleteCourseAsync(
41+
certificateProgress?.id,
42+
certificateProgress.certification,
43+
userHandle as string,
44+
providerName,
45+
)
46+
.then(setCertificateProgress)
47+
.then(() => {
48+
const completedPath: string = getCertificationCompletedPath(
49+
providerName,
50+
certificateProgress.certification,
51+
)
52+
isUpdating.current = false
53+
navigate(completedPath, {
54+
state: {
55+
tcaCertInfo: location.state?.tcaCertInfo,
56+
},
57+
})
58+
})
59+
// eslint-disable-next-line react-hooks/exhaustive-deps
60+
}, [
61+
certificateProgress,
62+
isLoggedIn,
63+
userHandle,
64+
providerName,
65+
location.state,
66+
])
67+
}

0 commit comments

Comments
 (0)