summaryrefslogtreecommitdiff
path: root/webapp/src/components
diff options
context:
space:
mode:
authorKjetil Orbekk <kj@orbekk.com>2022-11-05 19:36:28 -0400
committerKjetil Orbekk <kj@orbekk.com>2022-11-05 19:36:28 -0400
commit0012c25c9ab94754f3d6396e91dc3ae63d19ac9c (patch)
tree2a4f236be8eebfb39858a7900d574c6172e92404 /webapp/src/components
parent50d6ef8ad0f344c9b510c6752119fa5983629d95 (diff)
Update context state change code
Diffstat (limited to 'webapp/src/components')
-rw-r--r--webapp/src/components/app_context_provider.rs29
1 files changed, 18 insertions, 11 deletions
diff --git a/webapp/src/components/app_context_provider.rs b/webapp/src/components/app_context_provider.rs
index e3fe82e..4cf233b 100644
--- a/webapp/src/components/app_context_provider.rs
+++ b/webapp/src/components/app_context_provider.rs
@@ -8,7 +8,7 @@ pub struct ErrorInfoProperties {
pub message: String,
}
-#[derive(Clone, Debug, PartialEq)]
+#[derive(Clone, Debug, PartialEq, Default)]
pub struct AppContext {
pub error: Option<ErrorInfoProperties>,
pub user: Option<UserInfo>,
@@ -19,16 +19,17 @@ pub struct Props {
pub children: Children,
}
-async fn initialize_context() -> Result<AppContext, anyhow::Error> {
+async fn initialize_user_info() -> Result<UserInfo, anyhow::Error> {
let response = Request::get("/api/user/info").send().await?;
if response.status() == 401 {
- web_sys::window().unwrap().location().assign("/api/login").unwrap();
+ web_sys::window()
+ .unwrap()
+ .location()
+ .assign("/api/login")
+ .unwrap();
};
- let user = response.json().await?;
- Ok(AppContext {
- user: user,
- error: None,
- })
+ let user_info = response.json().await?;
+ Ok(user_info)
}
#[function_component(AppContextProvider)]
@@ -40,13 +41,19 @@ pub fn app_context_provider(props: &Props) -> Html {
use_effect_with_deps(
move |_| {
wasm_bindgen_futures::spawn_local(async move {
- context.set(Some(Rc::new(match initialize_context().await {
- Ok(context) => context,
+ let previous_context: AppContext = context
+ .as_ref()
+ .map_or(Default::default(), |c| (**c).clone());
+ context.set(Some(Rc::new(match initialize_user_info().await {
+ Ok(user_info) => AppContext {
+ user: Some(user_info),
+ ..previous_context
+ },
Err(e) => AppContext {
- user: None,
error: Some(ErrorInfoProperties {
message: format!("Could not contact server"),
}),
+ ..previous_context
},
})));
});