global.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { queryNotices } from '@/services/user';
  2. const GlobalModel = {
  3. namespace: 'global',
  4. state: {
  5. collapsed: false,
  6. notices: [],
  7. },
  8. effects: {
  9. *fetchNotices(_, { call, put, select }) {
  10. const data = yield call(queryNotices);
  11. yield put({
  12. type: 'saveNotices',
  13. payload: data,
  14. });
  15. const unreadCount = yield select(
  16. (state) => state.global.notices.filter((item) => !item.read).length,
  17. );
  18. yield put({
  19. type: 'user/changeNotifyCount',
  20. payload: {
  21. totalCount: data.length,
  22. unreadCount,
  23. },
  24. });
  25. },
  26. *clearNotices({ payload }, { put, select }) {
  27. yield put({
  28. type: 'saveClearedNotices',
  29. payload,
  30. });
  31. const count = yield select((state) => state.global.notices.length);
  32. const unreadCount = yield select(
  33. (state) => state.global.notices.filter((item) => !item.read).length,
  34. );
  35. yield put({
  36. type: 'user/changeNotifyCount',
  37. payload: {
  38. totalCount: count,
  39. unreadCount,
  40. },
  41. });
  42. },
  43. *changeNoticeReadState({ payload }, { put, select }) {
  44. const notices = yield select((state) =>
  45. state.global.notices.map((item) => {
  46. const notice = { ...item };
  47. if (notice.id === payload) {
  48. notice.read = true;
  49. }
  50. return notice;
  51. }),
  52. );
  53. yield put({
  54. type: 'saveNotices',
  55. payload: notices,
  56. });
  57. yield put({
  58. type: 'user/changeNotifyCount',
  59. payload: {
  60. totalCount: notices.length,
  61. unreadCount: notices.filter((item) => !item.read).length,
  62. },
  63. });
  64. },
  65. },
  66. reducers: {
  67. changeLayoutCollapsed(
  68. state = {
  69. notices: [],
  70. collapsed: true,
  71. },
  72. { payload },
  73. ) {
  74. return { ...state, collapsed: payload };
  75. },
  76. saveNotices(state, { payload }) {
  77. return {
  78. collapsed: false,
  79. ...state,
  80. notices: payload,
  81. };
  82. },
  83. saveClearedNotices(
  84. state = {
  85. notices: [],
  86. collapsed: true,
  87. },
  88. { payload },
  89. ) {
  90. return {
  91. ...state,
  92. collapsed: false,
  93. notices: state.notices.filter((item) => item.type !== payload),
  94. };
  95. },
  96. },
  97. };
  98. export default GlobalModel;