42 lines
1.1 KiB
Rust
42 lines
1.1 KiB
Rust
use leptos::*;
|
|
use rand::distributions::{Alphanumeric, DistString};
|
|
|
|
#[derive(Clone)]
|
|
pub struct NotificationsContext {
|
|
pub notifications: ReadSignal<Vec<ToastNotification>>,
|
|
pub set_notifications: WriteSignal<Vec<ToastNotification>>,
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub struct ToastNotification {
|
|
pub text: String,
|
|
pub option: String, // error, warning, info, success
|
|
pub id: String,
|
|
}
|
|
|
|
pub fn init_toast() {
|
|
let (notifications, set_notifications) = create_signal::<Vec<ToastNotification>>(vec![]);
|
|
provide_context(NotificationsContext {
|
|
notifications,
|
|
set_notifications,
|
|
});
|
|
}
|
|
|
|
pub fn add_toast(text: String, option: String) {
|
|
let context = expect_context::<NotificationsContext>();
|
|
|
|
let id = Alphanumeric.sample_string(&mut rand::thread_rng(), 4);
|
|
|
|
let mut vec = context.notifications.get();
|
|
vec.push(ToastNotification { text, option, id });
|
|
context.set_notifications.set(vec);
|
|
}
|
|
|
|
pub fn remove_toast(id: String) {
|
|
let context = expect_context::<NotificationsContext>();
|
|
|
|
let mut vec = context.notifications.get();
|
|
vec.retain(|x| x.id != id);
|
|
context.set_notifications.set(vec);
|
|
}
|