global.ts 3.43 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
  id: string;
  type: string;
10
  status: string;
何乐's avatar
何乐 committed
11
}
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
      yield put({
        type: 'saveNotices',
        payload: notices,
      });
92

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

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

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

export default GlobalModel;