gitea_pages/frontend/src/stores.rs

68 lines
1.6 KiB
Rust

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<UserData>);
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<components::notification::NotificationMessage>,
}
macro_rules! from_notification_types {
($($type:ident),*) => {
$(
paste::item! {
pub fn [<$type:snake>](message: Option<components::notification::NotificationMessage>) -> 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<usize>,
pub notifications: BTreeMap<usize, Notification>,
}
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
}
}