Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Toast Promise - In the API request created with RTK async thunk, success toast runs even though the HTTP request failed. #215

Closed
fulutas opened this issue Nov 1, 2023 · 3 comments

Comments

@fulutas
Copy link

fulutas commented Nov 1, 2023

I use sonner toast in react projects with great pleasure. But there is a problem with using promises.

The code below runs success toast even though the HTTP request failed.

COMPONENT SUBMIT CODE:
toast.promise(dispatch(AnonymousSaveIssue(postData)).then(res => {
if (res.meta.requestStatus === "fulfilled") {
return navigate(/issue-result/${res.payload.issueID}?BusinessChannelID=${PAGESTATE.BusinessChannelId}, { state: { issueId: res.payload.issueID } })
}
}), {
loading: 'Saving notification...',
success: (data) => {
return Notification created successfully.;
},
error: 'Error',
});

ASYNCTHUNK in RTK SLICE:
export const AnonymousSaveIssue = createAsyncThunk('AnonymousSaveIssue/POST', async (postData) => {
return new Promise(async (resolve, reject) => {
try {
const response = await http://request.post(${endpoints.issueEntry.anonymousSaveIssue}?businessChannelId=${PAGESTATE.BusinessChannelId}, postData);
if (response.status === 200) {
resolve(http://response.data);
} else {
reject({ status: response.status, message: 'HTTP response status is not 200.' });
}
} catch (error) {
console.log(error)
reject(error);
}
});
});

@fulutas fulutas changed the title In the API request created with RTK async thunk, success toast runs even though the HTTP request failed. Toast Promise - In the API request created with RTK async thunk, success toast runs even though the HTTP request failed. Nov 1, 2023
@emilkowalski
Copy link
Owner

Please, provide a codesandbox demo with reproduction.

@constDEFE
Copy link

Not sure about rtk, but even with the fetch api, it doesn't seem to handle a 404 code as an error
Here's a sandbox

@emilkowalski
Copy link
Owner

Fixed in #223

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants