forked from code-dot-org/code-dot-org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogressRedux.js
More file actions
157 lines (138 loc) · 4.55 KB
/
progressRedux.js
File metadata and controls
157 lines (138 loc) · 4.55 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* Reducer and actions for progress
*/
import _ from 'lodash';
import {
LOCKED_RESULT,
LevelStatus,
mergeActivityResult,
activityCssClass
} from './activityUtils';
// Action types
export const INIT_PROGRESS = 'progress/INIT_PROGRESS';
const MERGE_PROGRESS = 'progress/MERGE_PROGRESS';
const UPDATE_FOCUS_AREAS = 'progress/UPDATE_FOCUS_AREAS';
const SHOW_TEACHER_INFO = 'progress/SHOW_TEACHER_INFO';
const initialState = {
currentLevelId: null,
professionalLearningCourse: null,
// a mapping of level id to result
levelProgress: {},
focusAreaPositions: [],
saveAnswersBeforeNavigation: null,
stages: null,
peerReviewsRequired: {},
peerReviewsPerformed: [],
showTeacherInfo: false,
};
/**
* Progress reducer
*/
export default function reducer(state = initialState, action) {
if (action.type === INIT_PROGRESS) {
// Re-initializing with full set of stages shouldn't blow away currentStageId
const currentStageId = state.currentStageId ||
(action.stages.length === 1 ? action.stages[0].id : undefined);
// extract fields we care about from action
return Object.assign({}, state, {
currentLevelId: action.currentLevelId,
professionalLearningCourse: action.professionalLearningCourse,
saveAnswersBeforeNavigation: action.saveAnswersBeforeNavigation,
stages: action.stages.map(stage => _.omit(stage, 'hidden')),
scriptName: action.scriptName,
currentStageId
});
}
if (action.type === MERGE_PROGRESS) {
// TODO: _.mergeWith after upgrading to Lodash 4+
let newLevelProgress = {};
const combinedLevels = Object.keys(Object.assign({}, state.levelProgress, action.levelProgress));
combinedLevels.forEach(key => {
newLevelProgress[key] = mergeActivityResult(state.levelProgress[key], action.levelProgress[key]);
});
return Object.assign({}, state, {
levelProgress: newLevelProgress,
stages: state.stages.map(stage => Object.assign({}, stage, {levels: stage.levels.map((level, index) => {
if (stage.lockable && level.ids.every(id => newLevelProgress[id] === LOCKED_RESULT)) {
return Object.assign({}, level, { status: LevelStatus.locked });
}
const id = level.uid || bestResultLevelId(level.ids, newLevelProgress);
if (action.peerReviewsPerformed && stage.flex_category === 'Peer Review') {
Object.assign(level, action.peerReviewsPerformed[index]);
}
return Object.assign({}, level, level.kind !== 'peer_review' && {
status: activityCssClass(newLevelProgress[id])
});
})}))
});
}
if (action.type === UPDATE_FOCUS_AREAS) {
return Object.assign({}, state, {
changeFocusAreaPath: action.changeFocusAreaPath,
focusAreaPositions: action.focusAreaPositions
});
}
if (action.type === SHOW_TEACHER_INFO) {
return Object.assign({}, state, {
showTeacherInfo: true
});
}
return state;
}
// Helpers
/**
* Return the level with the highest progress, or the first level if none have
* been attempted
* @param {number[]} levelIds
* @param {Object.<number,number>} - Mapping from level id to progress result
*/
function bestResultLevelId(levelIds, progressData) {
// The usual case
if (levelIds.length === 1) {
return levelIds[0];
}
// Return the level with the highest result
var attemptedIds = levelIds.filter(id => progressData[id]);
if (attemptedIds.length === 0) {
// None of them have been attempted, just return the first
return levelIds[0];
}
var bestId = attemptedIds[0];
var bestResult = progressData[bestId];
attemptedIds.forEach(function (id) {
var result = progressData[id];
if (result > bestResult) {
bestId = id;
bestResult = result;
}
});
return bestId;
}
// Action creators
export const initProgress = ({currentLevelId, professionalLearningCourse,
saveAnswersBeforeNavigation, stages, scriptName, peerReviewsRequired}) => ({
type: INIT_PROGRESS,
currentLevelId,
professionalLearningCourse,
saveAnswersBeforeNavigation,
stages,
scriptName,
peerReviewsRequired
});
export const mergeProgress = (levelProgress, peerReviewsPerformed) => ({
type: MERGE_PROGRESS,
levelProgress,
peerReviewsPerformed
});
export const updateFocusArea = (changeFocusAreaPath, focusAreaPositions) => ({
type: UPDATE_FOCUS_AREAS,
changeFocusAreaPath,
focusAreaPositions
});
export const showTeacherInfo = () => ({ type: SHOW_TEACHER_INFO });
/* start-test-block */
// export private function(s) to expose to unit testing
export const __testonly__ = {
bestResultLevelId
};
/* end-test-block */