/*
 * @Author: cclu
 * @Date: 2021-12-13 11:01:23
 * @LastEditors: cclu
 * @LastEditTime: 2022-03-23 10:49:21
 * @FilePath: \cloud-platform\src\models\menu.ts
 * @Description:
 *
 * Copyright (c) 2022 by cclu/杭州驿商科技, All Rights Reserved.
 */

import type { Effect, Reducer } from 'umi';
import type { MenuDataItem } from '@ant-design/pro-layout';
import { menuFormatter } from '@/utils/menuformatter';
import session from '@/utils/session';
import { getUserMenu } from '@/services/user';

export interface MenuModelState {
    menuData: MenuDataItem[];
}

export interface MenuModelType {
    namespace: 'menu';
    state: MenuModelState;
    effects: {
        getMenuData: Effect;
    };
    reducers: {
        saveMenuData: Reducer<MenuModelState>;
    };
}

const MenuModel: MenuModelType = {
    namespace: 'menu',
    state: {
        menuData: [],
    },

    effects: {
        *getMenuData({ payload }, { put, call }) {
            const response = yield call(getUserMenu,payload);
            yield put({
                type: 'saveMenuData',
                payload: menuFormatter(response),
            });
        },
    },

    reducers: {
        saveMenuData(state, action) {
            session.set('menu',action.payload)

            return {
                ...state,
                menuData: action.payload ,
            };
        },
    },
};
export default MenuModel;
