Skip to content

Commit

Permalink
Merge pull request #307 from xyvora/toast
Browse files Browse the repository at this point in the history
Make toast into a shareable component
  • Loading branch information
sanders41 authored Aug 10, 2023
2 parents a98fb5f + f0302fb commit 5315b13
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 17 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
2 changes: 1 addition & 1 deletion frontend/tests/create_goals.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ test('end to end create goal', async ({ page }) => {
await page.locator('#time-bound').fill(timeBound);
await page.locator('#save-goal-button').click();
await expect(page.locator('#toast-message')).toBeVisible();
await expect(page).toHaveURL('/');
await expect(page).toHaveURL('/', { timeout: 10000 });

await page.getByLabel('account settings').click();
await expect(page.locator('#btn-delete')).toBeVisible();
Expand Down

0 comments on commit 5315b13

Please sign in to comment.