global.ts 3.26 KB
Newer Older
1
import { queryNotices } from '@/services/user';
何乐's avatar
何乐 committed
2
import { Subscription } from 'dva';
3
import { Reducer } from 'redux';
何乐's avatar
何乐 committed
4 5 6 7 8 9 10 11
import { Effect } from './connect';
import { INoticeIconData } from 'ant-design-pro/lib/NoticeIcon/NoticeIconTab';

export interface NoticeItem extends INoticeIconData {
  id: string;
  type: string;
  [key: string]: any;
}
12

13 14
export interface GlobalModelState {
  collapsed: boolean;
何乐's avatar
何乐 committed
15
  notices: NoticeItem[];
16 17 18 19 20 21 22 23 24 25 26
}

export interface GlobalModelType {
  namespace: 'global';
  state: GlobalModelState;
  effects: {
    fetchNotices: Effect;
    clearNotices: Effect;
    changeNoticeReadState: Effect;
  };
  reducers: {
何乐's avatar
何乐 committed
27 28 29
    changeLayoutCollapsed: Reducer<GlobalModelState>;
    saveNotices: Reducer<GlobalModelState>;
    saveClearedNotices: Reducer<GlobalModelState>;
30 31 32 33 34
  };
  subscriptions: { setup: Subscription };
}

const GlobalModel: GlobalModelType = {
35 36 37 38 39 40 41 42
  namespace: 'global',

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

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

  reducers: {
    changeLayoutCollapsed(state, { payload }) {
      return {
        ...state,
        collapsed: payload,
      };
    },
    saveNotices(state, { payload }) {
      return {
        ...state,
        notices: payload,
      };
    },
afc163's avatar
afc163 committed
114
    saveClearedNotices(state, { payload }) {
115 116 117 118 119 120
      return {
        ...state,
        notices: state.notices.filter(item => item.type !== payload),
      };
    },
  },
afc163's avatar
afc163 committed
121 122

  subscriptions: {
xiaohu's avatar
xiaohu committed
123
    setup({ history }) {
afc163's avatar
afc163 committed
124 125
      // Subscribe history(url) change, trigger `load` action if pathname is `/`
      return history.listen(({ pathname, search }) => {
126 127
        if (typeof (window as any).ga !== 'undefined') {
          (window as any).ga('send', 'pageview', pathname + search);
afc163's avatar
afc163 committed
128 129 130 131
        }
      });
    },
  },
132
};
133 134

export default GlobalModel;