logininfo.reducer.ts 2.44 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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
import { List } from "immutable";
import { recordify } from "typed-immutable-record";

import { LOGININFO_INIT, LOGININFO_SAVE, PROFILE_SAVE, STATEMENTAGREE_SAVE } from "../../constants";
import { LOGININFO_INITIAL_STATE } from "./logininfo.initial-state";
import { ILoginInfoObj, ILoginInfoRecord, ILoginInfoRecords } from "./logininfo.types";

export function loginInfoReducer(state: ILoginInfoRecords = LOGININFO_INITIAL_STATE, action): ILoginInfoRecords {
    switch (action.type) {
        case LOGININFO_SAVE:

            let loginInfoArray = Array<ILoginInfoRecord>();

            action.payload.loginInfos.forEach(loginInfo => {
                loginInfoArray.push(recordify<ILoginInfoObj, ILoginInfoRecord>({
                    auth_token: loginInfo.auth_token,
                    auth_role: loginInfo.auth_role,
                    cu_name: loginInfo.cu_name,
                    cu_surname: loginInfo.cu_surname,
                    cu_fathername: loginInfo.cu_fathername,
                    cu_mothername: loginInfo.cu_mothername,
                    cu_email: loginInfo.cu_email,
                    minedu_username: loginInfo.minedu_username,
                    minedu_userpassword: loginInfo.minedu_userpassword,
                    lock_capacity: loginInfo.lock_capacity,
                    lock_students: loginInfo.lock_students,
                    lock_application: loginInfo.lock_application,
                    disclaimer_checked: loginInfo.disclaimer_checked,
                    numapp_self: loginInfo.numapp_self,
                    numapp_children: loginInfo.numapp_children,
                    numchildren: loginInfo.numchildren
                }));
        });

        return List(loginInfoArray);

        case PROFILE_SAVE:
            return state.withMutations(function(list) {
                list.setIn([0, "cu_name"], action.payload.profile.userName);
                list.setIn([0, "cu_surname"], action.payload.profile.userSurname);
                list.setIn([0, "cu_fathername"], action.payload.profile.userFathername);
                list.setIn([0, "cu_mothername"], action.payload.profile.userMothername);
            });

        case STATEMENTAGREE_SAVE:
            return state.withMutations(function(list) {
                list.setIn([0, "disclaimer_checked"], action.payload.disclaimer_checked);
            });

        case LOGININFO_INIT:
            return LOGININFO_INITIAL_STATE;
        default:
            return state;
    }
};