import { queryOne, create, update } from '../../services/ware'; import pathToRegexp from 'path-to-regexp'; import { Codes } from '../../utils/config'; export default { namespace: 'wareDetail', state: { filters: {}, operType: 'create', currentItem: {}, modalVisible: false, itemLoading: false, }, subscriptions: { setup({ dispatch, history }) { history.listen(({ pathname, state, ...rest }) => { const match = pathToRegexp('/basic-product/ware/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/ware/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, status: Codes.CODE_NORMAL }); if (success) { yield put({ type: 'clearPage' }); if (callback) callback(); } }, * update ({ payload, callback }, { call, put }) { const { data, success } = yield call(update, payload); if (success) { yield put({ type: 'clearPage' }); if (callback) callback(); } } }, reducers: { changeLoading(state, { payload }) { return { ...state, ...payload }; }, querySuccess(state, { payload }) { return { ...state, currentItem: payload }; }, saveFilters(state, { payload: filters }) { return { ...state, filters }; }, showModal(state) { return { ...state, modalVisible: true }; }, hideModal(state) { return { ...state, modalVisible: false }; }, saveOperType(state, { payload }) { return { ...state, ...payload }; }, saveSortResult(state, { payload: { resourceList } }) { const currentItem = { ...state.currentItem, resourceList }; return { ...state, modalVisible: false, currentItem }; }, clearPage(state) { return { ...state, currentItem: {}, itemLoading: false }; } } }