regionschools.reducer.ts 5.86 KB
Newer Older
1
import { IRegions, Region, IRegion, IRRegion, IRegionM, RegionSchool, IRRegionSchool, IRegionSchools, IRegionSchoolRecord, IRegionSchoolRecords, IRegionRecord } from './regionschools.types';
2
import { REGION_SCHOOLS_INITIAL_STATE } from './regionschools.initial-state';
3
import { Seq, Map, fromJS } from 'immutable';
4
import {recordify} from 'typed-immutable-record';
5 6 7

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

13
export function regionSchoolsReducer(state: IRegionRecord[] = REGION_SCHOOLS_INITIAL_STATE, action): IRegionRecord[] {
14 15
  switch (action.type) {
    case REGIONSCHOOLS_RECEIVED:
16 17 18 19
        let newRegions = Array<IRegionRecord>();
        let i=0, j=0;
        let ii=0;

20 21
        action.payload.regions.forEach(region => {
            region.epals.forEach(epal => {
22 23 24 25 26 27
                if (j !== i || (i === 0 && ii===0)) {
                    newRegions.push(recordify<IRRegion, IRegionRecord>({region_id: region.region_id, region_name: region.region_name, epals: new Array(recordify<IRRegionSchool, IRegionSchoolRecord>({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 })) }));
                    j = i;
                } else
                    newRegions[i].epals.push(recordify<IRRegionSchool, IRegionSchoolRecord>({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 }));
                ii++;
28 29 30
            })
            i++;
        });
31
        return newRegions;
32
    case REGIONSCHOOLS_SELECTED_SAVE:
33
//        let ind=0;
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
        let region: IRegionRecord;

        region = state[action.payload.rIndex];
        let newState = Array<IRegionRecord>();

        i=0, j=0;
        ii=0;
        state.forEach(region => {
            let epals: IRegionSchoolRecords;
            epals = region.get("epals");
            ii=0;
            epals.forEach(epal => {
                if (j !== i || (i === 0 && ii===0)) {
                    if (i===action.payload.rIndex && ii===action.payload.sIndex)
                        newState.push(recordify<IRRegion, IRegionRecord>({region_id: region.region_id, region_name: region.region_name, epals: new Array(recordify<IRRegionSchool, IRegionSchoolRecord>({epal_id: epal.epal_id, epal_name: epal.epal_name, epal_special_case: epal.epal_special_case, globalIndex: epal.globalIndex, selected: action.payload.checked, order_id: epal.order_id })) }));
                    else
                        newState.push(recordify<IRRegion, IRegionRecord>({region_id: region.region_id, region_name: region.region_name, epals: new Array(recordify<IRRegionSchool, IRegionSchoolRecord>({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 })) }));
                    j = i;
                } else {
                    if (i===action.payload.rIndex && ii===action.payload.sIndex)
                        newState[i].epals.push(recordify<IRRegionSchool, IRegionSchoolRecord>({epal_id: epal.epal_id, epal_name: epal.epal_name, epal_special_case: epal.epal_special_case, globalIndex: epal.globalIndex, selected: action.payload.checked, order_id: epal.order_id }));
                    else
                        newState[i].epals.push(recordify<IRRegionSchool, IRegionSchoolRecord>({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 }));
                }
                ii++;
            })
            i++;

        })
63

64
        return newState;
65 66

/*        state.forEach(region => {
67
            if (ind === action.payload.rIndex) {
68 69
                let regionCopy = state.get(ind);
                regionCopy.epals[action.payload.sIndex].selected = action.payload.checked;
70
                return state.withMutations(function (list) {
71
                    list.set(ind++, regionCopy);
72 73
                });
            }
74
            ind++;
75 76
        }); */
//        return state;
77

78
    case REGIONSCHOOLS_ORDER_SAVE:
79 80 81 82
        region = state[action.payload.rIndex];
        let newState2 = Array<IRegionRecord>();

        i=0, j=0;
83 84
        let ind2=0;
        state.forEach(region => {
85 86 87 88 89 90 91 92 93 94
            let epals: IRegionSchoolRecords;
            epals = region.get("epals");
            epals.forEach(epal => {
                let newOrderId = epal.order_id;
                for (let jjj=0; jjj<3; jjj++) {
                    if (typeof action.payload.selectedSchools[jjj] !== 'undefined' &&
                            epal.globalIndex === action.payload.selectedSchools[jjj].globalIndex) {
                        newOrderId = action.payload.selectedSchools[jjj].order_id;
                        break;
                    }
95
                }
96 97 98 99 100
                if (j !== i || i === 0) {
                    newState2.push(recordify<IRRegion, IRegionRecord>({region_id: region.region_id, region_name: region.region_name, epals: new Array(recordify<IRRegionSchool, IRegionSchoolRecord>({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: newOrderId })) }));
                    j = i;
                } else {
                    newState2[i].epals.push(recordify<IRRegionSchool, IRegionSchoolRecord>({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: newOrderId }));
101
                }
102 103
            });
            i++;
104 105

        });
106

107 108 109
        return newState2;
    case REGIONSCHOOLS_INIT:
        return REGION_SCHOOLS_INITIAL_STATE;
110 111 112
    default: return state;
  }
};