DVA-subscriptions


import { routerRedux } from 'dva/router'
export default {
  namespace: 'notice',
  state: {
    notices:[],
    loading: false,
    editModalVisible: false
  },
  effects: {
    *watchAndRefreshList({ dispatch }, { put, call, take }){
      let listAction = {};
    //關閉彈框后重新獲取數據
    //調用hideModal就會觸發fetch

      while(true){
        const action = yield take(['notice/fetch', 'notice/hideModal']);
        console.log('action', action);
        if(action.type == 'notice/fetch'){
          action.type = 'fetch';
          listAction = action;
        }
        if(action.type == 'notice/hideModal'){
          action.type = 'hideModal';
          dispatch(listAction);
        }
      }
    },
    *fetch({ payload }, { call, put }) {
      const response = yield call(Get, '/api/notices');
      yield put({
        type: 'save',
        payload: response
      })
    },
    *gologin({ payload }, { call, put }) {
      yield put(routerRedux.push('/user/login'))
    }
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        notices: action.payload
      }
    },
    showModal(state, action){
      return {
        ...state,
        editModalVisible: true
      }
    },
    hideModal(state, action){
      return {
        ...state,
        editModalVisible: false,
      }
    },
  },
  subscriptions: {
    //監聽地址,如果地址含有app則跳轉到登陸頁
    setup({ dispatch, history }) {
      history.listen(location => {
        if (location.pathname.includes('app')) {
          dispatch({
            type: 'gologin'
          })
        }
      });
    },
    watchAndRefreshList({ dispatch, history }){
      dispatch({
        type: 'watchAndRefreshList',
        dispatch
      });
    }
  },
};

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM