import { queryOne, create, update } from '../../services/product'; import { message } from 'antd'; import pathToRegexp from 'path-to-regexp'; import { Codes } from '../../utils/config'; export default { namespace: 'comboDetail', state: { filters: {}, operType: 'create', currentItem: {}, itemLoading: false, modalShow: false, }, subscriptions: { setup({ dispatch, history }) { history.listen(({ pathname, state, ...rest }) => { const match = pathToRegexp('/product/package/edit/:pid').exec(pathname); if (match) { dispatch({ type: 'query', payload: { pid: match[1] } }); dispatch({ type: 'saveFilters', payload: state }); dispatch({ type: 'saveOperType', payload: { operType: 'update' } }); } if (pathname === '/product/package/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, { payload: filters }) { return { ...state, filters }; }, saveOperType(state, { payload }) { return { ...state, ...payload }; }, saveProducts(state, action) { return { ...state, currentItem: { ...state.currentItem, products: [...action.payload], }, modalShow: false, }; }, savePrice(state, action) { return { ...state, currentItem: { ...state.currentItem, products: [...action.payload], }, }; }, showModal(state, action) { return { ...state, ...action.payload, modalShow: true }; }, hideModal(state, action) { return { ...state, ...action.payload, modalShow: false }; }, initState(state) { return { ...state, currentItem: {}, itemLoading: false }; } } }