forked from Lab-Lab-Lab/CPR-Music
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reducers.js
292 lines (266 loc) · 7.97 KB
/
reducers.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
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
289
290
291
292
import { combineReducers } from 'redux';
import * as types from './types';
const mockAssignments = {
loaded: false,
items: [],
};
const assignmentsReducer = (state = mockAssignments, { type, payload }) => {
switch (type) {
case types.Action.GotAssignments:
return {
loaded: true,
items: payload,
};
}
return state;
};
const initialAssignedPieces = {
loaded: false,
items: {},
};
// TODO what is this reducer for? it looks like it's to handle when the teacher
// assigns pieces, but it seems to also happen when the student needs to see
// their list of activities for a single piece?
// maybe any time either of the following bits of state are used from redux?
// activities
// assignedPieces
const assignedPiecesReducer = (
state = initialAssignedPieces,
{ type, payload },
) => {
switch (type) {
case types.Action.GotActivities:
// we have to make some changes here. the response is now already grouped.
// the response will be an object with each key being a piece slug whose
// value is an array of activities
const pieceSlugsKeys = Object.keys(payload.activities);
let pieces = pieceSlugsKeys.map((k) => ({
id: payload.activities[k][0].piece_id,
slug: k,
name: payload.activities[k][0].piece_name,
}));
// payload.activities.map(
// (assignment) => ({
// id: assignment.piece_id,
// name: assignment.piece_name
// })
// );
pieces.sort((a, b) => (a.id < b.id ? -1 : 1)); // FIXME how should the pieces be sorted? this assumes by the piece's id, but perhaps it should be by the order property of the piece_plan if it's available?
// what does this next step do??
// it looks like it says to only keep the first piece with a given id // thanks copilot for finishing my sentence
pieces = pieces.filter((piece, i, arr) =>
i === 0 ? true : piece.id !== arr[i - 1].id,
);
// FIXME again, as above, how do we want pieces sorted?
pieces.sort((a, b) => (a.name < b.name ? -1 : 1));
// return { loaded: true, items: pieces };
return { ...state, items: { ...state.items, [payload.slug]: pieces } };
case types.Action.AssignedPiece:
return {
...state,
items: {
...state.items,
[payload.slug]: [...state.items[payload.slug], payload.piece],
},
};
case types.Action.UnassignedPiece:
return {
...state,
items: {
...state.items,
[payload.slug]: [
...state.items[payload.slug].filter(
(pieceObj) => pieceObj.id !== payload.piece.id,
),
],
},
};
}
return state;
};
const initialActivities = {
loaded: false,
items: [],
};
const activitiesReducer = (state = initialActivities, { type, payload }) => {
switch (type) {
case types.Action.GotActivities:
console.log('payload', payload);
return { loaded: true, items: payload.activities };
}
return state;
};
const initialPieces = { loaded: false, items: [] };
const piecesReducer = (state = initialPieces, { type, payload }) => {
switch (type) {
case types.Action.GotPieces:
return { loaded: true, items: payload };
}
return state;
};
const mockEnrollments = { loaded: false, items: [] };
const enrollmentsReducer = (state = mockEnrollments, { type, payload }) => {
switch (type) {
case types.Action.AddedRoster:
return { ...state, shouldInstrument: true };
case types.Action.GotEnrollments:
return { loaded: true, items: payload, shouldInstrument: false };
case types.Action.DidInstrument:
return { ...state, shouldInstrument: false };
}
return state;
};
const mockRoster = { loaded: false, items: [] };
const rosterReducer = (state = mockRoster, { type, payload }) => {
switch (type) {
case types.Action.GotRoster:
const items = {};
payload.enrollments.forEach((item) => {
items[item.id] = {
...item,
activityState: types.ActivityState.Inactive,
};
});
return { loaded: true, items, courseSlug: payload.courseSlug };
case types.Action.UpdatedEnrollmentInstrument:
return {
...state,
items: {
...state.items,
[payload.enrollment.id]: {
...state.items[payload.enrollment.id],
instrument: payload.instrument,
activityState: types.ActivityState.Success,
},
},
};
case types.Action.SetInstrumentActive:
return {
...state,
items: {
...state.items,
[payload.enrollmentId]: {
...state.items[payload.enrollmentId],
activityState: payload.activityState,
},
},
};
}
return state;
};
const initialCurrentUser = { loaded: false };
const currentUserReducer = (state = initialCurrentUser, { type, payload }) => {
switch (type) {
case types.Action.HaveUser:
return {
...state,
loaded: true,
username: payload.user.name,
token: payload.token,
};
case types.Action.GotProfile:
return {
...state,
...payload,
};
case types.Action.LoggedOut:
return { loaded: false };
}
return state;
};
const mockInstruments = { loaded: false, items: [] };
const instrumentsReducer = (state = mockInstruments, { type, payload }) => {
switch (type) {
case types.Action.GotInstruments:
const items = {};
payload.forEach((instrument) => {
items[instrument.id] = instrument;
});
return { loaded: true, items };
}
return state;
};
const selectedEnrollmentReducer = (state = {}, { type, payload }) => {
switch (type) {
case types.Action.SelectedEnrollment:
if (payload) {
return payload;
}
}
return state;
};
const selectedAssignmentReducer = (
state = { uploadStatus: types.UploadStatusEnum.Inactive },
{ type, payload },
) => {
switch (type) {
case types.Action.SelectedAssignment:
return payload;
// case types.Action.BeginUpload:
// return { ...state, uploadStatus: types.UploadStatusEnum.Active };
// case types.Action.UploadSucceeded:
// return { ...state, uploadStatus: types.UploadStatusEnum.Success };
// case types.Action.UploadFailed:
// return { ...state, uploadStatus: types.UploadStatusEnum.Erroneous };
}
return state;
};
const submitStatusReducer = (
state = { submissions: {} },
{ type, payload },
) => {
switch (type) {
case types.Action.BeginUpload:
return {
...state,
submissions: {
...state.submissions,
[payload.id]: types.UploadStatusEnum.Active,
},
};
case types.Action.UploadSucceeded:
return {
...state,
submissions: {
...state.submissions,
[payload.id]: types.UploadStatusEnum.Success,
},
};
case types.Action.UploadFailed:
return {
...state,
submissions: {
...state.submissions,
[payload.id]: types.UploadStatusEnum.Erroneous,
},
};
case types.Action.UploadFinished:
return {
...state,
submissions: {
// ...Object.keys(state.submissions).filter((elem) => elem !== payload.id).reduce( (res, key) => (res[key] = state.submissions[key], res), {} )
...Object.fromEntries(
Object.entries(state.submissions).filter(
(elem) => elem !== payload.id,
),
),
},
};
}
return state;
};
// COMBINED REDUCERS
const reducers = {
assignments: assignmentsReducer,
activities: activitiesReducer,
assignedPieces: assignedPiecesReducer,
pieces: piecesReducer,
enrollments: enrollmentsReducer,
instruments: instrumentsReducer,
roster: rosterReducer,
currentUser: currentUserReducer,
selectedEnrollment: selectedEnrollmentReducer,
selectedAssignment: selectedAssignmentReducer,
submission: submitStatusReducer,
};
export default combineReducers(reducers);