sectorcourses.reducer.ts 1.89 KB
Newer Older
1
import { ISectors, ISector, ISectorCourse } from './sectorcourses.types';
2
import { SECTOR_COURSES_INITIAL_STATE } from './sectorcourses.initial-state';
3
4
5
6
import { Seq } from 'immutable';

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

11
export function sectorCoursesReducer(state: ISectors = SECTOR_COURSES_INITIAL_STATE, action): ISectors {
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  switch (action.type) {
    case SECTORCOURSES_RECEIVED:
        let newSectors = Array<ISector>();
        let i=0;
        action.payload.sectors.forEach(sector => {
            newSectors.push(<ISector>{sector_id: sector.sector_id, sector_name: sector.sector_name, sector_selected: sector.sector_selected, courses: Array<ISectorCourse>() });
            sector.courses.forEach(course => {
                newSectors[i].courses.push(<ISectorCourse>{course_id: course.course_id, course_name: course.course_name, globalIndex: course.globalIndex, selected: course.selected });
            })
            i++;
        });
        return Seq(newSectors).map(n => n).toList();

    case SECTORCOURSES_SELECTED_SAVE:
        let sectorsWithSelections = Array<ISector>();
        let ind=0, j = 0;
        state.forEach(sector => {
29
            sectorsWithSelections.push(<ISector>{sector_id: sector.sector_id, sector_name: sector.sector_name, sector_selected: action.payload.sectorSelected[ind], courses: Array<ISectorCourse>()});
30
31
32
33
34
35
36
            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++;
        });
        return Seq(sectorsWithSelections).map(n => n).toList();
37
    case SECTORCOURSES_INIT:
38
        return SECTOR_COURSES_INITIAL_STATE;
39
40
41
    default: return state;
  }
};