global.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { queryNotices } from '@/services/user';
  2. export default {
  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(state, { payload }) {
  68. return {
  69. ...state,
  70. collapsed: payload,
  71. };
  72. },
  73. saveNotices(state, { payload }) {
  74. return {
  75. ...state,
  76. notices: payload,
  77. };
  78. },
  79. saveClearedNotices(state, { payload }) {
  80. return {
  81. ...state,
  82. notices: state.notices.filter(item => item.type !== payload),
  83. };
  84. },
  85. },
  86. subscriptions: {
  87. setup({ history }) {
  88. // Subscribe history(url) change, trigger `load` action if pathname is `/`
  89. return history.listen(({ pathname, search }) => {
  90. if (typeof window.ga !== 'undefined') {
  91. window.ga('send', 'pageview', pathname + search);
  92. }
  93. });
  94. },
  95. },
  96. };