2017-11-14 14:27:16 +01:00
|
|
|
import { call, fork, takeLatest, put, select } from 'redux-saga/effects';
|
|
|
|
import auth from 'utils/auth';
|
|
|
|
import request from 'utils/request';
|
2017-11-10 14:20:33 +01:00
|
|
|
|
2017-11-14 14:27:16 +01:00
|
|
|
import { makeSelectFormType, makeSelectModifiedData } from './selectors';
|
2017-11-10 14:20:33 +01:00
|
|
|
import { submitSucceeded } from './actions';
|
|
|
|
import { SUBMIT } from './constants';
|
|
|
|
|
|
|
|
export function* submitForm() {
|
|
|
|
try {
|
2017-11-14 14:27:16 +01:00
|
|
|
const formType = yield select(makeSelectFormType());
|
|
|
|
const body = yield select(makeSelectModifiedData());
|
|
|
|
|
2017-11-16 15:51:12 +01:00
|
|
|
if (formType === 'login' || formType === 'register') {
|
|
|
|
const endPoint = formType === 'login' ? '' : '/register';
|
|
|
|
const response = yield call(request, `/auth/local${endPoint}`, { method: 'POST', body });
|
2017-11-10 14:20:33 +01:00
|
|
|
|
2017-11-14 14:27:16 +01:00
|
|
|
if (response.jwt) {
|
|
|
|
yield call(auth.setToken, response.jwt, body.rememberMe);
|
|
|
|
yield call(auth.setUserInfo, response.user, body.rememberMe);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
yield put(submitSucceeded());
|
2017-11-10 14:20:33 +01:00
|
|
|
} catch(error) {
|
2017-11-17 17:22:41 +01:00
|
|
|
strapi.notification.error('An error occured');
|
2017-11-10 14:20:33 +01:00
|
|
|
}
|
|
|
|
}
|
2017-11-09 17:36:07 +01:00
|
|
|
|
|
|
|
export default function* defaultSaga() {
|
2017-11-10 14:20:33 +01:00
|
|
|
yield fork(takeLatest, SUBMIT, submitForm);
|
2017-11-09 17:36:07 +01:00
|
|
|
}
|