Skip to content

Commit

Permalink
Make toast into a shareable component
Browse files Browse the repository at this point in the history
  • Loading branch information
sanders41 committed Aug 10, 2023
1 parent a98fb5f commit 2a6f9fe
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 16 deletions.
11 changes: 11 additions & 0 deletions frontend/src/lib/components/ToastNotification.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script lang="ts">
import { showToast, toastMessage } from '$lib/stores/stores';
</script>

{#if $showToast}
<div class="toast" id="toast-message">
<div class="alert alert-info">
<span>{$toastMessage}</span>
</div>
</div>
{/if}
23 changes: 23 additions & 0 deletions frontend/src/lib/stores/stores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,26 @@ export const isLoading = writable<boolean>(false);
isLoading.subscribe((value: boolean) => {
return value;
});

export const showToast = writable<boolean>(false);

showToast.subscribe((value: boolean) => {
return value;
});

export const toastMessage = writable<string>();

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);
}
2 changes: 2 additions & 0 deletions frontend/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<script lang="ts">
import '../app.css';
import Navbar from '$lib/components/Navbar.svelte';
import ToastNotification from '$lib/components/ToastNotification.svelte';
</script>

<div class="flex flex-col h-screen">
<Navbar />
<slot />
<ToastNotification />
</div>

<style>
Expand Down
18 changes: 2 additions & 16 deletions frontend/src/routes/create-goals/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import DaysOfWeekSelector from '$lib/components/DaysOfWeekSelector.svelte';
import Message from '$lib/components/Message.svelte';
import { createGoal } from '$lib/api';
import { setToast } from '$lib/stores/stores';
let goal: GoalCreate = {
days_of_week: {
Expand All @@ -19,7 +20,6 @@
let selectAll = false;
let goalError = false;
let showToast = false;
const toggleAll = () => {
selectAll = !selectAll;
Expand All @@ -33,10 +33,6 @@
}
};
function delay(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function handleSave() {
goalError = false;
Expand All @@ -50,13 +46,10 @@
try {
await createGoal(goal);
showToast = true;
await delay(3000);
showToast = false;
await setToast('Goal successfully saved, returning home.');
goto('/');
} catch (error) {
console.log(error);
showToast = false;
}
}
Expand Down Expand Up @@ -499,13 +492,6 @@
>Save Smart Goal</button
>
</div>
{#if showToast}
<div class="toast" id="toast-message">
<div class="alert alert-info">
<span>Goal successfully saved, returning home.</span>
</div>
</div>
{/if}
</div>

<div class="flex">
Expand Down

0 comments on commit 2a6f9fe

Please sign in to comment.