sectorcourses.reducer.ts 3.27 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
    SECTORCOURSES_RECEIVED,
8
    SECTORCOURSES_SECTOR_SELECTED_SAVE,
9 10 11
    SECTORCOURSES_SELECTED_SAVE,
    SECTORCOURSES_INIT
} from "../../constants";
12

13
export function sectorCoursesReducer(state: ISectorRecords = SECTOR_COURSES_INITIAL_STATE, action): ISectorRecords {
14 15
    switch (action.type) {
        case SECTORCOURSES_RECEIVED:
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
        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);

        case SECTORCOURSES_SECTOR_SELECTED_SAVE:
            return state.withMutations(function(list) {
                if (action.payload.prevChoice >= 0)
                    list.setIn([action.payload.prevChoice, "sector_selected"], false);
                if (action.payload.newChoice >= 0)
                    list.setIn([action.payload.newChoice, "sector_selected"], true);
38
            });
39

40
        case SECTORCOURSES_SELECTED_SAVE:
41 42 43 44 45 46 47 48 49 50 51
            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));
            });


/*            let sectorsWithSelections = Array<ISector>();
            let ind = 0;
            j = 0;
52 53 54 55 56 57 58 59
            state.forEach(sector => {
                sectorsWithSelections.push(<ISector>{ sector_id: sector.sector_id, sector_name: sector.sector_name, sector_selected: action.payload.sectorSelected[ind], courses: Array<ISectorCourse>() });
                sector.courses.forEach(course => {
                    sectorsWithSelections[ind].courses.push(<ISectorCourse>{ course_id: course.course_id, course_name: course.course_name, globalIndex: course.globalIndex, selected: action.payload.sectorCoursesSelected[j] });
                    j++;
                });
                ind++;
            });
60
            return Seq(sectorsWithSelections).map(n => n).toList(); */
61 62 63 64
        case SECTORCOURSES_INIT:
            return SECTOR_COURSES_INITIAL_STATE;
        default: return state;
    }
65
};