mirror of
https://github.com/jcreek/SvelteKitSaasBoilerplate.git
synced 2026-07-12 18:43:50 +00:00
refactor(#35): split out toast and toast store
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
<script lang="ts">
|
||||
import { createEventDispatcher } from 'svelte';
|
||||
import { general } from '$lib/stores/generalStore.js';
|
||||
import { scheduleToast } from '$lib/stores/toastStore.js';
|
||||
|
||||
const dispatch = createEventDispatcher();
|
||||
|
||||
@@ -28,21 +28,7 @@
|
||||
|
||||
async function forgotPassword() {
|
||||
if (email === '') {
|
||||
general.update((value) => {
|
||||
return {
|
||||
...value,
|
||||
hideToast: false,
|
||||
toastMessage: 'Please enter your email address',
|
||||
toastType: 'error'
|
||||
};
|
||||
});
|
||||
|
||||
setTimeout(() => {
|
||||
general.update((value) => {
|
||||
return { ...value, hideToast: true, toastMessage: '', toastType: 'success' };
|
||||
});
|
||||
}, 5000);
|
||||
|
||||
scheduleToast('Please enter your email address', 'error', 5000);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -52,31 +38,11 @@
|
||||
|
||||
if (error) {
|
||||
console.error('Error sending password reset email:', error.message);
|
||||
general.update((value) => {
|
||||
return {
|
||||
...value,
|
||||
hideToast: false,
|
||||
toastMessage: 'Error sending password reset email. Please try again.',
|
||||
toastType: 'error'
|
||||
};
|
||||
});
|
||||
scheduleToast('Error sending password reset email. Please try again.', 'error', 5000);
|
||||
} else {
|
||||
console.log('Password reset email sent:', data);
|
||||
general.update((value) => {
|
||||
return {
|
||||
...value,
|
||||
hideToast: false,
|
||||
toastMessage: 'Password reset email sent. Please check your inbox.',
|
||||
toastType: 'success'
|
||||
};
|
||||
});
|
||||
scheduleToast('Password reset email sent. Please check your inbox.', 'error', 5000);
|
||||
}
|
||||
|
||||
setTimeout(() => {
|
||||
general.update((value) => {
|
||||
return { ...value, hideToast: true, toastMessage: '', toastType: 'success' };
|
||||
});
|
||||
}, 5000);
|
||||
}
|
||||
</script>
|
||||
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
<script lang="ts">
|
||||
import { slide } from 'svelte/transition';
|
||||
import { cubicInOut } from 'svelte/easing';
|
||||
import { onDestroy } from 'svelte';
|
||||
import { toast } from '$lib/stores/toastStore.js';
|
||||
import type { Toast } from '$lib/types/Shared/Toast';
|
||||
|
||||
let localToast: Toast;
|
||||
const unsubscribeToToastStore = toast.subscribe((value) => {
|
||||
localToast = value;
|
||||
});
|
||||
|
||||
onDestroy(() => {
|
||||
unsubscribeToToastStore();
|
||||
});
|
||||
</script>
|
||||
|
||||
{#if !localToast.hideToast}
|
||||
<div
|
||||
id={localToast.toastType == 'success' ? 'success-toast' : 'error-toast'}
|
||||
role="alert"
|
||||
class="alert {localToast.toastType == 'success'
|
||||
? 'alert-success'
|
||||
: 'alert-error'} fixed top-20 right-3 max-w-80 z-50 text-white"
|
||||
transition:slide={{ duration: 200, easing: cubicInOut }}
|
||||
>
|
||||
{#if localToast.toastType == 'success'}
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
class="stroke-current shrink-0 h-6 w-6"
|
||||
fill="none"
|
||||
viewBox="0 0 24 24"
|
||||
><path
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
stroke-width="2"
|
||||
d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"
|
||||
/></svg
|
||||
>
|
||||
{:else}
|
||||
<svg
|
||||
class="stroke-current shrink-0 h-6 w-6"
|
||||
viewBox="0 0 32 32"
|
||||
xml:space="preserve"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
xmlns:xlink="http://www.w3.org/1999/xlink"
|
||||
><g
|
||||
><g id="Error_1_"
|
||||
><g id="Error">
|
||||
<circle cx="16" cy="16" id="BG" r="16" style="fill:#E6E6E6;" />
|
||||
<path
|
||||
d="M14.5,25h3v-3h-3V25z M14.5,6v13h3V6H14.5z"
|
||||
id="Exclamatory_x5F_Sign"
|
||||
style="fill:#D72828;"
|
||||
/>
|
||||
</g>
|
||||
</g></g
|
||||
></svg
|
||||
>
|
||||
{/if}
|
||||
|
||||
<span>{localToast.toastMessage}</span>
|
||||
</div>
|
||||
{/if}
|
||||
@@ -1,3 +0,0 @@
|
||||
import { writable } from 'svelte/store';
|
||||
|
||||
export const general = writable({ hideToast: true, toastMessage: '', toastType: 'success' });
|
||||
@@ -0,0 +1,24 @@
|
||||
import type { Toast } from '$lib/types/Shared/Toast';
|
||||
import { writable } from 'svelte/store';
|
||||
|
||||
const MIN_DELAY = 3000;
|
||||
|
||||
export const toast = writable<Toast>({ hideToast: true, toastMessage: '', toastType: 'success' });
|
||||
|
||||
export function scheduleToast(message: string, toastType: 'success' | 'error', delay: number = MIN_DELAY) {
|
||||
|
||||
toast.update((value) => {
|
||||
return {
|
||||
...value,
|
||||
hideToast: false,
|
||||
toastMessage: message,
|
||||
toastType: toastType
|
||||
};
|
||||
});
|
||||
|
||||
setTimeout(() => {
|
||||
toast.update((value) => {
|
||||
return { ...value, hideToast: true, toastMessage: '' };
|
||||
});
|
||||
}, Math.max(delay, MIN_DELAY));
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
export type Toast = {
|
||||
hideToast: boolean,
|
||||
toastMessage: string,
|
||||
toastType: 'success' | 'error'
|
||||
}
|
||||
Reference in New Issue
Block a user