Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quantum-kittens
GitHub Repository: quantum-kittens/platypus
Path: blob/main/frontend/ts/courses.ts
3855 views
1
import { getUserData } from './storage'
2
3
interface Section {
4
title: string,
5
id: string,
6
uuid: string,
7
url: string,
8
pageUrl: string,
9
progress: number
10
}
11
12
interface Course {
13
title: string,
14
url: string,
15
id: string
16
type: string,
17
sections: Section[]
18
}
19
20
let promise: Promise<Course[]> | undefined
21
22
const getCourseList = () : Promise<Course[]> => {
23
if (!promise) {
24
promise = fetch('/courseList/').then((res) => {
25
return res.json().then((courses: Course[]) => {
26
assignProgressToCourses(courses)
27
return courses
28
})
29
})
30
}
31
return promise
32
}
33
34
const assignProgressToCourses = (courses: Course[]) => {
35
const userData = getUserData()
36
courses.forEach((course) => {
37
course.sections.forEach((section) => {
38
section.progress = (userData?.[course.id]?.[section.id]?.progress || 0) / 100
39
})
40
})
41
}
42
43
export {
44
getCourseList
45
}
46
47
export type {
48
Section,
49
Course
50
}
51
52