studentdatafields.reducer.ts 1.69 KB
Newer Older
1
import { IStudentDataFieldRecords, IStudentDataFieldRecord, IStudentDataField } from "./studentdatafields.types";
2
import { STUDENT_DATA_FIELDS_INITIAL_STATE } from "./studentdatafields.initial-state";
3 4
import {recordify} from "typed-immutable-record";
import { List } from "immutable";
5
import { STUDENTDATAFIELDS_SAVE, STUDENTDATAFIELDS_INIT } from "../../constants";
6

7
export function studentDataFieldsReducer(state: IStudentDataFieldRecords = STUDENT_DATA_FIELDS_INITIAL_STATE, action): IStudentDataFieldRecords {
8 9
    switch (action.type) {
        case STUDENTDATAFIELDS_SAVE:
10
            let studentDataFields = Array<IStudentDataFieldRecord>();
11

12 13 14 15 16 17 18
            action.payload.studentDataFields.forEach(studentDataField => {
                let transformedDate = "";
                if (studentDataField.studentbirthdate && studentDataField.studentbirthdate.date) {
                    transformedDate = studentDataField.studentbirthdate.date.year + "-";
                    transformedDate += studentDataField.studentbirthdate.date.month < 10 ? "0" + studentDataField.studentbirthdate.date.month + "-" : studentDataField.studentbirthdate.date.month + "-";
                    transformedDate += studentDataField.studentbirthdate.date.day < 10 ? "0" + studentDataField.studentbirthdate.date.day : studentDataField.studentbirthdate.date.day;
                }
19

20
                studentDataField.studentbirthdate = transformedDate;
21
                studentDataFields.push(recordify<IStudentDataField, IStudentDataFieldRecord>(studentDataField));
22
            });
23

24
            return List(studentDataFields);
25 26 27 28
        case STUDENTDATAFIELDS_INIT:
            return STUDENT_DATA_FIELDS_INITIAL_STATE;
        default: return state;
    }
29
};