global.ts 3.1 KB

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