detail.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { queryOne, create, update } from '../../services/ware';
  2. import { message } from 'antd';
  3. import pathToRegexp from 'path-to-regexp';
  4. import { Codes } from '../../utils/config';
  5. export default {
  6. namespace: 'wareDetail',
  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('/product/ware/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 === '/product/ware/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. // 创建课件,默认状态为NORMAL
  42. const { data, success } = yield call(create, { ...payload, status: Codes.CODE_NORMAL });
  43. if (success) {
  44. message.success('创建成功!');
  45. yield put({ type: 'clearPage' });
  46. if (callback) {
  47. callback();
  48. }
  49. } else {
  50. message.error('创建失败!');
  51. }
  52. },
  53. * update ({ payload, callback }, { call, put }) {
  54. const { data, success } = yield call(update, payload);
  55. if (success) {
  56. message.success('更新成功!');
  57. yield put({ type: 'clearPage' });
  58. if (callback) {
  59. callback();
  60. }
  61. } else {
  62. message.error('更新失败!');
  63. }
  64. }
  65. },
  66. reducers: {
  67. changeLoading(state, { payload }) {
  68. return { ...state, ...payload };
  69. },
  70. querySuccess(state, { payload }) {
  71. return { ...state, currentItem: payload };
  72. },
  73. saveFilters(state, { payload: filters }) {
  74. return { ...state, filters };
  75. },
  76. showModal(state) {
  77. return { ...state, modalVisible: true };
  78. },
  79. hideModal(state) {
  80. return { ...state, modalVisible: false };
  81. },
  82. saveOperType(state, { payload }) {
  83. return { ...state, ...payload };
  84. },
  85. saveSortResult(state, { payload: { resourceList } }) {
  86. const currentItem = { ...state.currentItem, resourceList };
  87. return { ...state, modalVisible: false, currentItem };
  88. },
  89. clearPage(state) {
  90. return { ...state, currentItem: {}, itemLoading: false };
  91. }
  92. }
  93. }