🎨 Adds defaults to notification service
This commit is contained in:
@@ -1,42 +1,42 @@
|
|||||||
import { writable, derived, type Writable } from 'svelte/store';
|
import { writable, derived, type Writable } from 'svelte/store'
|
||||||
|
|
||||||
type StateType = 'info' | 'success' | 'warning' | 'error';
|
type StateType = 'info' | 'success' | 'warning' | 'error'
|
||||||
|
|
||||||
type State = {
|
type State = {
|
||||||
id: string;
|
id: string
|
||||||
type: StateType;
|
type: StateType
|
||||||
message: string;
|
message: string
|
||||||
};
|
}
|
||||||
|
|
||||||
function createNotificationStore() {
|
function createNotificationStore() {
|
||||||
const state: State[] = [];
|
const state: State[] = []
|
||||||
const notifications = writable(state);
|
const notifications = writable(state)
|
||||||
const { subscribe } = notifications;
|
const { subscribe } = notifications
|
||||||
|
|
||||||
function send(message: string, type: StateType = 'info', timeout: number) {
|
function send(message: string, type: StateType = 'info', timeout: number) {
|
||||||
const id = generateId();
|
const id = generateId()
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
notifications.update((state) => {
|
notifications.update(state => {
|
||||||
return state.filter((n) => n.id !== id);
|
return state.filter(n => n.id !== id)
|
||||||
});
|
})
|
||||||
}, timeout);
|
}, timeout)
|
||||||
notifications.update((state) => {
|
notifications.update(state => {
|
||||||
return [...state, { id, type, message }];
|
return [...state, { id, type, message }]
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
subscribe,
|
subscribe,
|
||||||
send,
|
send,
|
||||||
error: (msg: string, timeout: number) => send(msg, 'error', timeout),
|
error: (msg: string, timeout: number = 4000) => send(msg, 'error', timeout),
|
||||||
warning: (msg: string, timeout: number) => send(msg, 'warning', timeout),
|
warning: (msg: string, timeout: number = 4000) => send(msg, 'warning', timeout),
|
||||||
info: (msg: string, timeout: number) => send(msg, 'info', timeout),
|
info: (msg: string, timeout: number = 4000) => send(msg, 'info', timeout),
|
||||||
success: (msg: string, timeout: number) => send(msg, 'success', timeout)
|
success: (msg: string, timeout: number = 4000) => send(msg, 'success', timeout)
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateId() {
|
function generateId() {
|
||||||
return '_' + Math.random().toString(36).substr(2, 9);
|
return '_' + Math.random().toString(36).substr(2, 9)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const notifications = createNotificationStore();
|
export const notifications = createNotificationStore()
|
||||||
|
|||||||
Reference in New Issue
Block a user