The problem is that I have several inputs and one button to save the data. I have this method in my component in react:
handleClick(e) {
e.preventDefault();
this.props.newEmail ? this.props.onSaveNewEmail(this.props.newEmail) : null;
this.props.newName ? this.props.onSaveNewName(this.props.newName) : null;
}
And this method is captured in my redux saga:
export function* isName() {
const name = yield select(makeNewNameSelector());
...
// validation name
if (!re.test(name)) {
...
} else {
// ! OK, I CAN UPDATE NAME BECAUSE NEW NAME IS OK
}
}
export function* isEmail() {
const email = yield select(makeNewEmailSelector());
const requestURL = `/api/users/isEmail/${email}`;
...
// validation email
if (!re.test(email)) {
...
} else {
try {
const response = yield call(request, requestURL);
if (!response.isEmail) {
// ! OK, I CAN UPDATE EMAIL BECAUSE IT DOESN'T HAVE IN MY DB MYSQL
}
} catch (err) {
...
}
}
}
// this method send request PUT and this can update data:
function* saveData() {
const name = yield select(makeNewNameSelector());
const email = yield select(makeNewEmailSelector());
...
try {
const response = yield call(request, requestURL, {
method: 'PUT',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: `Bearer ${jwt}`,
},
body: JSON.stringify({
name,
email,
}),
});
if (response.success) {
name ? yield put(enterNewNameSuccessAction()) : null;
email ? yield put(enterNewEmailSuccessAction()) : null;
yield put(saveDataSuccessAction());
}
} catch (err) {
...
}
}
export default function* settingsPageSaga() {
yield takeLatest(ENTER_NEW_NAME, isName);
yield takeLatest(ENTER_NEW_EMAIL, isEmail);
}
And now how should I call my saveData()
method only once?
If in place of the comments I put a yield call(saveData)
, it will work, but this will send a two request! I would like to send only one PUT request.
and if I want to change the e-mail, I have to wait until my code will check if such e-mail exists in the database. if not, then only change action.