Correctly handle shared state
This commit is contained in:
parent
183f8a75d2
commit
ede98561b5
2 changed files with 19 additions and 6 deletions
|
|
@ -170,7 +170,7 @@ pub fn FullContextRedirect<'a>(cx: Scope<'a, RedirectorProps<'a>>) -> Element {
|
||||||
Ok(_) => true,
|
Ok(_) => true,
|
||||||
Err(StorageError::KeyNotFound(_)) => {
|
Err(StorageError::KeyNotFound(_)) => {
|
||||||
navigator.push(Route::Login);
|
navigator.push(Route::Login);
|
||||||
false
|
return None;
|
||||||
}
|
}
|
||||||
Err(e) => unreachable!("Could not get token: {e:?}"),
|
Err(e) => unreachable!("Could not get token: {e:?}"),
|
||||||
};
|
};
|
||||||
|
|
@ -179,7 +179,7 @@ pub fn FullContextRedirect<'a>(cx: Scope<'a, RedirectorProps<'a>>) -> Element {
|
||||||
Ok(_) => true,
|
Ok(_) => true,
|
||||||
Err(StorageError::KeyNotFound(_)) => {
|
Err(StorageError::KeyNotFound(_)) => {
|
||||||
navigator.push(Route::HouseholdSelection);
|
navigator.push(Route::HouseholdSelection);
|
||||||
false
|
return None;
|
||||||
}
|
}
|
||||||
Err(e) => unreachable!("Could not get household: {e:?}"),
|
Err(e) => unreachable!("Could not get household: {e:?}"),
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -134,7 +134,8 @@ pub fn LoginRedirect(cx: Scope) -> Element {
|
||||||
let token = match LocalStorage::get::<LoginInfo>("token") {
|
let token = match LocalStorage::get::<LoginInfo>("token") {
|
||||||
Ok(v) => Some(v),
|
Ok(v) => Some(v),
|
||||||
Err(StorageError::KeyNotFound(_)) => {
|
Err(StorageError::KeyNotFound(_)) => {
|
||||||
return None;
|
navigator.push(Route::Login);
|
||||||
|
None
|
||||||
}
|
}
|
||||||
Err(e) => unreachable!("Could not get token: {e:?}"),
|
Err(e) => unreachable!("Could not get token: {e:?}"),
|
||||||
};
|
};
|
||||||
|
|
@ -142,16 +143,28 @@ pub fn LoginRedirect(cx: Scope) -> Element {
|
||||||
use_shared_state_provider(cx, || token.clone());
|
use_shared_state_provider(cx, || token.clone());
|
||||||
|
|
||||||
cx.render(match token {
|
cx.render(match token {
|
||||||
Some(_) => rsx! {
|
Some(info) => rsx! {
|
||||||
Outlet::<Route> {}
|
LoginRedirectInner {info: info},
|
||||||
},
|
},
|
||||||
None => {
|
None => {
|
||||||
navigator.push(Route::Login);
|
|
||||||
rsx! {{}}
|
rsx! {{}}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Props, PartialEq)]
|
||||||
|
struct LoginRedirectInnerProps {
|
||||||
|
info: LoginInfo,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn LoginRedirectInner(cx: Scope<LoginRedirectInnerProps>) -> Element {
|
||||||
|
use_shared_state_provider(cx, || cx.props.info.clone());
|
||||||
|
|
||||||
|
cx.render(rsx! {
|
||||||
|
Outlet::<Route> {}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
async fn do_login(username: String, password: String) -> anyhow::Result<()> {
|
async fn do_login(username: String, password: String) -> anyhow::Result<()> {
|
||||||
let rsp = gloo_net::http::Request::post(api!("login"))
|
let rsp = gloo_net::http::Request::post(api!("login"))
|
||||||
.json(&LoginRequest {
|
.json(&LoginRequest {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue