From ef61f83f74b5d019d575102b208146e1faf2df0d Mon Sep 17 00:00:00 2001 From: PM <12273891+n33pm@users.noreply.github.com> Date: Wed, 4 May 2022 16:50:33 +0200 Subject: [PATCH 1/7] feat(): promise optional success/error msg --- src/core/toast.ts | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/core/toast.ts b/src/core/toast.ts index ec04865..a8c2262 100644 --- a/src/core/toast.ts +++ b/src/core/toast.ts @@ -63,8 +63,8 @@ toast.promise = ( promise: Promise, msgs: { loading: Renderable; - success: ValueOrFunction; - error: ValueOrFunction; + success?: ValueOrFunction; + error?: ValueOrFunction; }, opts?: DefaultToastOptions ) => { @@ -72,19 +72,23 @@ toast.promise = ( promise .then((p) => { - toast.success(resolveValue(msgs.success, p), { - id, - ...opts, - ...opts?.success, - }); + if (msgs.success) + toast.success(resolveValue(msgs.success, p), { + id, + ...opts, + ...opts?.success, + }); + else toast.remove(id); return p; }) .catch((e) => { - toast.error(resolveValue(msgs.error, e), { - id, - ...opts, - ...opts?.error, - }); + if (msgs.error) + toast.error(resolveValue(msgs.error, e), { + id, + ...opts, + ...opts?.error, + }); + else toast.remove(id); }); return promise; From 44fe3af38dc4bdb342d8aae64626660fec929baf Mon Sep 17 00:00:00 2001 From: katsuya_U Date: Sat, 12 Aug 2023 16:27:38 +0900 Subject: [PATCH 2/7] feat: `toast.promise(async () => {})` --- src/core/toast.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/core/toast.ts b/src/core/toast.ts index 8a71eb0..4cd3030 100644 --- a/src/core/toast.ts +++ b/src/core/toast.ts @@ -59,7 +59,7 @@ toast.remove = (toastId?: string) => dispatch({ type: ActionType.REMOVE_TOAST, toastId }); toast.promise = ( - promise: Promise, + promise: Promise | (() => Promise), msgs: { loading: Renderable; success: ValueOrFunction; @@ -69,6 +69,10 @@ toast.promise = ( ) => { const id = toast.loading(msgs.loading, { ...opts, ...opts?.loading }); + if (typeof promise === 'function') { + promise = promise(); + } + promise .then((p) => { toast.success(resolveValue(msgs.success, p), { From 79a3603e20bb59ba2d7bf1bc9ad7568e66dada0d Mon Sep 17 00:00:00 2001 From: katsuya_U Date: Sat, 12 Aug 2023 16:39:49 +0900 Subject: [PATCH 3/7] docs: write `Use Async Function` --- site/pages/docs/toast.mdx | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/site/pages/docs/toast.mdx b/site/pages/docs/toast.mdx index 75910ea..0d976c0 100644 --- a/site/pages/docs/toast.mdx +++ b/site/pages/docs/toast.mdx @@ -123,6 +123,22 @@ toast.promise( ); ``` +#### Use Async Function + +```js +toast.promise( + async () => { + const { id } = await fetchData1(); + await fetchData2(id); + }, + { + loading: 'Loading', + success: 'Got the data', + error: 'Error when fetching', + } +); +``` + ## Default durations Every type has its own duration. You can overwrite them `duration` with the toast options. This can be done per toast options or globally by the [``](/docs/toaster). From 84fdad6faf4c21afb5230da88df414b7b4b082ed Mon Sep 17 00:00:00 2001 From: Andrew Isherwood Date: Tue, 2 Jul 2024 15:03:04 +0100 Subject: [PATCH 4/7] Fix incorrect property name in docs In `toaster.mdx` the property name `theme` is incorrect, it should be `iconTheme`. --- site/pages/docs/toaster.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/site/pages/docs/toaster.mdx b/site/pages/docs/toaster.mdx index fb90689..10540de 100644 --- a/site/pages/docs/toaster.mdx +++ b/site/pages/docs/toaster.mdx @@ -32,7 +32,7 @@ This component will render all toasts. Alternatively you can create own renderer // Default options for specific types success: { duration: 3000, - theme: { + iconTheme: { primary: 'green', secondary: 'black', }, From ea97637878af3eaedfb4ccca90df87ca7a3887ee Mon Sep 17 00:00:00 2001 From: Timo Lins Date: Sat, 21 Dec 2024 11:11:47 +0100 Subject: [PATCH 5/7] Dismiss toast instead of removing --- src/core/toast.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/core/toast.ts b/src/core/toast.ts index 7c3c628..274f721 100644 --- a/src/core/toast.ts +++ b/src/core/toast.ts @@ -71,23 +71,27 @@ toast.promise = ( promise .then((p) => { - if (msgs.success) + if (msgs.success) { toast.success(resolveValue(msgs.success, p), { id, ...opts, ...opts?.success, }); - else toast.remove(id); + } else { + toast.dismiss(id); + } return p; }) .catch((e) => { - if (msgs.error) + if (msgs.error) { toast.error(resolveValue(msgs.error, e), { id, ...opts, ...opts?.error, }); - else toast.remove(id); + } else { + toast.dismiss(id); + } }); return promise; From 952c31477d98244ac219460ecea5812b9fc6a01a Mon Sep 17 00:00:00 2001 From: Timo Lins Date: Sat, 21 Dec 2024 11:16:04 +0100 Subject: [PATCH 6/7] Don't show empty messages in `toast.promise` --- src/core/toast.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/core/toast.ts b/src/core/toast.ts index 274f721..51b8d55 100644 --- a/src/core/toast.ts +++ b/src/core/toast.ts @@ -71,8 +71,12 @@ toast.promise = ( promise .then((p) => { - if (msgs.success) { - toast.success(resolveValue(msgs.success, p), { + const successMessage = msgs.success + ? resolveValue(msgs.success, p) + : undefined; + + if (successMessage) { + toast.success(successMessage, { id, ...opts, ...opts?.success, @@ -83,8 +87,10 @@ toast.promise = ( return p; }) .catch((e) => { - if (msgs.error) { - toast.error(resolveValue(msgs.error, e), { + const errorMessage = msgs.error ? resolveValue(msgs.error, e) : undefined; + + if (errorMessage) { + toast.error(errorMessage, { id, ...opts, ...opts?.error, From 1550a0f1b7c1d95da3c4070a2c5e8db2b2e90874 Mon Sep 17 00:00:00 2001 From: Timo Lins Date: Sat, 21 Dec 2024 12:58:53 +0100 Subject: [PATCH 7/7] Add a description to docs --- site/pages/docs/toast.mdx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/site/pages/docs/toast.mdx b/site/pages/docs/toast.mdx index 0d976c0..b107c0d 100644 --- a/site/pages/docs/toast.mdx +++ b/site/pages/docs/toast.mdx @@ -123,7 +123,9 @@ toast.promise( ); ``` -#### Use Async Function +#### Using an Async Function + +You can also provide a function that returns a promise, which will be called automatically. ```js toast.promise(