From f50867b0712cefdf0811f367f25f51ce65e428b6 Mon Sep 17 00:00:00 2001 From: afc163 Date: Fri, 3 Nov 2017 19:33:53 +0800 Subject: [PATCH] Better dispatch and saga for login logic --- src/layouts/BasicLayout.js | 8 +------- src/models/login.js | 25 ++++++++++--------------- 2 files changed, 11 insertions(+), 22 deletions(-) diff --git a/src/layouts/BasicLayout.js b/src/layouts/BasicLayout.js index a41b44d5..c575df3a 100644 --- a/src/layouts/BasicLayout.js +++ b/src/layouts/BasicLayout.js @@ -3,7 +3,7 @@ import PropTypes from 'prop-types'; import { Layout, Menu, Icon, Avatar, Dropdown, Tag, message, Spin } from 'antd'; import DocumentTitle from 'react-document-title'; import { connect } from 'dva'; -import { Link, routerRedux, Route, Redirect, Switch } from 'dva/router'; +import { Link, Route, Redirect, Switch } from 'dva/router'; import moment from 'moment'; import groupBy from 'lodash/groupBy'; import { ContainerQuery } from 'react-container-query'; @@ -80,12 +80,6 @@ class BasicLayout extends React.PureComponent { if (key === 'logout') { this.props.dispatch({ type: 'login/logout', - payload: { - status: false, - }, - callback: () => { - this.props.dispatch(routerRedux.push('/user/login')); - }, }); } } diff --git a/src/models/login.js b/src/models/login.js index 81d60252..5b130336 100644 --- a/src/models/login.js +++ b/src/models/login.js @@ -1,3 +1,4 @@ +import { routerRedux } from 'dva/router'; import { fakeAccountLogin, fakeMobileLogin } from '../services/api'; export default { @@ -15,7 +16,7 @@ export default { }); const response = yield call(fakeAccountLogin, payload); yield put({ - type: 'loginHandle', + type: 'changeLoginStatus', payload: response, }); yield put({ @@ -30,7 +31,7 @@ export default { }); const response = yield call(fakeMobileLogin); yield put({ - type: 'loginHandle', + type: 'changeLoginStatus', payload: response, }); yield put({ @@ -38,19 +39,19 @@ export default { payload: false, }); }, - *logout({ payload, callback }, { put }) { + *logout(_, { put }) { yield put({ - type: 'logoutHandle', - payload, + type: 'changeLoginStatus', + payload: { + status: false, + }, }); - if (callback) { - callback(); - } + yield put(routerRedux.push('/user/login')); }, }, reducers: { - loginHandle(state, { payload }) { + changeLoginStatus(state, { payload }) { return { ...state, status: payload.status, @@ -63,11 +64,5 @@ export default { submitting: payload, }; }, - logoutHandle(state, { payload }) { - return { - ...state, - status: payload.status, - }; - }, }, }; -- GitLab