detail.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { queryOne, create, update } from '../../services/product';
  2. import { message } from 'antd';
  3. import pathToRegexp from 'path-to-regexp';
  4. import { Codes } from '../../utils/config';
  5. export default {
  6. namespace: 'comboDetail',
  7. state: {
  8. filters: {},
  9. operType: 'create',
  10. currentItem: {},
  11. itemLoading: false,
  12. modalShow: false,
  13. },
  14. subscriptions: {
  15. setup({ dispatch, history }) {
  16. history.listen(({ pathname, state, ...rest }) => {
  17. const match = pathToRegexp('/product/package/edit/:pid').exec(pathname);
  18. if (match) {
  19. dispatch({ type: 'query', payload: { pid: match[1] } });
  20. dispatch({ type: 'saveFilters', payload: state });
  21. dispatch({ type: 'saveOperType', payload: { operType: 'update' } });
  22. }
  23. if (pathname === '/product/package/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, { payload: filters }) {
  65. return { ...state, filters };
  66. },
  67. saveOperType(state, { payload }) {
  68. return { ...state, ...payload };
  69. },
  70. saveProducts(state, action) {
  71. return {
  72. ...state,
  73. currentItem: {
  74. ...state.currentItem,
  75. products: [...action.payload],
  76. },
  77. modalShow: false,
  78. };
  79. },
  80. savePrice(state, action) {
  81. return {
  82. ...state,
  83. currentItem: {
  84. ...state.currentItem,
  85. products: [...action.payload],
  86. },
  87. };
  88. },
  89. showModal(state, action) {
  90. return { ...state, ...action.payload, modalShow: true };
  91. },
  92. hideModal(state, action) {
  93. return { ...state, ...action.payload, modalShow: false };
  94. },
  95. initState(state) {
  96. return { ...state, currentItem: {}, itemLoading: false };
  97. }
  98. }
  99. }