global.ts 3.41 KB
Newer Older
1
import { Effect } from './connect.d';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
2
import { NoticeIconData } from '@/components/NoticeIcon';
3 4 5
import { Reducer } from 'redux';
import { Subscription } from 'dva';
import { queryNotices } from '@/services/user';
何乐's avatar
何乐 committed
6

7
export interface NoticeItem extends NoticeIconData {
何乐's avatar
何乐 committed
8 9 10
  id: string;
  type: string;
}
11

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

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

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

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

  effects: {
42
    *fetchNotices(_, { call, put, select }) {
43 44 45
      const data = yield call(queryNotices);
      yield put({
        type: 'saveNotices',
Yu's avatar
Yu committed
46
        payload: data,
47
      });
何乐's avatar
何乐 committed
48 49
      const unreadCount: number = yield select(
        state => state.global.notices.filter(item => !item.read).length,
50
      );
Xiaoming Liu's avatar
Xiaoming Liu committed
51 52
      yield put({
        type: 'user/changeNotifyCount',
53 54 55 56
        payload: {
          totalCount: data.length,
          unreadCount,
        },
Xiaoming Liu's avatar
Xiaoming Liu committed
57
      });
58
    },
afc163's avatar
afc163 committed
59
    *clearNotices({ payload }, { put, select }) {
60 61 62 63
      yield put({
        type: 'saveClearedNotices',
        payload,
      });
何乐's avatar
何乐 committed
64 65 66
      const count: number = yield select(state => state.global.notices.length);
      const unreadCount: number = yield select(
        state => state.global.notices.filter(item => !item.read).length,
67
      );
afc163's avatar
afc163 committed
68 69
      yield put({
        type: 'user/changeNotifyCount',
70 71 72 73 74 75 76
        payload: {
          totalCount: count,
          unreadCount,
        },
      });
    },
    *changeNoticeReadState({ payload }, { put, select }) {
何乐's avatar
何乐 committed
77
      const notices: NoticeItem[] = yield select(state =>
78 79 80 81 82 83
        state.global.notices.map(item => {
          const notice = { ...item };
          if (notice.id === payload) {
            notice.read = true;
          }
          return notice;
何乐's avatar
何乐 committed
84
        }),
85
      );
86

87 88 89 90
      yield put({
        type: 'saveNotices',
        payload: notices,
      });
91

92 93 94 95 96 97
      yield put({
        type: 'user/changeNotifyCount',
        payload: {
          totalCount: notices.length,
          unreadCount: notices.filter(item => !item.read).length,
        },
afc163's avatar
afc163 committed
98 99
      });
    },
100 101 102
  },

  reducers: {
103
    changeLayoutCollapsed(state = { notices: [], collapsed: true }, { payload }): GlobalModelState {
104 105 106 107 108
      return {
        ...state,
        collapsed: payload,
      };
    },
109
    saveNotices(state, { payload }): GlobalModelState {
110
      return {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
111
        collapsed: false,
112 113 114 115
        ...state,
        notices: payload,
      };
    },
116
    saveClearedNotices(state = { notices: [], collapsed: true }, { payload }): GlobalModelState {
117
      return {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
118
        collapsed: false,
119
        ...state,
120
        notices: state.notices.filter((item): boolean => item.type !== payload),
121 122 123
      };
    },
  },
afc163's avatar
afc163 committed
124 125

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

export default GlobalModel;