global.ts 2.97 KB
Newer Older
1
import { queryNotices } from '@/services/user';
2 3
import { Effect, Subscription } from 'dva';
import { Reducer } from 'redux';
4

5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
export interface GlobalModelState {
  collapsed: boolean;
  notices: any[];
}

export interface GlobalModelType {
  namespace: 'global';
  state: GlobalModelState;
  effects: {
    fetchNotices: Effect;
    clearNotices: Effect;
    changeNoticeReadState: Effect;
  };
  reducers: {
    changeLayoutCollapsed: Reducer<any>;
    saveNotices: Reducer<any>;
    saveClearedNotices: Reducer<any>;
  };
  subscriptions: { setup: Subscription };
}

const GlobalModel: GlobalModelType = {
27 28 29 30 31 32 33 34
  namespace: 'global',

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

  effects: {
35
    *fetchNotices(_, { call, put, select }) {
36 37 38
      const data = yield call(queryNotices);
      yield put({
        type: 'saveNotices',
Yu's avatar
Yu committed
39
        payload: data,
40
      });
41 42 43
      const unreadCount = yield select(
        state => state.global.notices.filter(item => !item.read).length
      );
Xiaoming Liu's avatar
Xiaoming Liu committed
44 45
      yield put({
        type: 'user/changeNotifyCount',
46 47 48 49
        payload: {
          totalCount: data.length,
          unreadCount,
        },
Xiaoming Liu's avatar
Xiaoming Liu committed
50
      });
51
    },
afc163's avatar
afc163 committed
52
    *clearNotices({ payload }, { put, select }) {
53 54 55 56
      yield put({
        type: 'saveClearedNotices',
        payload,
      });
afc163's avatar
afc163 committed
57
      const count = yield select(state => state.global.notices.length);
58 59 60
      const unreadCount = yield select(
        state => state.global.notices.filter(item => !item.read).length
      );
afc163's avatar
afc163 committed
61 62
      yield put({
        type: 'user/changeNotifyCount',
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
        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
89 90
      });
    },
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  },

  reducers: {
    changeLayoutCollapsed(state, { payload }) {
      return {
        ...state,
        collapsed: payload,
      };
    },
    saveNotices(state, { payload }) {
      return {
        ...state,
        notices: payload,
      };
    },
afc163's avatar
afc163 committed
106
    saveClearedNotices(state, { payload }) {
107 108 109 110 111 112
      return {
        ...state,
        notices: state.notices.filter(item => item.type !== payload),
      };
    },
  },
afc163's avatar
afc163 committed
113 114

  subscriptions: {
xiaohu's avatar
xiaohu committed
115
    setup({ history }) {
afc163's avatar
afc163 committed
116 117
      // Subscribe history(url) change, trigger `load` action if pathname is `/`
      return history.listen(({ pathname, search }) => {
118 119
        if (typeof (window as any).ga !== 'undefined') {
          (window as any).ga('send', 'pageview', pathname + search);
afc163's avatar
afc163 committed
120 121 122 123
        }
      });
    },
  },
124
};
125 126

export default GlobalModel;