global.ts 3.29 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
import { Effect } from './connect';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
5
import { NoticeIconData } from '@/components/NoticeIcon';
何乐's avatar
何乐 committed
6

7
export interface NoticeItem extends NoticeIconData {
何乐's avatar
何乐 committed
8 9 10 11
  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
  },

  reducers: {
    changeLayoutCollapsed(state, { payload }) {
      return {
        ...state,
        collapsed: payload,
      };
    },
    saveNotices(state, { payload }) {
      return {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
110
        collapsed: false,
111 112 113 114
        ...state,
        notices: payload,
      };
    },
afc163's avatar
afc163 committed
115
    saveClearedNotices(state, { payload }) {
116
      return {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
117
        collapsed: false,
118 119 120 121 122
        ...state,
        notices: state.notices.filter(item => item.type !== payload),
      };
    },
  },
afc163's avatar
afc163 committed
123 124

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

export default GlobalModel;