From e00f70e1f70463e1deeda99af991ca5309a23f73 Mon Sep 17 00:00:00 2001 From: xeovalyte Date: Tue, 12 Mar 2024 16:28:20 +0100 Subject: [PATCH] Added timeout function for toast notifications --- application/Cargo.toml | 1 + application/src/util/toast.rs | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/application/Cargo.toml b/application/Cargo.toml index 2148ed8..72380ba 100644 --- a/application/Cargo.toml +++ b/application/Cargo.toml @@ -17,6 +17,7 @@ leptos-use = "0.10.2" serde = "1.0.196" serde_json = "1.0.113" rand = "0.8.5" +gloo-timers = "0.3.0" # utils # strum = { version = "0.25", features = ["derive", "strum_macros"] } diff --git a/application/src/util/toast.rs b/application/src/util/toast.rs index 860e430..8081dc2 100644 --- a/application/src/util/toast.rs +++ b/application/src/util/toast.rs @@ -1,3 +1,4 @@ +use gloo_timers::future::TimeoutFuture; use leptos::*; 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 mut vec = context.notifications.get(); - vec.push(ToastNotification { text, option, id }); + vec.push(ToastNotification { + text, + option, + id: id.clone(), + }); context.set_notifications.set(vec); + + spawn_local(async { + TimeoutFuture::new(5000).await; + remove_toast(id); + }); } pub fn remove_toast(id: String) { let context = expect_context::(); - let mut vec = context.notifications.get(); + let mut vec = context.notifications.get_untracked(); vec.retain(|x| x.id != id); context.set_notifications.set(vec); }