schedule.reducer.ts 901 Bytes
Newer Older
1
2
3
4
5
6
/**
 * reducers/schedule.reducer.ts
 *
 * Perform operations on the current state of the "Schedule" list in the store
 * and return a new definition of the state.
 */
David Haynes's avatar
David Haynes committed
7
import { ADD_COURSE_SECTION, REMOVE_COURSE_SECTION } from '../actions/schedule/schedule.action-types';
Andrew Hrdy's avatar
Andrew Hrdy committed
8
import { ScheduleAction } from '../actions/schedule/schedule.actions';
9
import CourseSection from '../util/CourseSection';
Andrew Hrdy's avatar
Andrew Hrdy committed
10

David Haynes's avatar
David Haynes committed
11
export type ScheduleState = CourseSection[];
Andrew Hrdy's avatar
Andrew Hrdy committed
12
13
14

export const schedule = (state: ScheduleState = [], action: ScheduleAction) => {
    switch (action.type) {
David Haynes's avatar
David Haynes committed
15
16
17
18
19
20
        case ADD_COURSE_SECTION:
            return state.findIndex(s => s.crn === action.courseSection.crn) === -1
                ? [...state, action.courseSection]
                : state;
        case REMOVE_COURSE_SECTION:
            return state.filter(s => s.crn !== action.courseSection.crn);
Andrew Hrdy's avatar
Andrew Hrdy committed
21
22
23
        default:
            return state;
    }
David Haynes's avatar
David Haynes committed
24
};