import { message } from 'antd'; import { queryOne, create, update } from '../../services/lesson'; import pathToRegexp from 'path-to-regexp'; import { Codes } from '../../utils/config'; export default { namespace: 'lessonDetail', state: { filters: {}, operType: 'create', currentItem: {}, modalVisible: false, itemLoading: false, }, subscriptions: { setup({ dispatch, history }) { history.listen(({ pathname, state, ...rest }) => { const match = pathToRegexp('/basic-product/lesson/edit/:id').exec(pathname); if (match) { dispatch({ type: 'query', payload: { id: match[1] } }); dispatch({ type: 'saveFilters', payload: state }); dispatch({ type: 'saveOperType', payload: { operType: 'update' } }); } if (pathname === '/basic-product/lesson/add') { dispatch({ type: 'saveFilters', payload: state }); dispatch({ type: 'saveFilters', payload: state }); dispatch({ type: 'saveOperType', payload: { operType: 'create' } }); } }); } }, effects: { * query ({ payload }, { call, put }) { yield put({ type: 'changeLoading', payload: { itemLoading: true } }); const { data, success } = yield call(queryOne, payload); if (success) { yield put({ type: 'querySuccess', payload: data }); } yield put({ type: 'changeLoading', payload: { itemLoading: false } }); }, * create ({ payload, callback }, { call, put }) { const { data, success } = yield call(create, payload); if (success) { message.success('创建成功!'); if (callback) callback(); yield put({ type: 'initState' }); } }, * update ({ payload, callback }, { call, put }) { const { data, success } = yield call(update, payload); if (success) { message.success('修改成功!'); yield put({ type: 'initState' }); if (callback) callback(); } } }, reducers: { changeLoading(state, { payload }) { return { ...state, ...payload }; }, querySuccess(state, { payload }) { return { ...state, currentItem: payload }; }, saveFilters(state, action) { return { ...state, filters: action.payload }; }, showModal(state) { return { ...state, modalVisible: true }; }, hideModal(state) { return { ...state, modalVisible: false }; }, saveOperType(state, { payload }) { return { ...state, ...payload }; }, saveSortResult(state, { payload: { wareList } }) { const currentItem = { ...state.currentItem, wareList }; return { ...state, modalVisible: false, currentItem }; }, initState(state) { return { ...state, currentItem: {}, itemLoading: false }; } } }