studentdatafields.reducer.ts 1.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
import { List } from "immutable";
import { recordify } from "typed-immutable-record";

import { STUDENTDATAFIELDS_INIT, STUDENTDATAFIELDS_SAVE } from "../../constants";
import { STUDENT_DATA_FIELDS_INITIAL_STATE } from "./studentdatafields.initial-state";
import { IStudentDataField, IStudentDataFieldRecord, IStudentDataFieldRecords } from "./studentdatafields.types";

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

            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;
                }

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

            return List(studentDataFields);
        case STUDENTDATAFIELDS_INIT:
            return STUDENT_DATA_FIELDS_INITIAL_STATE;
        default: return state;
    }
};