forked from code-dot-org/code-dot-org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstageLockRedux.js
More file actions
270 lines (233 loc) · 7.26 KB
/
stageLockRedux.js
File metadata and controls
270 lines (233 loc) · 7.26 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/**
* Reducer and actions for stage lock info. This includes the teacher panel on
* the course overview page, and the stage locking dialog.
*/
import $ from 'jquery';
import _ from 'lodash';
import { makeEnum } from '@cdo/apps/utils';
export const ViewType = makeEnum('Student', 'Teacher');
export const LockStatus = makeEnum('Locked', 'Editable', 'ReadonlyAnswers');
// Action types
const SET_VIEW_TYPE = 'stageLock/SET_VIEW_TYPE';
const SET_SECTIONS = 'stageLock/SET_SECTIONS';
const SELECT_SECTION = 'stageLock/SELECT_SECTION';
const OPEN_LOCK_DIALOG = 'stageLock/OPEN_LOCK_DIALOG';
export const CLOSE_LOCK_DIALOG = 'stageLock/CLOSE_LOCK_DIALOG';
export const BEGIN_SAVE = 'stageLock/BEGIN_SAVE';
export const FINISH_SAVE = 'stageLock/FINISH_SAVE';
const AUTHORIZE_LOCKABLE = 'progress/AUTHORIZE_LOCKABLE';
export const initialState = {
viewAs: ViewType.Student,
sections: {},
selectedSection: null,
sectionsLoaded: false,
lockDialogStageId: null,
// The locking info for the currently selected section/stage
lockStatus: [],
saving: false,
// whether user is allowed to see lockable stages
lockableAuthorized: false
};
/**
* Stage lock reducer
*/
export default function reducer(state = initialState, action) {
if (action.type === AUTHORIZE_LOCKABLE) {
return Object.assign({}, state, {
lockableAuthorized: true
});
}
if (action.type === SET_VIEW_TYPE) {
return Object.assign({}, state, {
viewAs: action.viewAs
});
}
if (action.type === SET_SECTIONS) {
const sectionId = Object.keys(action.sections)[0];
return Object.assign({}, state, {
sections: action.sections,
sectionsLoaded: true,
selectedSection: sectionId,
});
}
if (action.type === SELECT_SECTION) {
const sectionId = action.sectionId;
if (!state.sections[sectionId]) {
throw new Error(`Unknown sectionId ${sectionId}`);
}
const nextState = {
...state,
selectedSection: sectionId,
};
// If we have a lockStatus (i.e. from an open dialog) we need to update
// it with the new section
const { lockDialogStageId, lockStatus } = state;
return {
...nextState,
lockStatus: lockDialogStageId ?
lockStatusForStage(nextState, lockDialogStageId) : lockStatus
};
}
if (action.type === OPEN_LOCK_DIALOG) {
const lockDialogStageId = action.stageId;
return Object.assign({}, state, {
lockDialogStageId,
lockStatus: lockStatusForStage(state, lockDialogStageId)
});
}
if (action.type === CLOSE_LOCK_DIALOG) {
return Object.assign({}, state, {
lockDialogStageId: null,
lockStatus: []
});
}
if (action.type === BEGIN_SAVE) {
return Object.assign({}, state, {
saving: true
});
}
if (action.type === FINISH_SAVE) {
const { sections, selectedSection } = state;
const { lockStatus: nextLockStatus, stageId } = action;
const nextStage = _.cloneDeep(sections[selectedSection].stages[stageId]);
// Update locked/readonly_answers in stages based on the new lockStatus provided
// by our dialog.
nextStage.forEach((item, index) => {
const update = nextLockStatus[index];
// We assume lockStatus is ordered the same as stageToUpdate. Let's
// validate that.
if (item.user_level_id !== update.userLevelId) {
throw new Error('Expect user ids be the same');
}
item.locked = update.lockStatus === LockStatus.Locked;
item.readonly_answers = update.lockStatus === LockStatus.ReadonlyAnswers;
});
const nextState = _.cloneDeep(state);
nextState.sections[selectedSection].stages[stageId] = nextStage;
return Object.assign(nextState, {
lockStatus: nextLockStatus,
saving: false
});
}
return state;
}
// Action creators
/**
* Authorizes the user to be able to see lockable stages
*/
export const authorizeLockable = () => ({ type: AUTHORIZE_LOCKABLE });
export const setViewType = viewType => {
if (!ViewType[viewType]) {
throw new Error('unknown ViewType: ' + viewType);
}
return {
type: SET_VIEW_TYPE,
viewAs: viewType
};
};
export const setSections = sections => ({
type: SET_SECTIONS,
sections
});
export const selectSection = sectionId => ({
type: SELECT_SECTION,
sectionId
});
export const openLockDialog = stageId => ({
type: OPEN_LOCK_DIALOG,
stageId
});
export const beginSave = () => ({ type: BEGIN_SAVE });
export const finishSave = (newLockStatus, stageId) => ({
type: FINISH_SAVE,
lockStatus: newLockStatus,
stageId
});
/**
* Action asynchronously dispatches a set of actions around saving our
* lock status.
*/
const performSave = (newLockStatus, stageId, onComplete) => {
return (dispatch, getState) => {
const oldLockStatus = getState().stageLock.lockStatus;
const saveData = newLockStatus.filter((item, index) => {
// Only need to save items that changed
return !_.isEqual(item, oldLockStatus[index]);
}).map(item => ({
user_level_data: item.userLevelData,
locked: item.lockStatus === LockStatus.Locked,
readonly_answers: item.lockStatus === LockStatus.ReadonlyAnswers
}));
if (saveData.length === 0) {
onComplete();
return;
}
dispatch(beginSave());
$.ajax({
type: 'POST',
url: '/api/lock_status',
dataType: 'json',
contentType: 'application/json',
data: JSON.stringify({updates: saveData})
}).done(() => {
dispatch(finishSave(newLockStatus, stageId));
onComplete();
})
.fail(err => {
console.error(err);
onComplete();
});
};
};
export const saveLockDialog = (newLockStatus) => {
return (dispatch, getState) => {
const stageId = getState().stageLock.lockDialogStageId;
dispatch(performSave(newLockStatus, stageId, () => {
dispatch(closeLockDialog());
}));
};
};
export const lockStage = (stageId) => {
return (dispatch, getState) => {
const oldLockStatus = lockStatusForStage(getState().stageLock, stageId);
const newLockStatus = oldLockStatus.map(student => Object.assign({}, student, {
lockStatus: LockStatus.Locked
}));
dispatch(performSave(newLockStatus, stageId, () => {}));
};
};
export const closeLockDialog = () => ({
type: CLOSE_LOCK_DIALOG
});
// Helpers
const lockStatusForStage = (state, stageId) => {
const { sections, selectedSection } = state;
const students = sections[selectedSection].stages[stageId];
return students.map(student => ({
userLevelData: student.user_level_data,
name: student.name,
lockStatus: student.locked ? LockStatus.Locked : (
student.readonly_answers ? LockStatus.ReadonlyAnswers : LockStatus.Editable)
}));
};
/**
* Helper that returns a mapping of stageId to whether or not it is fully locked
* in the current section. A stage is fully locked if and only if it is locked
* for all of the students in the section
*/
export const fullyLockedStageMapping = (state) => {
const { sections, selectedSection } = state;
if (!selectedSection) {
return {};
}
const section = sections[selectedSection];
const stageIds = Object.keys(section.stages);
return stageIds.reduce((obj, stageId) => {
const students = section.stages[stageId];
const fullyLocked = !students.some(student => !student.locked);
return {
...obj,
[stageId]: fullyLocked
};
}, {});
};