course.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { query, update, remove } from '../../services/product';
  2. import modelExtend from 'dva-model-extend';
  3. import queryString from 'query-string';
  4. import { pageModel } from '../common';
  5. import { pageSize } from '../../utils/config';
  6. import { checkSearchParams } from '../../utils/utils';
  7. import { Codes } from '../../utils/config';
  8. export default modelExtend(pageModel, {
  9. namespace: 'course',
  10. state: {
  11. listLoading: false,
  12. },
  13. subscriptions: {
  14. setup({ dispatch, history }) {
  15. history.listen((location) => {
  16. if (location.pathname === '/product/course') {
  17. const payload = checkSearchParams(queryString.parse(location.search));
  18. dispatch({ type: 'query', payload });
  19. }
  20. });
  21. }
  22. },
  23. effects: {
  24. * query ({ payload = {} }, { call, put }) {
  25. yield put({ type: 'changeLoading', payload: { listLoading: true }});
  26. const { data, success } = yield call(query, { ...payload, type: Codes.CODE_COURSE });
  27. if (success) {
  28. yield put({
  29. type: 'querySuccess',
  30. payload: {
  31. list: data.list,
  32. pagination: {
  33. current: Number(payload.pageNo) || 1,
  34. pageSize: Number(payload.pageSize) || pageSize,
  35. total: data.totalSize,
  36. }
  37. }
  38. });
  39. }
  40. yield put({ type: 'changeLoading', payload: { listLoading: false }});
  41. },
  42. * delete ({ payload, callback }, { call, put }) {
  43. const { data, success } = yield call(remove, payload);
  44. if (success) {
  45. if (callback) callback();
  46. }
  47. },
  48. * recover ({ payload, callback }, { call, put }) {
  49. const { data, success } = yield call(update, payload);
  50. if (success) {
  51. if (callback) callback();
  52. }
  53. },
  54. },
  55. reducers: {
  56. changeLoading(state, action) {
  57. return { ...state, ...action.payload };
  58. },
  59. }
  60. })