I saw toast implementation in the toolkit docs but it's not what I'd want.
I want to show a snackbar that depends on the current state of async action.
const addUser = createAsyncThunk('users/addUser', async (user: TUser) => {
const { data } = await axios.post(USERS_ENDPOINTS.POST, user);
return data;
}
...
extraReducers: (builder) => {
builder.addCase(addUser.pending, (state, action) => {
// at this stage I want to show to a user "Adding..." snackbar
state.loading = true;
}
builder.addCase(addUser.fulfilled, (state, action) => {
// at this stage I want to show to a user "Added!" snackbar
state.users = [...state.users, action.payload]
state.loading = false;
}
// so on for each async action
}
I can't dispatch anything inside a reducer, so how do I do that?
Do I wrap async action body inside try catch and dispatch snackbar actions here?
const addUser = createAsyncThunk('users/addUser', async (user: TAddUserDto, dispatch) => {
try {
dispatch(pendingSnackbar('Adding user...'));
const { data } = await axios.post<TUser>(USERS_ENDPOINTS.POST, user);
dispatch(fulfilledSnackbar('Added!'));
return data;
} catch (err) {
dispatch(rejectedSnackbar('Error!'));
}
}
I think it would break rejected case in extraReducers.
How can I implement this logic?