From c64a7a640ac8c59eb6339f0a06d2ad2efab3fd11 Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Fri, 7 Oct 2022 16:59:29 -0400 Subject: Start working on authentication --- webapp/src/components/app_context_provider.rs | 50 +++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 webapp/src/components/app_context_provider.rs (limited to 'webapp/src/components') diff --git a/webapp/src/components/app_context_provider.rs b/webapp/src/components/app_context_provider.rs new file mode 100644 index 0000000..f2938ff --- /dev/null +++ b/webapp/src/components/app_context_provider.rs @@ -0,0 +1,50 @@ +use gloo_net::http::Request; +use protocol::UserInfo; +use std::rc::Rc; +use yew::prelude::*; + +#[derive(Clone, Debug, PartialEq)] +pub struct AppContext { + pub user: Option, +} + +#[derive(Properties, Clone, PartialEq)] +pub struct Props { + pub children: Children, +} + +#[function_component(AppContextProvider)] +pub fn app_context_provider(props: &Props) -> Html { + let context: UseStateHandle>> = use_state(|| None); + + { + let context = context.clone(); + use_effect_with_deps( + move |_| { + wasm_bindgen_futures::spawn_local(async move { + let user_info: Option = Request::get("/api/user/info") + .send() + .await + .unwrap() + .json() + .await + .unwrap(); + context.set(Some(Rc::new(AppContext { user: user_info }))); + }); + || () + }, + (), + ); + } + + match &*context { + None => html! { +

{ "Loading app..." }

+ }, + Some(context) => html! { + > {context}> + { for props.children.iter() } + >> + }, + } +} -- cgit v1.2.3