I have Action in Redux. Where I make request to backEnd to do something. And I need to check if Post request whichIi made was successful or no.
export const changeEmployee = (id, status) => {
return async (dispatch, getState) => {
try {
dispatch(changeEmployeeRequest());
await adminAPI.changeEmployee(id, status);
dispatch(updateUserWithRolesSuccess(usersWithRoles))
} catch (error) {
dispatch(updateUserWithRolesFailure());
}
}
}
Unfortunately I do not receive any data from this request. So I cannot check it works. But I was curious is there are any ways to get status of request. As I can see this status in Chrome's Network tab.
And this is my API, which is in separate APIs page.
export const changeEmployee = (id, status) => API.post(`${UPDATE_EMPLOYEE}`, {id, status});
You should be able to store the response as a value within your function and then check the status of that
const res = await adminAPI.changeEmployee(id, status)
if(res.status === 200) {...}
As said above, it looks like you are using axios which should throw an error and what will be handled in the catch block. If you have got different outcomes for different errors you can add logic based on the error status code, similar to this:
catch (err) {
switch (err.response.status) {
case 401:
...
break
case 400:
...
break
default:
...
break
}
}