user.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { query as queryUsers, queryCurrent } from '../services/user';
  2. export default {
  3. namespace: 'user',
  4. state: {
  5. list: [],
  6. loading: false,
  7. currentUser: {},
  8. },
  9. effects: {
  10. *fetch(_, { call, put }) {
  11. yield put({
  12. type: 'changeLoading',
  13. payload: true,
  14. });
  15. const response = yield call(queryUsers);
  16. yield put({
  17. type: 'save',
  18. payload: response,
  19. });
  20. yield put({
  21. type: 'changeLoading',
  22. payload: false,
  23. });
  24. },
  25. *fetchCurrent(_, { call, put }) {
  26. const response = yield call(queryCurrent);
  27. yield put({
  28. type: 'saveCurrentUser',
  29. payload: response,
  30. });
  31. },
  32. },
  33. reducers: {
  34. save(state, action) {
  35. return {
  36. ...state,
  37. list: action.payload,
  38. };
  39. },
  40. changeLoading(state, action) {
  41. return {
  42. ...state,
  43. loading: action.payload,
  44. };
  45. },
  46. saveCurrentUser(state, action) {
  47. return {
  48. ...state,
  49. currentUser: action.payload,
  50. };
  51. },
  52. changeNotifyCount(state, action) {
  53. return {
  54. ...state,
  55. currentUser: {
  56. ...state.currentUser,
  57. notifyCount: action.payload,
  58. },
  59. };
  60. },
  61. },
  62. };