|
| 1 | +import { LOCATION_CHANGE } from 'react-router-redux'; |
| 2 | +import { all, call, fork, takeLatest, select, take, cancel } from 'redux-saga/effects'; |
1 | 3 | import { set } from 'lodash';
|
2 |
| -import { call, fork, takeLatest, put, select } from 'redux-saga/effects'; |
| 4 | +import { history } from 'app'; |
3 | 5 |
|
4 | 6 | // Utils
|
5 | 7 | import auth from 'utils/auth';
|
6 | 8 | import request from 'utils/request';
|
7 | 9 |
|
8 | 10 | import { makeSelectFormType, makeSelectModifiedData } from './selectors';
|
9 |
| -import { submitSucceeded } from './actions'; |
10 | 11 | import { SUBMIT } from './constants';
|
11 | 12 |
|
12 | 13 | export function* submitForm() {
|
@@ -37,15 +38,27 @@ export function* submitForm() {
|
37 | 38 |
|
38 | 39 | if (response.jwt) {
|
39 | 40 | // 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, '/'); |
42 | 46 | }
|
43 |
| - yield put(submitSucceeded()); |
44 | 47 | } catch(error) {
|
45 | 48 | console.log(error.response.payload.message);
|
46 | 49 | }
|
47 | 50 | }
|
48 | 51 |
|
49 | 52 | 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); |
51 | 64 | }
|
0 commit comments