Skip to content

Commit

Permalink
feat: description support function in promise (#279)
Browse files Browse the repository at this point in the history
* feat: `description` support function in promise

* fix: data type

* chore: code format
  • Loading branch information
kahosan authored Dec 30, 2023
1 parent 033c427 commit 3ac2386
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
14 changes: 11 additions & 3 deletions src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class Observer {
promise,
type: 'loading',
message: data.loading,
description: typeof data.description !== 'function' ? data.description : undefined,
});
}

Expand All @@ -131,18 +132,25 @@ class Observer {
const message =
// @ts-expect-error
typeof data.error === 'function' ? data.error(`HTTP error! status: ${response.status}`) : data.error;
this.create({ id, type: 'error', message });
const description =
typeof data.description === 'function'
? // @ts-expect-error
data.description(`HTTP error! status: ${response.status}`)
: data.description;
this.create({ id, type: 'error', message, description });
} else if (data.success !== undefined) {
shouldDismiss = false;
const message = typeof data.success === 'function' ? data.success(response) : data.success;
this.create({ id, type: 'success', message });
const description = typeof data.description === 'function' ? data.description(response) : data.description;
this.create({ id, type: 'success', message, description });
}
})
.catch((error) => {
if (data.error !== undefined) {
shouldDismiss = false;
const message = typeof data.error === 'function' ? data.error(error) : data.error;
this.create({ id, type: 'error', message });
const description = typeof data.description === 'function' ? data.description(error) : data.description;
this.create({ id, type: 'error', message, description });
}
})
.finally(() => {
Expand Down
5 changes: 4 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ export type ToastTypes = 'normal' | 'action' | 'success' | 'info' | 'warning' |

export type PromiseT<Data = any> = Promise<Data> | (() => Promise<Data>);

export type PromiseData<ToastData = any> = ExternalToast & {
export type PromiseExternalToast = Omit<ExternalToast, 'description'>;

export type PromiseData<ToastData = any> = PromiseExternalToast & {
loading?: string | React.ReactNode;
success?: string | React.ReactNode | ((data: ToastData) => React.ReactNode | string);
error?: string | React.ReactNode | ((error: any) => React.ReactNode | string);
description?: string | React.ReactNode | ((data: any) => React.ReactNode | string);
finally?: () => void | Promise<void>;
};

Expand Down

1 comment on commit 3ac2386

@vercel
Copy link

@vercel vercel bot commented on 3ac2386 Dec 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.