Files
freeCodeCamp/client/src/redux/settings/update-email-saga.js
2018-09-21 11:26:30 +01:00

32 lines
964 B
JavaScript

import { call, put, takeEvery } from 'redux-saga/effects';
import isEmail from 'validator/lib/isEmail';
import { updateMyEmailComplete, updateMyEmailError } from './';
import { createFlashMessage } from '../../components/Flash/redux';
import { putUserUpdateEmail } from '../../utils/ajax';
import reallyWeirdErrorMessage from '../../utils/reallyWeirdErrorMessage';
function* updateMyEmailSaga({ payload: email = '' }) {
if (!email || !isEmail(email)) {
yield put(createFlashMessage(reallyWeirdErrorMessage));
return;
}
try {
const { data: response } = yield call(putUserUpdateEmail, email);
yield put(
updateMyEmailComplete({
...response,
payload: { email, isEmailVerified: false }
})
);
yield put(createFlashMessage(response));
} catch (e) {
yield put(updateMyEmailError(e));
}
}
export function createUpdateMyEmailSaga(types) {
return [takeEvery(types.updateMyEmail, updateMyEmailSaga)];
}