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
3 changed files with 19 additions and 2 deletions

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 crate::util;
use leptos::*; use leptos::*;
use leptos_use::{core::ConnectionReadyState, use_websocket, UseWebsocketReturn}; use leptos_use::{core::ConnectionReadyState, use_websocket, UseWebsocketReturn};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -158,6 +159,11 @@ fn surrealdb_response(response: String) {
/// Function to execute when DB signin is succesful /// Function to execute when DB signin is succesful
fn use_surrealdb(_response: SurrealResponse) { fn use_surrealdb(_response: SurrealResponse) {
util::toast::add_toast(
"Succesfully signed into DB".to_string(),
"success".to_string(),
);
let websocket = expect_context::<SurrealContext>(); let websocket = expect_context::<SurrealContext>();
websocket.set_authenticated.set(true); websocket.set_authenticated.set(true);

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);
} }