app: Correctly redirect to login or household selection
This commit is contained in:
parent
141ff9e419
commit
e005d8b129
1 changed files with 104 additions and 17 deletions
121
app/src/main.rs
121
app/src/main.rs
|
|
@ -1,5 +1,5 @@
|
||||||
use api::{LoginRequest, LoginResponse};
|
use api::{LoginRequest, LoginResponse};
|
||||||
use gloo_storage::{LocalStorage, Storage, errors::StorageError};
|
use gloo_storage::{errors::StorageError, LocalStorage, Storage};
|
||||||
use log::Level;
|
use log::Level;
|
||||||
use wasm_bindgen::JsCast;
|
use wasm_bindgen::JsCast;
|
||||||
use web_sys::HtmlInputElement;
|
use web_sys::HtmlInputElement;
|
||||||
|
|
@ -16,6 +16,10 @@ enum Route {
|
||||||
Index,
|
Index,
|
||||||
#[at("/login")]
|
#[at("/login")]
|
||||||
Login,
|
Login,
|
||||||
|
#[at("/ingredients")]
|
||||||
|
Ingredients,
|
||||||
|
#[at("/household_select")]
|
||||||
|
HouseholdSelect,
|
||||||
#[at("/404")]
|
#[at("/404")]
|
||||||
#[not_found]
|
#[not_found]
|
||||||
NotFound,
|
NotFound,
|
||||||
|
|
@ -30,35 +34,118 @@ fn App() -> Html {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn switch(route: Route) -> Html {
|
#[derive(Debug, Clone)]
|
||||||
match route {
|
struct RegaladeGlobalState {
|
||||||
Route::Index => html! {
|
token: AttrValue,
|
||||||
<Index />
|
household: AttrValue,
|
||||||
},
|
}
|
||||||
Route::Login => html! {
|
|
||||||
<Login />
|
impl RegaladeGlobalState {
|
||||||
},
|
pub fn get_or_navigate(navigator: Navigator) -> Option<Self> {
|
||||||
Route::NotFound => html! {
|
let token = match LocalStorage::get::<String>("token") {
|
||||||
"Page not found"
|
Ok(v) => v,
|
||||||
},
|
Err(StorageError::KeyNotFound(_)) => {
|
||||||
|
navigator.push(&Route::Login);
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
Err(e) => unreachable!("Could not get token: {e:?}"),
|
||||||
|
};
|
||||||
|
|
||||||
|
let household = match LocalStorage::get::<String>("household") {
|
||||||
|
Ok(v) => v,
|
||||||
|
Err(StorageError::KeyNotFound(_)) => {
|
||||||
|
navigator.push(&Route::HouseholdSelect);
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
Err(e) => unreachable!("Could not get household: {e:?}"),
|
||||||
|
};
|
||||||
|
|
||||||
|
Some(Self {
|
||||||
|
token: token.into(),
|
||||||
|
household: household.into(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get() -> Self {
|
||||||
|
let token = match LocalStorage::get::<String>("token") {
|
||||||
|
Ok(v) => v,
|
||||||
|
Err(e) => unreachable!("Could not get token: {e:?}"),
|
||||||
|
};
|
||||||
|
|
||||||
|
let household = match LocalStorage::get::<String>("household") {
|
||||||
|
Ok(v) => v,
|
||||||
|
Err(e) => unreachable!("Could not get household: {e:?}"),
|
||||||
|
};
|
||||||
|
|
||||||
|
Self {
|
||||||
|
token: token.into(),
|
||||||
|
household: household.into(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Properties)]
|
||||||
|
struct GlobalStateRedirectorProps {
|
||||||
|
children: Children,
|
||||||
|
route: Route,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[function_component]
|
||||||
|
fn GlobalStateRedirector(props: &GlobalStateRedirectorProps) -> Html {
|
||||||
|
let navigator = use_navigator().unwrap();
|
||||||
|
let state = use_state(|| RegaladeGlobalState::get_or_navigate(navigator));
|
||||||
|
|
||||||
|
match &*state {
|
||||||
|
Some(state) => {
|
||||||
|
html! {
|
||||||
|
<RegaladeSidebar current={props.route} household={state.household.clone()}>
|
||||||
|
{ for props.children.iter() }
|
||||||
|
</RegaladeSidebar>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => html! {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[function_component]
|
#[function_component]
|
||||||
fn Index() -> Html {
|
fn HouseholdSelection() -> Html {
|
||||||
let token = use_state(|| match LocalStorage::get::<String>("token") {
|
let token = use_state(|| match LocalStorage::get::<String>("token") {
|
||||||
Ok(v) => Some(v),
|
Ok(v) => Some(v),
|
||||||
Err(StorageError::KeyNotFound(_)) => None,
|
Err(StorageError::KeyNotFound(_)) => None,
|
||||||
Err(e) => unreachable!("Could not get token: {e:?}"),
|
Err(e) => unreachable!("Could not get household: {e:?}"),
|
||||||
});
|
});
|
||||||
|
|
||||||
match &*token {
|
match &*token {
|
||||||
Some(_) => html! {
|
|
||||||
"Index"
|
|
||||||
},
|
|
||||||
None => html! {
|
None => html! {
|
||||||
<Redirect<Route> to={Route::Login} />
|
<Redirect<Route> to={Route::Login} />
|
||||||
},
|
},
|
||||||
|
Some(_) => html! {
|
||||||
|
{"Household Selection"}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn switch(route: Route) -> Html {
|
||||||
|
match route {
|
||||||
|
Route::Index => html! {
|
||||||
|
<GlobalStateRedirector {route}>
|
||||||
|
{"Index"}
|
||||||
|
</GlobalStateRedirector>
|
||||||
|
},
|
||||||
|
Route::Login => html! {
|
||||||
|
<Login />
|
||||||
|
},
|
||||||
|
Route::Ingredients => html! {
|
||||||
|
<GlobalStateRedirector {route}>
|
||||||
|
{"Ingredients"}
|
||||||
|
</GlobalStateRedirector>
|
||||||
|
},
|
||||||
|
Route::HouseholdSelect => html! {
|
||||||
|
<HouseholdSelection />
|
||||||
|
},
|
||||||
|
Route::NotFound => html! {
|
||||||
|
"Page not found"
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue