From eaf8ab1e74c84cb2a6e09e5ada623bb3cf762180 Mon Sep 17 00:00:00 2001 From: emilkowalski <e@emilkowal.ski> Date: Fri, 10 Nov 2023 02:36:16 +0100 Subject: [PATCH 1/2] Account for fetch in promise --- src/state.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/state.ts b/src/state.ts index 344b7f18..f9c75bee 100644 --- a/src/state.ts +++ b/src/state.ts @@ -103,7 +103,10 @@ class Observer { return this.create({ ...data, type: 'loading', message }); }; - promise = <ToastData>(promise: PromiseT<ToastData>, data?: PromiseData<ToastData>) => { + promise = <ToastData>( + promise: PromiseT<ToastData & { ok?: boolean; status?: string }>, + data?: PromiseData<ToastData>, + ) => { if (!data) { // Nothing to show return; @@ -123,10 +126,15 @@ class Observer { let shouldDismiss = id !== undefined; - p.then((promiseData) => { - if (data.success !== undefined) { + p.then((response) => { + if (response.ok !== undefined && !response.ok) { + shouldDismiss = false; + const message = + 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(promiseData) : data.success; + const message = typeof data.success === 'function' ? data.success(response) : data.success; this.create({ id, type: 'success', message }); } }) From 88c3c95ce8fb425a36e4e391738da035ce2cf835 Mon Sep 17 00:00:00 2001 From: emilkowalski <e@emilkowal.ski> Date: Fri, 10 Nov 2023 03:05:41 +0100 Subject: [PATCH 2/2] Clenaup --- src/state.ts | 10 +++++----- website/src/components/Types/Types.tsx | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/state.ts b/src/state.ts index f9c75bee..89e8df1d 100644 --- a/src/state.ts +++ b/src/state.ts @@ -103,10 +103,7 @@ class Observer { return this.create({ ...data, type: 'loading', message }); }; - promise = <ToastData>( - promise: PromiseT<ToastData & { ok?: boolean; status?: string }>, - data?: PromiseData<ToastData>, - ) => { + promise = <ToastData>(promise: PromiseT<ToastData>, data?: PromiseData<ToastData>) => { if (!data) { // Nothing to show return; @@ -127,9 +124,12 @@ class Observer { let shouldDismiss = id !== undefined; p.then((response) => { - if (response.ok !== undefined && !response.ok) { + // TODO: Clean up TS here, response has incorrect type + // @ts-expect-error + if (response && typeof response.ok === 'boolean' && !response.ok) { shouldDismiss = false; 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) { diff --git a/website/src/components/Types/Types.tsx b/website/src/components/Types/Types.tsx index a3fef381..8f1b0e35 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 }), }, ];