course.js 1.7 KB

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