Skip to content

Commit

Permalink
Account for fetch in promise
Browse files Browse the repository at this point in the history
  • Loading branch information
emilkowalski committed Nov 10, 2023
1 parent 48de91c commit eaf8ab1
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 });
}
})
Expand Down

0 comments on commit eaf8ab1

Please sign in to comment.