1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
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<ErrorInfoProperties>,
pub user: Option<UserInfo>,
}
#[derive(Properties, Clone, PartialEq)]
pub struct Props {
pub children: Children,
}
async fn initialize_context() -> Result<AppContext, anyhow::Error> {
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<Option<Rc<AppContext>>> = 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! {
<p>{ "Loading app..." }</p>
},
Some(context) => html! {
<ContextProvider<Rc<AppContext>> {context}>
{ for props.children.iter() }
</ContextProvider<Rc<AppContext>>>
},
}
}
|