sectorfields.reducer.ts 1.4 KB
Newer Older
1
import { ISectorFieldRecords, ISectorFieldRecord, ISectorField } from "./sectorfields.types";
2
import { SECTOR_FIELDS_INITIAL_STATE } from "./sectorfields.initial-state";
3
import { List } from "immutable";
4
import {recordify} from "typed-immutable-record";
5
6

import {
7
8
9
10
    SECTORFIELDS_RECEIVED,
    SECTORFIELDS_SELECTED_SAVE,
    SECTORFIELDS_INIT
} from "../../constants";
11

12
export function sectorFieldsReducer(state: ISectorFieldRecords = SECTOR_FIELDS_INITIAL_STATE, action): ISectorFieldRecords {
13
14
    switch (action.type) {
        case SECTORFIELDS_RECEIVED:
15
            let newSectorFields = Array<ISectorFieldRecord>();
16
17
            let i = 0;
            action.payload.sectorFields.forEach(sectorField => {
18
                newSectorFields.push(recordify<ISectorField, ISectorFieldRecord>({ id: sectorField.id, name: sectorField.name, selected: false }));
19
20
                i++;
            });
21
            return List(newSectorFields);
22
        case SECTORFIELDS_SELECTED_SAVE:
23
24
25
26
27
            return state.withMutations(function(list) {
                if (action.payload.prevChoice >= 0)
                    list.setIn([action.payload.prevChoice, "selected"], false);
                if (action.payload.newChoice >= 0)
                    list.setIn([action.payload.newChoice, "selected"], true);
28
            });
29

30
31
32
33
        case SECTORFIELDS_INIT:
            return SECTOR_FIELDS_INITIAL_STATE;
        default: return state;
    }
34
};