use serde::{Deserialize, Serialize}; use std::collections::BTreeMap; use std::num::Wrapping; use yewdux::prelude::*; use crate::components; #[derive(PartialEq, Serialize, Deserialize, Debug)] pub struct UserData { pub username: String, pub password: String, } #[derive(Default, PartialEq, Serialize, Deserialize, Store, Debug)] #[store(storage = "local")] pub struct User(pub Option); impl User { pub fn logged_in(&self) -> bool { self.0.is_some() } } #[derive(Clone, PartialEq)] pub struct Notification { pub notification_type: components::notification::NotificationType, pub message: Option, } macro_rules! from_notification_types { ($($type:ident),*) => { $( paste::item! { pub fn [<$type:snake>](message: Option) -> Self { Self { notification_type: components::notification::NotificationType::$type, message, } } } )* }; } impl Notification { from_notification_types!(Dark, Primary, Link, Info, Success, Warning, Danger); } #[derive(Default, Clone, PartialEq, Store)] pub struct Notifications { counter: Wrapping, pub notifications: BTreeMap, } impl Notifications { fn inc(&mut self) { self.counter += 1; } pub fn push(&mut self, notif: Notification) -> usize { let id = self.counter.0; self.notifications.insert(id, notif); self.inc(); id } }