use gloo_net::http::Request; use protocol::UserInfo; use std::rc::Rc; use yew::prelude::*; #[derive(Properties, Clone, PartialEq, Debug)] pub struct ErrorInfoProperties { pub message: String, } #[derive(Clone, Debug, PartialEq)] pub struct AppContext { pub error: Option, pub user: Option, } #[derive(Properties, Clone, PartialEq)] pub struct Props { pub children: Children, } async fn initialize_context() -> Result { let user = Request::get("/api/user/info").send().await?.json().await?; Ok(AppContext { user: user, error: None, }) } #[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 { context.set(Some(Rc::new(match initialize_context().await { Ok(context) => context, Err(e) => AppContext { user: None, error: Some(ErrorInfoProperties { message: format!("Could not contact server"), }), }, }))); }); || () }, (), ); } match &*context { None => html! {

{ "Loading app..." }

}, Some(context) => html! { > {context}> { for props.children.iter() } >> }, } }