app: Store both household display name & id

This commit is contained in:
traxys 2023-05-29 12:36:21 +02:00
parent d0313af5ba
commit dd266dccfd
4 changed files with 21 additions and 9 deletions

2
Cargo.lock generated
View file

@ -76,7 +76,9 @@ dependencies = [
"gloo-storage", "gloo-storage",
"gloo-utils", "gloo-utils",
"log", "log",
"serde",
"serde_json", "serde_json",
"uuid",
"wasm-bindgen", "wasm-bindgen",
"wasm-bindgen-futures", "wasm-bindgen-futures",
"web-sys", "web-sys",

View file

@ -13,7 +13,9 @@ gloo-net = "0.2.6"
gloo-storage = "0.2.2" gloo-storage = "0.2.2"
gloo-utils = "0.1.6" gloo-utils = "0.1.6"
log = "0.4.17" log = "0.4.17"
serde = { version = "1.0.163", features = ["derive"] }
serde_json = "1.0.96" serde_json = "1.0.96"
uuid = "1.3.3"
wasm-bindgen = "0.2.86" wasm-bindgen = "0.2.86"
wasm-bindgen-futures = "0.4.36" wasm-bindgen-futures = "0.4.36"
web-sys = "0.3.63" web-sys = "0.3.63"

View file

@ -1,6 +1,8 @@
use api::{LoginRequest, LoginResponse}; use api::{LoginRequest, LoginResponse};
use gloo_storage::{errors::StorageError, LocalStorage, Storage}; use gloo_storage::{errors::StorageError, LocalStorage, Storage};
use log::Level; use log::Level;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use wasm_bindgen::JsCast; use wasm_bindgen::JsCast;
use web_sys::HtmlInputElement; use web_sys::HtmlInputElement;
use yew::prelude::*; use yew::prelude::*;
@ -45,10 +47,16 @@ fn App() -> Html {
} }
} }
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
struct HouseholdInfo {
id: Uuid,
name: String,
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
struct RegaladeGlobalState { struct RegaladeGlobalState {
token: AttrValue, token: AttrValue,
household: AttrValue, household: HouseholdInfo,
} }
impl RegaladeGlobalState { impl RegaladeGlobalState {
@ -62,7 +70,7 @@ impl RegaladeGlobalState {
Err(e) => unreachable!("Could not get token: {e:?}"), Err(e) => unreachable!("Could not get token: {e:?}"),
}; };
let household = match LocalStorage::get::<String>("household") { let household = match LocalStorage::get::<HouseholdInfo>("household") {
Ok(v) => v, Ok(v) => v,
Err(StorageError::KeyNotFound(_)) => { Err(StorageError::KeyNotFound(_)) => {
navigator.push(&Route::HouseholdSelect); navigator.push(&Route::HouseholdSelect);
@ -73,7 +81,7 @@ impl RegaladeGlobalState {
Some(Self { Some(Self {
token: token.into(), token: token.into(),
household: household.into(), household,
}) })
} }
@ -83,14 +91,14 @@ impl RegaladeGlobalState {
Err(e) => unreachable!("Could not get token: {e:?}"), Err(e) => unreachable!("Could not get token: {e:?}"),
}; };
let household = match LocalStorage::get::<String>("household") { let household = match LocalStorage::get::<HouseholdInfo>("household") {
Ok(v) => v, Ok(v) => v,
Err(e) => unreachable!("Could not get household: {e:?}"), Err(e) => unreachable!("Could not get household: {e:?}"),
}; };
Self { Self {
token: token.into(), token: token.into(),
household: household.into(), household,
} }
} }
} }

View file

@ -1,5 +1,5 @@
use yew::prelude::*; use yew::prelude::*;
use crate::Route; use crate::{Route, HouseholdInfo};
#[derive(PartialEq)] #[derive(PartialEq)]
struct MenuEntry { struct MenuEntry {
@ -12,7 +12,7 @@ struct MenuEntry {
struct SidebarProps { struct SidebarProps {
entries: Vec<MenuEntry>, entries: Vec<MenuEntry>,
current: Route, current: Route,
household: AttrValue, household: HouseholdInfo,
children: Children, children: Children,
} }
@ -100,7 +100,7 @@ fn Sidebar(props: &SidebarProps) -> Html {
> >
<i class={classes!("fs-4", "bi-house-door-fill")}></i> <i class={classes!("fs-4", "bi-house-door-fill")}></i>
<strong class={classes!("ms-2", "d-none", "d-sm-inline")}> <strong class={classes!("ms-2", "d-none", "d-sm-inline")}>
{&props.household} {&props.household.name}
</strong> </strong>
</a> </a>
<ul class="dropdown-menu"> <ul class="dropdown-menu">
@ -121,7 +121,7 @@ fn Sidebar(props: &SidebarProps) -> Html {
#[derive(Properties, PartialEq)] #[derive(Properties, PartialEq)]
pub(crate) struct RegaladeSidebarProps { pub(crate) struct RegaladeSidebarProps {
pub(crate) current: Route, pub(crate) current: Route,
pub(crate) household: AttrValue, pub(crate) household: HouseholdInfo,
pub(crate) children: Children, pub(crate) children: Children,
} }