-
Notifications
You must be signed in to change notification settings - Fork 526
Expand file tree
/
Copy pathhiddenLessonRedux.js
More file actions
288 lines (261 loc) · 8.42 KB
/
Copy pathhiddenLessonRedux.js
File metadata and controls
288 lines (261 loc) · 8.42 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/**
* Reducer and actions used to track what sections/scripts are are hidden on a
* per section basis.
*/
import Immutable from 'immutable';
import $ from 'jquery';
const SET_HIDDEN_LESSONS = 'hiddenLesson/SET_HIDDEN_LESSONS';
const UPDATE_HIDDEN_LESSON = 'hiddenLesson/UPDATE_HIDDEN_LESSON';
const UPDATE_HIDDEN_SCRIPT = 'hiddenLesson/UPDATE_HIDDEN_SCRIPT';
export const STUDENT_SECTION_ID = 'STUDENT';
const HiddenState = Immutable.Record({
hiddenLessonsInitialized: false,
hideableLessonsAllowed: false,
// A mapping, where the key is the sectionId, and the value is a mapping from
// lessonId to a bool indicating whether that lesson is hidden (true) or not (false)
// Teachers will potentially have a number of section ids. For students we
// use a sectionId of STUDENT_SECTION_ID, which represents the hidden state
// for the student based on the sections they are in.
lessonsBySection: Immutable.Map({
// [sectionId]: {
// [lessonId]: true
// }
}),
// Same as above but for hiding scripts in a section instead of lessons
scriptsBySection: Immutable.Map({
// [sectionId]: {
// [scriptId]: true
// }
}),
});
/**
* Validates that we never have multiple lessonsBySection if we have STUDENT_SECTION_ID
* @throws If new state is invalid
*/
function validateSectionIds(state) {
if (
state.getIn(['lessonsBySection', STUDENT_SECTION_ID]) &&
state.get('lessonsBySection').size > 1
) {
throw new Error(
'Should never have STUDENT_SECTION_ID alongside other sectionIds'
);
}
}
/**
* Hidden lesson reducer
*/
export default function reducer(state = new HiddenState(), action) {
if (action.type === SET_HIDDEN_LESSONS) {
const {hiddenLessonsPerSection, hideableLessonsAllowed} = action;
// Iterate through each section
const sectionIds = Object.keys(hiddenLessonsPerSection);
let nextState = state;
sectionIds.forEach(sectionId => {
// And iterate through each hidden lesson within that section
const hiddenLessonIds = hiddenLessonsPerSection[sectionId];
hiddenLessonIds.forEach(lessonId => {
nextState = nextState.setIn(
['lessonsBySection', sectionId, lessonId.toString()],
true
);
});
});
validateSectionIds(nextState);
return nextState.merge({
hiddenLessonsInitialized: true,
hideableLessonsAllowed,
});
}
if (action.type === UPDATE_HIDDEN_LESSON) {
const {sectionId, lessonId, hidden} = action;
const nextState = state.setIn(
['lessonsBySection', sectionId, lessonId.toString()],
hidden
);
validateSectionIds(nextState);
return nextState;
}
if (action.type === UPDATE_HIDDEN_SCRIPT) {
const {sectionId, scriptId, hidden} = action;
const nextState = state.setIn(
['scriptsBySection', sectionId.toString(), scriptId.toString()],
hidden
);
validateSectionIds(nextState);
return nextState;
}
return state;
}
// action creators
/**
* @param {object} hiddenLessonsPerSection - Mapping from sectionId to a list of lessonIds
* that are hidden for that section.
* @param {bool} hideableLessonsAllowed - True if we're able to toggle hidden lessons
*/
export function setHiddenLessons(
hiddenLessonsPerSection,
hideableLessonsAllowed
) {
return {
type: SET_HIDDEN_LESSONS,
hiddenLessonsPerSection,
hideableLessonsAllowed,
};
}
export function updateHiddenLesson(sectionId, lessonId, hidden) {
return {
type: UPDATE_HIDDEN_LESSON,
sectionId,
lessonId,
hidden,
};
}
export function updateHiddenScript(sectionId, scriptId, hidden) {
return {
type: UPDATE_HIDDEN_SCRIPT,
sectionId,
scriptId,
hidden,
};
}
/**
* Toggle the hidden state of a particular lesson in a section, updating our local
* state to reflect the change, and posting to the server.
*/
export function toggleHiddenLesson(scriptName, sectionId, lessonId, hidden) {
return dispatch => {
// update local state
dispatch(updateHiddenLesson(sectionId, lessonId, hidden));
postToggleHidden(scriptName, sectionId, lessonId, hidden);
};
}
/**
* Toggle the hidden state of a particular script in a section.
*/
export function toggleHiddenScript(scriptName, sectionId, scriptId, hidden) {
return dispatch => {
dispatch(updateHiddenScript(sectionId, scriptId, hidden));
postToggleHidden(scriptName, sectionId, null, hidden);
};
}
/**
* Post to the server to toggle the hidden state of a lesson or script. lessonId
* should be null if we're hiding the script rather than a particular lesson
* @param {string} scriptName
* @param {string} sectionId
* @param {string} lessonId
* @param {boolean} hidden
*/
function postToggleHidden(scriptName, sectionId, lessonId, hidden) {
const data = {
section_id: sectionId,
hidden,
};
if (lessonId) {
data.stage_id = lessonId;
}
$.ajax({
type: 'POST',
url: `/s/${scriptName}/toggle_hidden`,
dataType: 'json',
contentType: 'application/json',
data: JSON.stringify(data),
}).success(() => {
window.__TestInterface = window.__TestInterface || {};
window.__TestInterface.toggleHiddenUnitComplete = true;
});
}
/**
* Query server for hidden lesson ids, and (potentially) toggle whether or not we
* are able to mark lessons as hideable.
* @param {string} scriptName
* @param {boolean} canHideLessons If true, inform redux that we're able to toggle
* whether or not lessons are hidden.
*/
export function getHiddenLessons(scriptName, canHideLessons) {
return dispatch => {
$.ajax({
type: 'GET',
url: `/s/${scriptName}/hidden_lessons`,
dataType: 'json',
contentType: 'application/json',
})
.done(response =>
dispatch(initializeHiddenLessons(response, canHideLessons))
)
.fail(err => console.error(err));
};
}
/**
* Initialize hidden lessons based on server data. In the case of a student, this
* will be a list of hidden lesson ids. In the case of a teacher, it will be
* a mapping from section id to a list of hidden lesson ids for that section
* @param {string[]|Object<string, string[]>} data
* @param {boolean} canHideLessons - True if we're able to toggle hidden lessons
*/
function initializeHiddenLessons(data, canHideLessons) {
return dispatch => {
// For a instructor, we get back a map of section id to hidden lesson ids
// For a participant, we just get back a list of hidden lesson ids. Turn that
// into an object, under the 'sectionId' of STUDENT_SECTION_ID
if (Array.isArray(data)) {
data = {[STUDENT_SECTION_ID]: data};
}
dispatch(setHiddenLessons(data, !!canHideLessons));
};
}
/**
* Given server data for the set of scripts that are hidden for this user,
* populate our redux store.
* @param {string[]|Object<string, string[]>} data
*/
export function initializeHiddenScripts(data) {
return dispatch => {
if (!data) {
return;
}
// For a teacher, we get back a map of section id to hidden script ids
// For a student, we just get back a list of hidden script ids. Turn that
// into an object, under the 'sectionId' of STUDENT_SECTION_ID
if (Array.isArray(data)) {
data = {[STUDENT_SECTION_ID]: data};
}
Object.keys(data).forEach(sectionId => {
const hiddenScriptIds = data[sectionId];
hiddenScriptIds.forEach(scriptId => {
dispatch(updateHiddenScript(sectionId, scriptId, true));
});
});
};
}
// utils
/**
* Helper to determine whether a lesson is hidden for a given section. If no
* section is given, we assume this is a student and use STUDENT_SECTION_ID
*/
export function isLessonHiddenForSection(state, sectionId, lessonId) {
return isHiddenForSection(state, sectionId, lessonId, 'lessonsBySection');
}
/**
* Helper to determine whether a script is hidden for a given section. If no
* section is given, we assume this is a student and use STUDENT_SECTION_ID
*/
export function isScriptHiddenForSection(state, sectionId, scriptId) {
return isHiddenForSection(state, sectionId, scriptId, 'scriptsBySection');
}
/**
* Helper used by the above two methods so that we behave the same when looking
* for hidden lessons/scripts
*/
function isHiddenForSection(state, sectionId, itemId, bySectionKey) {
if (!itemId) {
return false;
}
// if we don't have a sectionId, we must be a student
if (!sectionId) {
sectionId = STUDENT_SECTION_ID;
}
const bySection = state.get(bySectionKey);
return !!bySection.getIn([sectionId.toString(), itemId.toString()]);
}