I'm using redux-api-middleware for api calls, and redux-sagas for side-effects. For a particular form flow, user can make 2 different changes, that needs to be handled by separate apis. And the whole saga should fail if we one of them fails. I have a separate saga that handles failure. In some cases, this breaks. When we look in the developer tools, the saga seems to run twice, jumping to the initial point before running through the steps again. I've marked this as comments. The action thats triggering the saga is only run once (verified in redux dev tools)
Here's the mock code -
Actions -
function changesAApi() {
return (dispatch, getState) => {
dispatch({
[CALL_API]: {
types: ['changes-A', 'changes-A-success','changes-A-failure'],
endpoint: '/changesA',
method: 'post',
}
})
}
}
// can be called individually, but state in A affects this call
function changesBApi() {
return (dispatch, getState) => {
dispatch({
[CALL_API]: {
types: ['changes-B', 'changes-B-success','changes-B-failure'],
endpoint: '/changesB',
method: 'post',
}
})
}
}
Sagas -
function* saveFormChanges(action) {
// point (1)
if (action.changesA) {
yield put(changesAApi());
// jumps to point (1) the first time
yield take('changes-A-success');
// point (2)
}
if (action.changesB) {
yield put(changesBApi());
// jumps to point (2)
yield take('changes-B-success');
// point (3)
}
// jumps to point (3)
yield call(displaySuccessMessage);
}
function* errorSaga() {
yield takeLatest(action => /failure$/.test(action.type), () => {
alert('failure');
});
}
Is this the right way to create this flow? Is there a better way to do this?