detail.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { message } from 'antd';
  2. import { queryOne, create, update } from '../../services/lesson';
  3. import pathToRegexp from 'path-to-regexp';
  4. import { Codes } from '../../utils/config';
  5. export default {
  6. namespace: 'lessonDetail',
  7. state: {
  8. filters: {},
  9. operType: 'create',
  10. currentItem: {},
  11. modalVisible: false,
  12. itemLoading: false,
  13. },
  14. subscriptions: {
  15. setup({ dispatch, history }) {
  16. history.listen(({ pathname, state, ...rest }) => {
  17. const match = pathToRegexp('/basic-product/lesson/edit/:id').exec(pathname);
  18. if (match) {
  19. dispatch({ type: 'query', payload: { id: match[1] } });
  20. dispatch({ type: 'saveFilters', payload: state });
  21. dispatch({ type: 'saveOperType', payload: { operType: 'update' } });
  22. }
  23. if (pathname === '/basic-product/lesson/add') {
  24. dispatch({ type: 'saveFilters', payload: state });
  25. dispatch({ type: 'saveFilters', payload: state });
  26. dispatch({ type: 'saveOperType', payload: { operType: 'create' } });
  27. }
  28. });
  29. }
  30. },
  31. effects: {
  32. * query ({ payload }, { call, put }) {
  33. yield put({ type: 'changeLoading', payload: { itemLoading: true } });
  34. const { data, success } = yield call(queryOne, payload);
  35. if (success) {
  36. yield put({ type: 'querySuccess', payload: data });
  37. }
  38. yield put({ type: 'changeLoading', payload: { itemLoading: false } });
  39. },
  40. * create ({ payload, callback }, { call, put }) {
  41. const { data, success } = yield call(create, payload);
  42. if (success) {
  43. message.success('创建成功!');
  44. if (callback) callback();
  45. yield put({ type: 'initState' });
  46. }
  47. },
  48. * update ({ payload, callback }, { call, put }) {
  49. const { data, success } = yield call(update, payload);
  50. if (success) {
  51. message.success('修改成功!');
  52. yield put({ type: 'initState' });
  53. if (callback) callback();
  54. }
  55. }
  56. },
  57. reducers: {
  58. changeLoading(state, { payload }) {
  59. return { ...state, ...payload };
  60. },
  61. querySuccess(state, { payload }) {
  62. return { ...state, currentItem: payload };
  63. },
  64. saveFilters(state, action) {
  65. return { ...state, filters: action.payload };
  66. },
  67. showModal(state) {
  68. return { ...state, modalVisible: true };
  69. },
  70. hideModal(state) {
  71. return { ...state, modalVisible: false };
  72. },
  73. saveOperType(state, { payload }) {
  74. return { ...state, ...payload };
  75. },
  76. saveSortResult(state, { payload: { wareList } }) {
  77. const currentItem = { ...state.currentItem, wareList };
  78. return { ...state, modalVisible: false, currentItem };
  79. },
  80. initState(state) {
  81. return { ...state, currentItem: {}, itemLoading: false };
  82. }
  83. }
  84. }