-
Notifications
You must be signed in to change notification settings - Fork 634
/
useContentNodeProgress.js
73 lines (66 loc) · 2.14 KB
/
useContentNodeProgress.js
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
/**
* A composable function containing logic related to learner's
* progress through resources
* All data exposed by this function belong to a current learner.
*/
import { reactive } from 'kolibri.lib.vueCompositionApi';
import { set } from '@vueuse/core';
import { ContentNodeProgressResource } from 'kolibri.resources';
// The reactive is defined in the outer scope so it can be used as a shared store
const contentNodeProgressMap = reactive({});
export function setContentNodeProgress(progress) {
// Avoid setting stale progress data - assume that progress increases monotonically
if (
!contentNodeProgressMap[progress.content_id] ||
progress.progress > contentNodeProgressMap[progress.content_id]
) {
set(contentNodeProgressMap, progress.content_id, progress.progress);
}
}
export default function useContentNodeProgress() {
/**
* Fetches content node progress data
* and saves data to this composable's store
*
* @param {Object} getParams Parameters to filter by, should be the same as
* the contentnodes fetched that we want the progress for.
* @returns {Promise}
* @public
*/
function fetchContentNodeProgress(getParams) {
return ContentNodeProgressResource.fetchCollection({
getParams,
force: true,
}).then(progressData => {
const progresses = progressData ? progressData : [];
for (let progress of progresses) {
setContentNodeProgress(progress);
}
});
}
/**
* Fetches content node progress data
* and saves data to this composable's store
*
* @param {Object} getParams Parameters to filter by, should be the same as
* the contentnodes fetched that we want the progress for.
* @returns {Promise}
* @public
*/
function fetchContentNodeTreeProgress({ id, params }) {
return ContentNodeProgressResource.fetchTree({
params,
id,
}).then(progressData => {
const progresses = progressData ? progressData : [];
for (let progress of progresses) {
setContentNodeProgress(progress);
}
});
}
return {
fetchContentNodeProgress,
fetchContentNodeTreeProgress,
contentNodeProgressMap,
};
}