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 gloo_storage::{LocalStorage, Storage, errors::StorageError};
|
||||
use gloo_storage::{errors::StorageError, LocalStorage, Storage};
|
||||
use log::Level;
|
||||
use wasm_bindgen::JsCast;
|
||||
use web_sys::HtmlInputElement;
|
||||
|
|
@ -16,6 +16,10 @@ enum Route {
|
|||
Index,
|
||||
#[at("/login")]
|
||||
Login,
|
||||
#[at("/ingredients")]
|
||||
Ingredients,
|
||||
#[at("/household_select")]
|
||||
HouseholdSelect,
|
||||
#[at("/404")]
|
||||
#[not_found]
|
||||
NotFound,
|
||||
|
|
@ -30,35 +34,118 @@ fn App() -> Html {
|
|||
}
|
||||
}
|
||||
|
||||
fn switch(route: Route) -> Html {
|
||||
match route {
|
||||
Route::Index => html! {
|
||||
<Index />
|
||||
},
|
||||
Route::Login => html! {
|
||||
<Login />
|
||||
},
|
||||
Route::NotFound => html! {
|
||||
"Page not found"
|
||||
},
|
||||
#[derive(Debug, Clone)]
|
||||
struct RegaladeGlobalState {
|
||||
token: AttrValue,
|
||||
household: AttrValue,
|
||||
}
|
||||
|
||||
impl RegaladeGlobalState {
|
||||
pub fn get_or_navigate(navigator: Navigator) -> Option<Self> {
|
||||
let token = match LocalStorage::get::<String>("token") {
|
||||
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]
|
||||
fn Index() -> Html {
|
||||
fn HouseholdSelection() -> Html {
|
||||
let token = use_state(|| match LocalStorage::get::<String>("token") {
|
||||
Ok(v) => Some(v),
|
||||
Err(StorageError::KeyNotFound(_)) => None,
|
||||
Err(e) => unreachable!("Could not get token: {e:?}"),
|
||||
Err(e) => unreachable!("Could not get household: {e:?}"),
|
||||
});
|
||||
|
||||
match &*token {
|
||||
Some(_) => html! {
|
||||
"Index"
|
||||
},
|
||||
None => html! {
|
||||
<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