global.ts 3.46 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
}

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

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

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

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

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

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

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

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

export default GlobalModel;