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() } >> }, } }