From 2a6f9feb5208884a684dba59d1f6f24046a21d2d Mon Sep 17 00:00:00 2001 From: Paul Sanders Date: Thu, 10 Aug 2023 19:40:20 -0400 Subject: [PATCH] Make toast into a shareable component --- .../lib/components/ToastNotification.svelte | 11 +++++++++ frontend/src/lib/stores/stores.ts | 23 +++++++++++++++++++ frontend/src/routes/+layout.svelte | 2 ++ frontend/src/routes/create-goals/+page.svelte | 18 ++------------- 4 files changed, 38 insertions(+), 16 deletions(-) create mode 100644 frontend/src/lib/components/ToastNotification.svelte diff --git a/frontend/src/lib/components/ToastNotification.svelte b/frontend/src/lib/components/ToastNotification.svelte new file mode 100644 index 00000000..e7a1e6be --- /dev/null +++ b/frontend/src/lib/components/ToastNotification.svelte @@ -0,0 +1,11 @@ + + +{#if $showToast} +
+
+ {$toastMessage} +
+
+{/if} diff --git a/frontend/src/lib/stores/stores.ts b/frontend/src/lib/stores/stores.ts index b17e8eca..d6fccd27 100644 --- a/frontend/src/lib/stores/stores.ts +++ b/frontend/src/lib/stores/stores.ts @@ -48,3 +48,26 @@ export const isLoading = writable(false); isLoading.subscribe((value: boolean) => { return value; }); + +export const showToast = writable(false); + +showToast.subscribe((value: boolean) => { + return value; +}); + +export const toastMessage = writable(); + +toastMessage.subscribe((value: string) => { + return value; +}); + +function delay(ms: number) { + return new Promise((resolve) => setTimeout(resolve, ms)); +} + +export async function setToast(message: string, timeout = 5000) { + toastMessage.set(message); + showToast.set(true); + await delay(timeout); + showToast.set(false); +} diff --git a/frontend/src/routes/+layout.svelte b/frontend/src/routes/+layout.svelte index 3c4c90ba..5c3e74db 100644 --- a/frontend/src/routes/+layout.svelte +++ b/frontend/src/routes/+layout.svelte @@ -1,11 +1,13 @@
+