global.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { queryNotices } from '../services/notice';
  2. export default {
  3. namespace: 'global',
  4. state: {
  5. collapsed: false,
  6. notices: [],
  7. fetchingNotices: false,
  8. },
  9. effects: {
  10. *fetchNotices(_, { call, put }) {
  11. yield put({
  12. type: 'changeNoticeLoading',
  13. payload: true,
  14. });
  15. const data = yield call(queryNotices);
  16. yield put({
  17. type: 'saveNotices',
  18. payload: data,
  19. });
  20. },
  21. *clearNotices({ payload }, { put, select }) {
  22. const count = yield select(state => state.global.notices.length);
  23. yield put({
  24. type: 'user/changeNotifyCount',
  25. payload: count,
  26. });
  27. yield put({
  28. type: 'saveClearedNotices',
  29. payload,
  30. });
  31. },
  32. },
  33. reducers: {
  34. changeLayoutCollapsed(state, { payload }) {
  35. return {
  36. ...state,
  37. collapsed: payload,
  38. };
  39. },
  40. saveNotices(state, { payload }) {
  41. return {
  42. ...state,
  43. notices: payload,
  44. fetchingNotices: false,
  45. };
  46. },
  47. saveClearedNotices(state, { payload }) {
  48. return {
  49. ...state,
  50. notices: state.notices.filter(item => item.type !== payload),
  51. };
  52. },
  53. changeNoticeLoading(state, { payload }) {
  54. return {
  55. ...state,
  56. fetchingNotices: payload,
  57. };
  58. },
  59. },
  60. subscriptions: {
  61. setup({ history }) {
  62. // Subscribe history(url) change, trigger `load` action if pathname is `/`
  63. return history.listen(({ pathname, search }) => {
  64. if (typeof window.ga !== 'undefined') {
  65. window.ga('send', 'pageview', pathname + search);
  66. }
  67. });
  68. },
  69. },
  70. };