import * as service from '../services/operation'; export default { namespace: 'paramOperation', state: { list: [], pageData: { pageSize: 10, pageNum: 1, total: 0, current: 1, }, }, reducers: { save( state, { payload: { records: list, pageSize, pageNum, total, current }, }, ) { list.forEach(item => { // eslint-disable-next-line no-param-reassign item.key = item.paramId; }); return { ...state, list: [...list], pageData: { pageSize, pageNum, total, current }, }; }, setList( state, { payload: { list }, }, ) { return { ...state, list: [...list] }; }, }, effects: { // 获取列表数据 *getList({ payload }, { call, put }) { const { data } = yield call(service.getList, payload); yield put({ type: 'save', payload: { ...data }, }); }, // 增加 *add({ payload }, { call }) { const { code } = yield call(service.add, payload); return code === 'sys.success'; }, // 更改 *update({ payload }, { call, put, select }) { const { code } = yield call(service.update, payload); const result = code === 'sys.success'; if (result) { const list = yield select(({ paramOperation }) => paramOperation.list); const index = list.findIndex(element => payload.key === element.key); if (index > -1) { list[index] = { ...list[index], ...payload }; } yield put({ type: 'changeList', payload: { list }, }); } return result; }, // 删除 *remove({ payload }, { call }) { const { code } = yield call(service.remove, payload); return code === 'sys.success'; }, *exportParam({ params }, { call }) { yield call(service.exportParam, { ...params }); }, }, subscriptions: { // setup({ dispatch, history }) { // return history.listen(({ pathname, query, hash }) => { // // 初始化数据加载 也可以在componentDidMount中 // if (pathname === "/parameter" && (hash === "#operation" || !hash)) { // dispatch({ type: "getList", payload: query }); // } // }); // }, }, };