Skip to content

Commit

Permalink
Better dispatch and saga for login logic
Browse files Browse the repository at this point in the history
  • Loading branch information
afc163 committed Nov 3, 2017
1 parent 41fffed commit f50867b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 22 deletions.
8 changes: 1 addition & 7 deletions src/layouts/BasicLayout.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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'));
},
});
}
}
Expand Down
25 changes: 10 additions & 15 deletions src/models/login.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { routerRedux } from 'dva/router';
import { fakeAccountLogin, fakeMobileLogin } from '../services/api';

export default {
Expand All @@ -15,7 +16,7 @@ export default {
});
const response = yield call(fakeAccountLogin, payload);
yield put({
type: 'loginHandle',
type: 'changeLoginStatus',
payload: response,
});
yield put({
Expand All @@ -30,27 +31,27 @@ export default {
});
const response = yield call(fakeMobileLogin);
yield put({
type: 'loginHandle',
type: 'changeLoginStatus',
payload: response,
});
yield put({
type: 'changeSubmitting',
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,
Expand All @@ -63,11 +64,5 @@ export default {
submitting: payload,
};
},
logoutHandle(state, { payload }) {
return {
...state,
status: payload.status,
};
},
},
};

0 comments on commit f50867b

Please sign in to comment.