Added toast function for creating toast notifications #9

Merged
xeovalyte merged 3 commits from toast-notification into main 2024-03-12 16:29:06 +01:00
2 changed files with 13 additions and 2 deletions
Showing only changes of commit e00f70e1f7 - Show all commits

View File

@ -17,6 +17,7 @@ leptos-use = "0.10.2"
serde = "1.0.196" serde = "1.0.196"
serde_json = "1.0.113" serde_json = "1.0.113"
rand = "0.8.5" rand = "0.8.5"
gloo-timers = "0.3.0"
# utils # utils
# strum = { version = "0.25", features = ["derive", "strum_macros"] } # strum = { version = "0.25", features = ["derive", "strum_macros"] }

View File

@ -1,3 +1,4 @@
use gloo_timers::future::TimeoutFuture;
use leptos::*; use leptos::*;
use rand::distributions::{Alphanumeric, DistString}; use rand::distributions::{Alphanumeric, DistString};
@ -28,14 +29,23 @@ pub fn add_toast(text: String, option: String) {
let id = Alphanumeric.sample_string(&mut rand::thread_rng(), 4); let id = Alphanumeric.sample_string(&mut rand::thread_rng(), 4);
let mut vec = context.notifications.get(); let mut vec = context.notifications.get();
vec.push(ToastNotification { text, option, id }); vec.push(ToastNotification {
text,
option,
id: id.clone(),
});
context.set_notifications.set(vec); context.set_notifications.set(vec);
spawn_local(async {
TimeoutFuture::new(5000).await;
remove_toast(id);
});
} }
pub fn remove_toast(id: String) { pub fn remove_toast(id: String) {
let context = expect_context::<NotificationsContext>(); let context = expect_context::<NotificationsContext>();
let mut vec = context.notifications.get(); let mut vec = context.notifications.get_untracked();
vec.retain(|x| x.id != id); vec.retain(|x| x.id != id);
context.set_notifications.set(vec); context.set_notifications.set(vec);
} }