#41 add toasts and use for success and error
This commit is contained in:
27
view/src/lib/toast.ts
Normal file
27
view/src/lib/toast.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { writable } from "svelte/store";
|
||||
|
||||
|
||||
type Type = 'success' | 'info' | 'warning' | 'error';
|
||||
type Toast = {
|
||||
message: string;
|
||||
type: Type;
|
||||
}
|
||||
|
||||
const toastStore = writable<Toast[]>([]);
|
||||
|
||||
|
||||
const addToast = (message: string, type: Type) => {
|
||||
const newToast = { message, type };
|
||||
|
||||
toastStore.update((toasts) => {
|
||||
return [...toasts, newToast];
|
||||
});
|
||||
|
||||
setTimeout(() => {
|
||||
toastStore.update((toasts) => toasts.filter((t) => t !== newToast));
|
||||
}, 5000);
|
||||
}
|
||||
|
||||
|
||||
export { toastStore, addToast, };
|
||||
export type { Toast }
|
||||
Reference in New Issue
Block a user