-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreducers.js
165 lines (155 loc) · 4.63 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
import { combineReducers } from 'redux'
import objectAssignDeep from 'object-assign-deep'
import objectAssign from 'object-assign-deep'
function selectSections(course, section_id, select) {
let out = objectAssignDeep({}, course)
let sections = out.data.sections
for(const key in sections) {
let section_types = sections[key]
for(let section_type in section_types) {
for(let section of section_types[section_type]) {
if(section_id === undefined || section_id === null || section['Class Number'] == section_id ) {
if (select === undefined) {
section.selected = !section.selected
}
else {
section.selected = select
}
}
}
}
}
return out
}
function setEnrollmentSection(course, section_id, enrollment) {
let out = objectAssignDeep({}, course)
let sections = out.data.sections
for(const key in sections) {
let section_types = sections[key]
for(let section_type in section_types) {
for(let section of section_types[section_type]) {
if(section['Class Number'] == section_id ) {
section.enrollment = enrollment
}
}
}
}
return out
}
function course(state = {}, action) {
switch(action.type) {
case 'ADD_COURSE':
return {
course: selectSections(action.course),
id: action.id,
selected: (action.selected === undefined ? true : action.selected)
}
case 'TOGGLE_COURSE':
if(state.id !== action.id) {
return state
} else {
return objectAssign({}, state, {
selected: !state.selected
})
}
case 'TOGGLE_SECTION':
if(state.id !== action.course_id) {
return state
} else {
return {
course: selectSections(state.course, action.section_id),
id: state.id,
selected: state.selected
}
}
case 'TOGGLE_ALL_SECTIONS':
if (state.id !== action.id) {
return state;
} else {
return {
course: selectSections(state.course, null, action.select),
id: state.id,
selected: state.selected
}
}
case 'ENROLLMENT_SECTION':
if(state.id !== action.course_id) {
return state
} else {
return {
course: setEnrollmentSection(state.course, action.section_id, action.enrollment),
id: state.id,
selected: state.selected
}
}
default:
return state
}
}
function courses(state = {picked: [], ids: {}}, action) {
switch(action.type) {
case 'ADD_COURSE':
if(state.ids[action.id]) {
return state;
} else {
let ids = objectAssignDeep({}, state.ids)
ids[action.id] = true
return {
picked: state.picked.concat([ course(undefined, action) ]),
ids
}
}
case 'TOGGLE_COURSE':
return {
picked: state.picked.map( c => course(c, action) ),
ids: state.ids
}
case 'ENROLLMENT_SECTION':
return {
picked: state.picked.map( c => course(c, action) ),
ids: state.ids
}
case 'REMOVE_COURSE':
let ids = objectAssignDeep({}, state.ids)
delete ids[action.id]
return {
picked: state.picked.filter( c => c.id != action.id ),
ids: ids
}
case 'TOGGLE_SECTION':
case 'TOGGLE_ALL_SECTIONS':
return {
picked: state.picked.map( c => course(c, action) ),
ids: state.ids
}
default:
return state
}
}
function calendars(state = {calendars: [], index: undefined}, action) {
switch(action.type) {
case 'SET_CALENDARS':
return {
calendars: action.calendars,
index: 0
}
case 'NEXT_CALENDAR_INDEX':
return objectAssignDeep({}, state, {
index: Math.min(state.index + 1, state.calendars.length - 1)
})
case 'PREV_CALENDAR_INDEX':
return objectAssignDeep({}, state, {
index: Math.max(state.index - 1, 0)
})
case 'SET_CALENDAR_INDEX':
return objectAssignDeep({}, state, {
index: action.index
})
default:
return state;
}
}
export const reducer = combineReducers({
courses,
calendars
})