regionschools.reducer.ts 3.57 KB
Newer Older
1
import { IRegions, Region, IRegion, IRegionM, RegionSchool, IRegionSchools } from './regionschools.types';
2
import { REGION_SCHOOLS_INITIAL_STATE } from './regionschools.initial-state';
3
import { Seq, Map, fromJS } from 'immutable';
4
5
6

import {
  REGIONSCHOOLS_RECEIVED,
7
  REGIONSCHOOLS_SELECTED_SAVE,
8
9
  REGIONSCHOOLS_ORDER_SAVE,
  REGIONSCHOOLS_INIT
10
11
} from '../../constants';

12
export function regionSchoolsReducer(state: IRegions = REGION_SCHOOLS_INITIAL_STATE, action): IRegions {
13
14
  switch (action.type) {
    case REGIONSCHOOLS_RECEIVED:
15
        let newRegions = Array<Region>();
16
17
        let i=0;
        action.payload.regions.forEach(region => {
18
            newRegions.push(<any>{region_id: region.region_id, region_name: region.region_name, epals: <any>[] });
19
            region.epals.forEach(epal => {
20
                newRegions[i].epals.push(<any>{epal_id: epal.epal_id, epal_name: epal.epal_name, epal_special_case: epal.epal_special_case, globalIndex: epal.globalIndex, selected: epal.selected, order_id: epal.order_id });
21
22
23
            })
            i++;
        });
24
        return fromJS(newRegions);
25
    case REGIONSCHOOLS_SELECTED_SAVE:
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//        let ind=0;
        let region: IRegionM;
        let epals: IRegionSchools;
        region = state.get(action.payload.rIndex);
        console.log(region);

        epals = region.get("epals").epals;
        let epal = epals.get(action.payload.sIndex);
        epal.set("selected", action.payload.checked);
        region = region.set("epals", epals.set(action.payload.sIndex,
            epal.set("selected", action.payload.checked)));
        return state
            .set(action.payload.rIndex, region);

/*        state.forEach(region => {
41
            if (ind === action.payload.rIndex) {
42
43
                let regionCopy = state.get(ind);
                regionCopy.epals[action.payload.sIndex].selected = action.payload.checked;
44
                return state.withMutations(function (list) {
45
                    list.set(ind++, regionCopy);
46
47
                });
            }
48
            ind++;
49
50
        }); */
//        return state;
51

52
53
54
55
56
57
58
59
60
61
    case REGIONSCHOOLS_ORDER_SAVE:
        let ind2=0;
        state.forEach(region => {
            for(let i=0; i<region.epals.length; i++){
                if (region.epals[i].globalIndex === action.payload.selectedSchools[0].globalIndex) {
                    region.epals[i].order_id = action.payload.selectedSchools[0].order_id
                    return state.withMutations(function (list) {
                        list.set(ind2++, region);
                    });
                }
62
63
                if (typeof action.payload.selectedSchools[1] !== 'undefined' &&
                  region.epals[i].globalIndex === action.payload.selectedSchools[1].globalIndex) {
64
65
66
67
68
                    region.epals[i].order_id = action.payload.selectedSchools[1].order_id
                    return state.withMutations(function (list) {
                        list.set(ind2++, region);
                    });
                }
69
70
71
72
73
74
75
76
77
                if (typeof action.payload.selectedSchools[2] !== 'undefined' &&
                    region.epals[i].globalIndex === action.payload.selectedSchools[2].globalIndex) {
                      region.epals[i].order_id = action.payload.selectedSchools[2].order_id
                      return state.withMutations(function (list) {
                          list.set(ind2++, region);
                      });
                  }
                //}

78
79
80
81
82

            }
            ind2++;
        });
        return state;
83

84
85
        case REGIONSCHOOLS_INIT:
            return REGION_SCHOOLS_INITIAL_STATE;
86
87
88
    default: return state;
  }
};