Skip to content

Commit 402ea06

Browse files
committed
Handle navigation and redirect from sagas
1 parent 8e8fcab commit 402ea06

File tree

7 files changed

+42
-39
lines changed

7 files changed

+42
-39
lines changed

login-react/react-login-front-end-app/app/containers/AuthPage/actions.js

-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
ON_CHANGE,
99
SET_FORM,
1010
SUBMIT,
11-
SUBMIT_SUCCEEDED,
1211
} from './constants';
1312

1413
/**
@@ -87,9 +86,3 @@ export function submit() {
8786
type: SUBMIT,
8887
};
8988
}
90-
91-
export function submitSucceeded() {
92-
return {
93-
type: SUBMIT_SUCCEEDED,
94-
};
95-
}

login-react/react-login-front-end-app/app/containers/AuthPage/constants.js

-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,3 @@
77
export const ON_CHANGE = 'app/AuthPage/ON_CHANGE';
88
export const SET_FORM = 'app/AuthPage/SET_FORM';
99
export const SUBMIT = 'app/AuthPage/SUBMIT';
10-
export const SUBMIT_SUCCEEDED = 'app/AuthPage/SUBMIT_SUCCEEDED';

login-react/react-login-front-end-app/app/containers/AuthPage/index.js

-13
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,6 @@ export class AuthPage extends React.Component { // eslint-disable-line react/pre
3737
if (nextProps.match.params.authType !== this.props.match.params.authType) {
3838
this.setForm(nextProps);
3939
}
40-
41-
// Redirect the user to HomePage after login
42-
if (nextProps.submitSuccess) {
43-
switch (this.props.match.params.authType) {
44-
case 'login':
45-
case 'reset-password':
46-
case 'register':
47-
this.props.history.push('/');
48-
break;
49-
default:
50-
// Do nothing
51-
}
52-
}
5340
}
5441

5542
/**

login-react/react-login-front-end-app/app/containers/AuthPage/reducer.js

-5
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ import { fromJS, Map } from 'immutable';
88
import {
99
ON_CHANGE,
1010
SET_FORM,
11-
SUBMIT_SUCCEEDED,
1211
} from './constants';
1312

1413
const initialState = fromJS({
1514
formType: 'login',
1615
modifiedData: Map({}),
17-
submitSuccess: false,
1816
});
1917

2018
function authPageReducer(state = initialState, action) {
@@ -26,9 +24,6 @@ function authPageReducer(state = initialState, action) {
2624
.set('formType', action.formType)
2725
.set('submitSuccess', false)
2826
.set('modifiedData', Map(action.data));
29-
case SUBMIT_SUCCEEDED:
30-
return state
31-
.update('submitSuccess', (v) => !v);
3227
default:
3328
return state;
3429
}

login-react/react-login-front-end-app/app/containers/AuthPage/saga.js

+19-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
import { LOCATION_CHANGE } from 'react-router-redux';
2+
import { all, call, fork, takeLatest, select, take, cancel } from 'redux-saga/effects';
13
import { set } from 'lodash';
2-
import { call, fork, takeLatest, put, select } from 'redux-saga/effects';
4+
import { history } from 'app';
35

46
// Utils
57
import auth from 'utils/auth';
68
import request from 'utils/request';
79

810
import { makeSelectFormType, makeSelectModifiedData } from './selectors';
9-
import { submitSucceeded } from './actions';
1011
import { SUBMIT } from './constants';
1112

1213
export function* submitForm() {
@@ -37,15 +38,27 @@ export function* submitForm() {
3738

3839
if (response.jwt) {
3940
// Set the user's credentials
40-
yield call(auth.setToken, response.jwt, body.rememberMe);
41-
yield call(auth.setUserInfo, response.user, body.rememberMe);
41+
yield all([
42+
call(auth.setToken, response.jwt, body.rememberMe),
43+
call(auth.setUserInfo, response.user, body.rememberMe),
44+
]);
45+
yield call(forwardTo, '/');
4246
}
43-
yield put(submitSucceeded());
4447
} catch(error) {
4548
console.log(error.response.payload.message);
4649
}
4750
}
4851

4952
export default function* defaultSaga() {
50-
yield fork(takeLatest, SUBMIT, submitForm);
53+
const submitWatcher = yield fork(takeLatest, SUBMIT, submitForm);
54+
yield take(LOCATION_CHANGE);
55+
yield cancel(submitWatcher);
56+
}
57+
58+
/**
59+
* Helper to handle navigation from sagas.
60+
* @param {Sting} location The path to navigate
61+
*/
62+
function forwardTo(location) {
63+
history.push(location);
5164
}

login-react/react-login-front-end-app/app/containers/ConnectPage/index.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ export class ConnectPage extends React.Component { // eslint-disable-line react/
2525
this.props.logUser(match.params.provider, location.search);
2626
}
2727
render() {
28-
console.log(this.props);
2928
return (
3029
<div>
3130
Retrieving your token and checking validity
@@ -35,7 +34,7 @@ export class ConnectPage extends React.Component { // eslint-disable-line react/
3534
}
3635

3736
ConnectPage.propTypes = {
38-
// dispatch: PropTypes.func.isRequired,
37+
logUser: PropTypes.func.isRequired,
3938
};
4039

4140
const mapStateToProps = createStructuredSelector({

login-react/react-login-front-end-app/app/containers/ConnectPage/saga.js

+22-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1-
import { take, call, put, select, takeLatest, fork, cancel } from 'redux-saga/effects';
1+
import {
2+
all,
3+
call,
4+
cancel,
5+
fork,
6+
take,
7+
takeLatest,
8+
} from 'redux-saga/effects';
29
import { LOCATION_CHANGE } from 'react-router-redux';
310
import { history } from 'app';
11+
12+
// Utils
413
import auth from 'utils/auth';
514
import request from 'utils/request';
15+
16+
// Constants
617
import { LOG_USER } from './constants';
718

819
export function* login(action) {
@@ -12,8 +23,10 @@ export function* login(action) {
1223

1324
if (response.jwt) {
1425
// Set the user's credentials
15-
yield call(auth.setToken, response.jwt, true);
16-
yield call(auth.setUserInfo, response.user, true);
26+
yield all([
27+
call(auth.setToken, response.jwt, true),
28+
call(auth.setUserInfo, response.user, true),
29+
]);
1730
yield call(forwardTo, '/');
1831
}
1932

@@ -26,9 +39,13 @@ export function* login(action) {
2639
export default function* defaultSaga() {
2740
const loginWatcher = yield fork(takeLatest, LOG_USER, login);
2841
yield take(LOCATION_CHANGE);
29-
yield cancel(loginWatcher)
42+
yield cancel(loginWatcher);
3043
}
3144

32-
function forwardTo (location) {
45+
/**
46+
* Helper to handle navigation from sagas.
47+
* @param {Sting} location The path to navigate
48+
*/
49+
function forwardTo(location) {
3350
history.push(location)
3451
}

0 commit comments

Comments
 (0)