Skip to content

An opinionated toast component for React.

Account for fetch in promise (#223)

Emil Kowalski8472d54524d7
diff --git a/src/state.ts b/src/state.ts
index 344b7f1..89e8df1 100644
--- a/src/state.ts
+++ b/src/state.ts
@@ -123,10 +123,18 @@ class Observer {
let shouldDismiss = id !== undefined;
- p.then((promiseData) => {
- if (data.success !== undefined) {
+ p.then((response) => {
+ // TODO: Clean up TS here, response has incorrect type
+ // @ts-expect-error
+ if (response && typeof response.ok === 'boolean' && !response.ok) {
shouldDismiss = false;
- const message = typeof data.success === 'function' ? data.success(promiseData) : data.success;
+ const message =
+ // @ts-expect-error
+ typeof data.error === 'function' ? data.error(`HTTP error! status: ${response.status}`) : data.error;
+ this.create({ id, type: 'error', message });
+ } else if (data.success !== undefined) {
+ shouldDismiss = false;
+ const message = typeof data.success === 'function' ? data.success(response) : data.success;
this.create({ id, type: 'success', message });
}
})
diff --git a/website/src/components/Types/Types.tsx b/website/src/components/Types/Types.tsx
index a3fef38..8f1b0e3 100644
--- a/website/src/components/Types/Types.tsx
+++ b/website/src/components/Types/Types.tsx
@@ -114,6 +114,6 @@ toast.promise(promise, {
{
name: 'Custom',
snippet: `toast(<div>A custom toast with default styling</div>)`,
- action: () => toast(<div>A custom toast with default styling</div>),
+ action: () => toast(<div>A custom toast with default styling</div>, { duration: 1000000 }),
},
];