import { routerRedux } from 'dva/router'
import { message } from 'antd'

export default {
    namespace: 'main',
    state: {
        currentPage: ''
    },
    reducers: {
        setPage(state, action) {
            return { state, currentPage: action.page || 'home' };
        }
    },
    effects: {
        * redirect (action, { put }) {
            const { path, reload } = action;
            yield put(routerRedux.push(path || '/'));
            if(reload) {
                window.location.reload();
            }
        },
    },
    subscriptions: {
        setup({ dispatch, history }) {
            message.config({
                top: 60,
                duration: 2,
                maxCount: 3,
            });
            return history.listen(({ pathname, query }) => {
                let page = pathname.match(/\/(\w*)/)[1];
                dispatch({ type: 'setPage', page });
            })
        }
    }
};