global.js 2.39 KB
Newer Older
ζ„šι“'s avatar
ζ„šι“ committed
1
import { queryNotices } from '@/services/api';
2 3 4 5 6 7 8 9 10 11

export default {
  namespace: 'global',

  state: {
    collapsed: false,
    notices: [],
  },

  effects: {
12
    *fetchNotices(_, { call, put, select }) {
13 14 15
      const data = yield call(queryNotices);
      yield put({
        type: 'saveNotices',
16
        payload: data,
17
      });
18 19 20
      const unreadCount = yield select(
        state => state.global.notices.filter(item => !item.read).length
      );
Xiaoming Liu's avatar
Xiaoming Liu committed
21 22
      yield put({
        type: 'user/changeNotifyCount',
23 24 25 26
        payload: {
          totalCount: data.length,
          unreadCount,
        },
Xiaoming Liu's avatar
Xiaoming Liu committed
27
      });
28
    },
afc163's avatar
afc163 committed
29
    *clearNotices({ payload }, { put, select }) {
30 31 32 33
      yield put({
        type: 'saveClearedNotices',
        payload,
      });
afc163's avatar
afc163 committed
34
      const count = yield select(state => state.global.notices.length);
35 36 37
      const unreadCount = yield select(
        state => state.global.notices.filter(item => !item.read).length
      );
afc163's avatar
afc163 committed
38 39
      yield put({
        type: 'user/changeNotifyCount',
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
        payload: {
          totalCount: count,
          unreadCount,
        },
      });
    },
    *changeNoticeReadState({ payload }, { put, select }) {
      const notices = yield select(state =>
        state.global.notices.map(item => {
          const notice = { ...item };
          if (notice.id === payload) {
            notice.read = true;
          }
          return notice;
        })
      );
      yield put({
        type: 'saveNotices',
        payload: notices,
      });
      yield put({
        type: 'user/changeNotifyCount',
        payload: {
          totalCount: notices.length,
          unreadCount: notices.filter(item => !item.read).length,
        },
afc163's avatar
afc163 committed
66 67
      });
    },
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
  },

  reducers: {
    changeLayoutCollapsed(state, { payload }) {
      return {
        ...state,
        collapsed: payload,
      };
    },
    saveNotices(state, { payload }) {
      return {
        ...state,
        notices: payload,
      };
    },
afc163's avatar
afc163 committed
83
    saveClearedNotices(state, { payload }) {
84 85 86 87 88 89
      return {
        ...state,
        notices: state.notices.filter(item => item.type !== payload),
      };
    },
  },
afc163's avatar
afc163 committed
90 91

  subscriptions: {
xiaohu's avatar
xiaohu committed
92
    setup({ history }) {
afc163's avatar
afc163 committed
93 94 95 96 97 98 99 100
      // Subscribe history(url) change, trigger `load` action if pathname is `/`
      return history.listen(({ pathname, search }) => {
        if (typeof window.ga !== 'undefined') {
          window.ga('send', 'pageview', pathname + search);
        }
      });
    },
  },
101
};