generated from openedx/frontend-template-application
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdataUtils.js
More file actions
96 lines (88 loc) · 3.04 KB
/
dataUtils.js
File metadata and controls
96 lines (88 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* Calculates the completion status based on completion percentage
* @param {number} completion - Completion percentage (0-1)
* @returns {Object} Object containing status and percent
*/
export const calculateCompletionStatus = (completion) => {
const percent = completion || 0;
let status = 'In progress';
if (percent <= 0.0) {
status = 'Not started';
} else if (percent >= 1.0) {
status = 'Completed';
}
return { status, percent };
};
/**
* Adds completion status to a course
* @param {Object} course - Course object
* @param {Object} completionsMap - Map of course IDs to completion data
* @param {string} courseId - Course ID
* @returns {Object} Course object with completion status
*/
export const addCompletionStatus = (course, completionsMap, courseId) => {
const completionData = completionsMap[courseId];
const completion = completionData?.completion?.percent || 0;
const { status, percent } = calculateCompletionStatus(completion);
const optionalPossible = completionData?.optionalCompletion?.possible ?? 0;
const optionalEarned = completionData?.optionalCompletion?.earned ?? 0;
const hasOptionalCompletion = optionalPossible > 0;
const hasUnearnedOptionalCompletion = optionalPossible > optionalEarned;
return {
...course,
status,
percent,
hasOptionalCompletion,
hasUnearnedOptionalCompletion,
};
};
/**
* Creates a completions map from completions data
* @param {Array} completions - Array of completion data
* @returns {Object} Map of item keys to completion data
*/
export const createCompletionsMap = (completions) => {
const completionsMap = {};
completions?.forEach?.(item => {
completionsMap[item.courseKey] = {
completion: item.completion,
optionalCompletion: item.optionalCompletion,
};
});
return completionsMap;
};
/**
* Creates a map of course keys to learning path data
* @param {Array} learningPaths - Array of learning path data
* @returns {Object} Map of course keys to an array of learning path objects with name and key
*/
export const createCourseToLearningPathsMap = (learningPaths) => {
const courseToLearningPathsMap = {};
if (!learningPaths || !Array.isArray(learningPaths)) {
return courseToLearningPathsMap;
}
learningPaths.forEach(path => {
if (path.steps && Array.isArray(path.steps)) {
path.steps.forEach(step => {
if (!courseToLearningPathsMap[step.courseKey]) {
courseToLearningPathsMap[step.courseKey] = [];
}
courseToLearningPathsMap[step.courseKey].push({
name: path.displayName,
key: path.key,
});
});
}
});
return courseToLearningPathsMap;
};
/**
* Adds learning path data to a course
* @param {Object} course - Course object
* @param {Object} courseToLearningPathMap - Map of course keys to an array of learning path objects with name and key
* @returns {Object} Course object with learning path data
*/
export const addLearningPaths = (course, courseToLearningPathMap) => ({
...course,
learningPaths: courseToLearningPathMap[course.id],
});