deptmentList.js 2.4 KB
Newer Older
zhenxianyimeng's avatar
zhenxianyimeng committed
1
import { message } from 'antd';
zhenxianyimeng's avatar
zhenxianyimeng committed
2 3 4 5 6 7 8
import {
  deptTreePage,
  deptTreeAll,
  addDeptment,
  updateDeptment,
  deleteDeptment,
} from '../../services/admin';
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

const buildSelectTree = list => {
  return list.map(item => {
    let children = [];
    if (item.children) {
      children = buildSelectTree(item.children);
    }
    return {
      title: item.name,
      value: `${item.name}-${item.id}`,
      key: item.id,
      children,
    };
  });
};
zhenxianyimeng's avatar
zhenxianyimeng committed
24 25 26 27 28 29 30 31 32 33 34 35 36

export default {
  namespace: 'deptmentList',

  state: {
    list: [],
    selectTree: [],
    deptmentData: {
      list: [],
    },
  },

  effects: {
37 38 39 40 41 42 43
    *add({ payload }, { call, put }) {
      const { onSuccess, body } = payload;
      const response = yield call(addDeptment, body);
      if (response && response.code === 0) {
        onSuccess && onSuccess();
      }
    },
zhenxianyimeng's avatar
zhenxianyimeng committed
44 45 46 47 48 49 50
    *delete({ payload }, { call, put }) {
      const { onSuccess, body } = payload;
      const response = yield call(deleteDeptment, body);
      if (response && response.code === 0) {
        onSuccess && onSuccess();
      }
    },
51 52 53 54 55 56 57 58 59 60 61 62 63 64
    *update({ payload }, { call, put }) {
      const { onSuccess, body } = payload;
      const response = yield call(updateDeptment, body);
      if (response && response.code === 0) {
        onSuccess && onSuccess();
      }
    },
    *getDeptmentAll({ payload }, { call, put }) {
      const result = yield call(deptTreeAll, payload);
      yield put({
        type: 'treeSuccess',
        payload: result.data,
      });
    },
zhenxianyimeng's avatar
zhenxianyimeng committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    *getDeptmentList({ payload }, { call, put }) {
      const result = yield call(deptTreePage, payload);
      let deptmentData = {};
      if (result.code === 0) {
        deptmentData = result.data;
      }
      yield put({
        type: 'save',
        payload: {
          deptmentData,
        },
      });
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },

    treeSuccess(state, { payload }) {
      const resultData = payload;
      const treeData = buildSelectTree(resultData);

      // value 要保护 displayName 不然,搜索会失效
      const rootNode = [
        {
          title: '根节点',
          value: `根节点-0`,
          key: 0,
          children: [],
        },
      ];

      const selectTree = rootNode.concat(treeData);
      return {
        ...state,
        list: resultData,
        selectTree,
      };
    },
  },
};