sectorcourses.reducer.ts 2.09 KB
Newer Older
1
import { ISectorRecords, ISectorRecord, ISectorCourseRecord, ISector, ISectorCourse } from "./sectorcourses.types";
2
import { SECTOR_COURSES_INITIAL_STATE } from "./sectorcourses.initial-state";
3 4
import { List } from "immutable";
import {recordify} from "typed-immutable-record";
5 6

import {
7 8 9 10
    SECTORCOURSES_RECEIVED,
    SECTORCOURSES_SELECTED_SAVE,
    SECTORCOURSES_INIT
} from "../../constants";
11

12
export function sectorCoursesReducer(state: ISectorRecords = SECTOR_COURSES_INITIAL_STATE, action): ISectorRecords {
13 14
    switch (action.type) {
        case SECTORCOURSES_RECEIVED:
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
        let newSectors = Array<ISectorRecord>();
        let newCourses = Array<ISectorCourseRecord>();
        let i = 0, j = 0;
        let ii = 0;

        action.payload.sectors.forEach(sector => {
            sector.courses.forEach(course => {
                newCourses.push(recordify<ISectorCourse, ISectorCourseRecord>({ course_id: course.course_id, course_name: course.course_name, globalIndex: course.globalIndex, selected: course.selected }));
                ii++;
            });
            newSectors.push(recordify<ISector, ISectorRecord>({ sector_id: sector.sector_id, sector_name: sector.sector_name, sector_selected: sector.sector_selected, courses: List(newCourses) }));
            newCourses = Array<ISectorCourseRecord>();
            i++;
        });
        return List(newSectors);

31
        case SECTORCOURSES_SELECTED_SAVE:
32 33 34 35 36 37 38
            return state.withMutations(function(list) {
                list.setIn([action.payload.oldSIndex, "sector_selected"], false);
                list.setIn([action.payload.sIndex, "sector_selected"], true);
                list.setIn([action.payload.oldSIndex, "courses"], list.get(action.payload.oldSIndex).get("courses").setIn([action.payload.oldCIndex, "selected"], false));
                list.setIn([action.payload.sIndex, "courses"], list.get(action.payload.sIndex).get("courses").setIn([action.payload.cIndex, "selected"], action.payload.checked));
            });

39 40 41 42
        case SECTORCOURSES_INIT:
            return SECTOR_COURSES_INITIAL_STATE;
        default: return state;
    }
43
};