Skip to content

PROD-2549 Add Course State to Course Cards -> dev #210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src-ts/tools/learn/Learn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { FC, useContext } from 'react'
import { Outlet, Routes } from 'react-router-dom'

import {
ContentLayout,
routeContext,
RouteContextData,
} from '../../lib'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ const CourseCompletedPage: FC<{}> = () => {
certificateProgress: progress,
ready: progressReady,
}: MyCertificationProgressProviderData = useMyCertificationProgress(
profile?.userId,
routeParams.provider,
routeParams.certification
)
Expand Down
1 change: 0 additions & 1 deletion src-ts/tools/learn/course-details/CourseDetailsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ const CourseDetailsPage: FC<{}> = () => {
certificateProgress: progress,
ready: progressReady,
}: MyCertificationProgressProviderData = useMyCertificationProgress(
profile?.userId,
routeParams.provider,
routeParams.certification,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@ import {
LearnLesson,
LearnModule,
LearnMyCertificationProgress,
myCertificationProgressStart,
MyCertificationProgressStatus,
startMyCertificationsProgressAsync,
UpdateMyCertificateProgressActions,
updateMyCertificationsProgressAsync
myCertificationProgressUpdate,
MyCertificationUpdateProgressActions
} from '../../learn-lib'
import { authenticateAndStartCourseRoute, getCertificatePath, getFccLessonPath, LEARN_PATHS } from '../../learn.routes'
import {
authenticateAndStartCourseRoute,
getCertificatePath,
getLessonPathFromCurrentLesson,
LEARN_PATHS,
} from '../../learn.routes'

import styles from './CourseCurriculum.module.scss'
import { CurriculumSummary } from './curriculum-summary'
Expand All @@ -29,14 +34,15 @@ interface CourseCurriculumProps {
}

const CourseCurriculum: FC<CourseCurriculumProps> = (props: CourseCurriculumProps) => {

const navigate: NavigateFunction = useNavigate()
const [searchParams]: any = useSearchParams()

const isLoggedIn: boolean = !!props.profile

const [isTcAcademyPolicyModal, setIsTcAcademyPolicyModal]: [boolean, Dispatch<SetStateAction<boolean>>] = useState<boolean>(false)

const status: string = props.progress?.status ?? 'init'
const status: string = props.progress?.status ?? MyCertificationProgressStatus.inititialized
const completedPercentage: number = (props.progress?.courseProgressPercentage ?? 0) / 100
const inProgress: boolean = status === MyCertificationProgressStatus.inProgress || !!props.progress?.currentLesson
const isCompleted: boolean = status === MyCertificationProgressStatus.completed
Expand All @@ -46,19 +52,25 @@ const CourseCurriculum: FC<CourseCurriculumProps> = (props: CourseCurriculumProp
* otherwise redirect to first module > first lesson
*/
const handleStartCourse: () => void = useCallback(() => {
const current: Array<string> = (props.progress?.currentLesson ?? '').split('/')

const course: LearnCourse = props.course
const module: LearnModule = course.modules[0]
const lesson: LearnLesson = module.lessons[0]

const lessonPath: string = getFccLessonPath(
const lessonPath: string = getLessonPathFromCurrentLesson(
course.provider,
course.certification,
current[0] || module.meta.dashedName,
current[1] || lesson.dashedName,
props.progress?.currentLesson,
module.meta.dashedName,
lesson.dashedName,
)
navigate(lessonPath)
}, [props.course, props.progress, navigate])
}, [
getLessonPathFromCurrentLesson,
navigate,
props.course,
props.progress,
])

/**
* Handle user click on start course/resume/login button
Expand Down Expand Up @@ -97,7 +109,7 @@ const CourseCurriculum: FC<CourseCurriculumProps> = (props: CourseCurriculumProp
}

if (!props.progress?.id) {
await startMyCertificationsProgressAsync(
await myCertificationProgressStart(
props.profile.userId,
props.course.certificationId,
props.course.id,
Expand All @@ -107,9 +119,9 @@ const CourseCurriculum: FC<CourseCurriculumProps> = (props: CourseCurriculumProp
}
)
} else {
await updateMyCertificationsProgressAsync(
await myCertificationProgressUpdate(
props.progress.id,
UpdateMyCertificateProgressActions.acceptHonestyPolicy,
MyCertificationUpdateProgressActions.acceptHonestyPolicy,
{}
)
}
Expand All @@ -134,9 +146,9 @@ const CourseCurriculum: FC<CourseCurriculumProps> = (props: CourseCurriculumProp
* proceed as if the user just clicked "Start course" button
*/
useEffect(() => {
if (props.progressReady && isLoggedIn && searchParams.get('start-course') !== null) {
handleStartCourseClick()
}
if (props.progressReady && isLoggedIn && searchParams.get('start-course') !== null) {
handleStartCourseClick()
}
}, [handleStartCourseClick, isLoggedIn, props.progressReady, searchParams])

return (
Expand Down
112 changes: 65 additions & 47 deletions src-ts/tools/learn/free-code-camp/FreeCodeCamp.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import { Dispatch, FC, SetStateAction, useCallback, useContext, useEffect, useLayoutEffect, useMemo, useState } from 'react'
import {
Dispatch,
FC,
SetStateAction,
useCallback,
useContext,
useEffect,
useLayoutEffect,
useMemo,
useState,
} from 'react'
import { NavigateFunction, Params, useNavigate, useParams } from 'react-router-dom'

import {
Expand All @@ -16,15 +26,15 @@ import {
LearnModule,
LessonProviderData,
MyCertificationProgressProviderData,
myCertificationProgressStart,
MyCertificationProgressStatus,
startMyCertificationsProgressAsync,
UpdateMyCertificateProgressActions,
updateMyCertificationsProgressAsync,
myCertificationProgressUpdate,
MyCertificationUpdateProgressActions,
useCoursesProvider,
useLessonProvider,
useMyCertificationProgress,
} from '../learn-lib'
import { getCertificationCompletedPath, getCoursePath, getFccLessonPath } from '../learn.routes'
import { getCertificationCompletedPath, getCoursePath, getLessonPathFromModule } from '../learn.routes'

import { FccFrame } from './fcc-frame'
import styles from './FreeCodeCamp.module.scss'
Expand All @@ -49,7 +59,7 @@ const FreeCodeCamp: FC<{}> = () => {
certificateProgress,
setCertificateProgress,
ready: progressReady,
}: MyCertificationProgressProviderData = useMyCertificationProgress(profile?.userId, routeParams.provider, certificationParam)
}: MyCertificationProgressProviderData = useMyCertificationProgress(routeParams.provider, certificationParam)

const {
course: courseData,
Expand All @@ -71,27 +81,27 @@ const FreeCodeCamp: FC<{}> = () => {
{ url: '/learn/fcc', name: lesson?.module.title ?? '' },
], [providerParam, lesson])

const currentModuleData: LearnModule|undefined = useMemo(() => {
const currentModuleData: LearnModule | undefined = useMemo(() => {
return courseData?.modules.find(d => d.key === moduleParam)
}, [courseData, moduleParam])

const currentStepIndex: number = useMemo(() => {
if (!currentModuleData) {
return 0
}
if (!currentModuleData) {
return 0
}

const lessonIndex: number = currentModuleData.lessons.findIndex(l => l.dashedName === lessonParam)
return lessonIndex + 1
const lessonIndex: number = currentModuleData.lessons.findIndex(l => l.dashedName === lessonParam)
return lessonIndex + 1
}, [currentModuleData, lessonParam])

const handleNavigate: (direction: number) => void = useCallback((direction = 1) => {

const nextStep: LearnLesson|undefined = currentModuleData?.lessons[(currentStepIndex - 1) + direction]
const nextStep: LearnLesson | undefined = currentModuleData?.lessons[(currentStepIndex - 1) + direction]
if (!nextStep) {
return
}

const lessonPath: string = getFccLessonPath(
const lessonPath: string = getLessonPathFromModule(
providerParam,
certificationParam,
moduleParam,
Expand All @@ -108,12 +118,19 @@ const FreeCodeCamp: FC<{}> = () => {
])

function updatePath(lessonPath: string, modulePath: string, coursePath: string): void {
if (coursePath !== certificationParam) { setCourseParam(coursePath) }
if (modulePath !== moduleParam) { setModuleParam(modulePath) }
if (lessonPath !== lessonParam) { setLessonParam(lessonPath) }

if (coursePath !== certificationParam) {
setCourseParam(coursePath)
}
if (modulePath !== moduleParam) {
setModuleParam(modulePath)
}
if (lessonPath !== lessonParam) {
setLessonParam(lessonPath)
}

if (lessonPath !== lessonParam || modulePath !== moduleParam || coursePath !== certificationParam) {
const nextLessonPath: string = getFccLessonPath(
const nextLessonPath: string = getLessonPathFromModule(
providerParam,
coursePath,
modulePath,
Expand All @@ -124,10 +141,11 @@ const FreeCodeCamp: FC<{}> = () => {
}

function handleFccLessonReady(lessonPath: string): void {

const [nLessonPath, modulePath, coursePath]: Array<string> = lessonPath.replace(/\/$/, '').split('/').reverse()
updatePath(nLessonPath, modulePath, coursePath)

const currentLesson: {[key: string]: string} = {
const currentLesson: { [key: string]: string } = {
lesson: nLessonPath,
module: modulePath,
}
Expand All @@ -141,7 +159,7 @@ const FreeCodeCamp: FC<{}> = () => {
}

if (!certificateProgress) {
startMyCertificationsProgressAsync(
myCertificationProgressStart(
profile.userId,
lesson.course.certificationId,
lesson.course.id,
Expand All @@ -151,51 +169,51 @@ const FreeCodeCamp: FC<{}> = () => {
// TODO: remove this delay!!
// TEMP_FIX: delay this api call to allow for previous "completeLesson" call to write in the api
setTimeout(() => {
updateMyCertificationsProgressAsync(
certificateProgress.id,
UpdateMyCertificateProgressActions.currentLesson,
currentLesson
)
myCertificationProgressUpdate(
certificateProgress.id,
MyCertificationUpdateProgressActions.currentLesson,
currentLesson
)
.then(setCertificateProgress)
}, 500)
}
}

function handleFccLessonComplete(): void {
const currentLesson: {[key: string]: string} = {
const currentLesson: { [key: string]: string } = {
lesson: lessonParam,
module: moduleParam,
}
if (certificateProgress) {
updateMyCertificationsProgressAsync(
myCertificationProgressUpdate(
certificateProgress.id,
UpdateMyCertificateProgressActions.completeLesson,
MyCertificationUpdateProgressActions.completeLesson,
currentLesson
).then(setCertificateProgress)
}
}

useEffect(() => {
if (
certificateProgress &&
certificateProgress.courseProgressPercentage === 100 &&
certificateProgress.status === MyCertificationProgressStatus.inProgress
) {
updateMyCertificationsProgressAsync(
certificateProgress.id,
UpdateMyCertificateProgressActions.completeCertificate,
{}
)
.then(setCertificateProgress)
.then(() => {
const completedPath: string = getCertificationCompletedPath(
providerParam,
certificationParam
)
if (
certificateProgress &&
certificateProgress.courseProgressPercentage === 100 &&
certificateProgress.status === MyCertificationProgressStatus.inProgress
) {
myCertificationProgressUpdate(
certificateProgress.id,
MyCertificationUpdateProgressActions.completeCertificate,
{}
)
.then(setCertificateProgress)
.then(() => {
const completedPath: string = getCertificationCompletedPath(
providerParam,
certificationParam
)

navigate(completedPath)
})
}
navigate(completedPath)
})
}
}, [
certificateProgress,
certificationParam,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ export interface LearnCertification {
key: string
providerId: string
providerName: string
state: string
state: 'active' | 'coming-soon'
title: string
}
19 changes: 10 additions & 9 deletions src-ts/tools/learn/learn-lib/course-outline/CourseOutline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
LearnModule,
LearnMyCertificationProgress,
} from '../../learn-lib'
import { getFccLessonPath } from '../../learn.routes'
import { getLessonPathFromModule } from '../../learn.routes'

import { CollapsibleItem } from './collapsible-item'
import styles from './CourseOutline.module.scss'
Expand All @@ -22,14 +22,15 @@ interface CourseOutlineProps {

const CourseOutline: FC<CourseOutlineProps> = (props: CourseOutlineProps) => {

const lessonPath: (course: LearnCourse, module: LearnModule, lesson: LearnLesson) => string = useCallback((course: LearnCourse, module: LearnModule, lesson: LearnLesson) => {
return getFccLessonPath(
course.provider,
course.certification,
module.key,
lesson.dashedName,
)
}, [])
const lessonPath: (course: LearnCourse, module: LearnModule, lesson: LearnLesson) => string
= useCallback((course: LearnCourse, module: LearnModule, lesson: LearnLesson) => {
return getLessonPathFromModule(
course.provider,
course.certification,
module.key,
lesson.dashedName,
)
}, [])

return (
<div className={classNames(styles['wrap'], 'course-outline-wrap')}>
Expand Down
7 changes: 3 additions & 4 deletions src-ts/tools/learn/learn-lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
export * from './certifications-provider'
export * from './resource-provider-provider'
export * from './collapsible-pane'
export * from './course-outline'
export * from './course-title'
export * from './courses-provider'
export * from './curriculum-summary'
export * from './lesson-provider'
export * from './my-certifications-provider'
export * from './my-course-card'

export * from './course-outline'
export * from './course-title'
export * from './resource-provider-provider'
export * from './svgs'
export * from './wave-hero'
Loading