I use Redux-Saga in situations where there is complex business logic that is best separated from the component. We are adopting RTK Query and aren't able to use the endpoints manually. Yes, I am aware that Redux best practices docs suggest using Thunk when at all possible.
This particular saga task example doesn't make a great case for using Redux-Saga but there are definitely situations where the business logic is so involved/long/complex that it doesn't belong in a component and where we use saga functionality that can't be (elegantly) emulated with a simple Thunk. In this example I want to make a simple mutation (post request) on the backend:
export function* addNewCustomerTask(action: ReturnType<typeof addNewCustomer>) {
const { name, industry, defaultIdentifierObfuscationLevel } = action.payload
if (!name) {
yield put(setToast({ text: 'Name can not be empty', style: ToastStyle.Error }))
}
else if (!industry) {
yield put(setToast({ text: 'Industry can not be empty', style: ToastStyle.Error }))
}
else if (!defaultIdentifierObfuscationLevel) {
yield put(setToast({ text: 'Default identifier obfuscation level can not be empty', style: ToastStyle.Error }))
}
else {
try {
yield call(authApiEndpoints.addCustomer.initiate, action.payload)
}
catch {
console.error('Error')
}
}
}
The yield call(authApiEndpoints.addCustomer.initiate, action.payload)
statement doesn't do anything.
How do you perform a mutation inside of a Saga?