summaryrefslogtreecommitdiff
path: root/webapp/src/components
diff options
context:
space:
mode:
authorKjetil Orbekk <kj@orbekk.com>2022-10-07 16:59:29 -0400
committerKjetil Orbekk <kj@orbekk.com>2022-10-07 16:59:29 -0400
commitc64a7a640ac8c59eb6339f0a06d2ad2efab3fd11 (patch)
tree229ccf88da26d5339aadab98013834b6c2af6cbc /webapp/src/components
parent01753ebd32e4e0fa8adb11fb02a77720773e3018 (diff)
Start working on authentication
Diffstat (limited to 'webapp/src/components')
-rw-r--r--webapp/src/components/app_context_provider.rs50
1 files changed, 50 insertions, 0 deletions
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<UserInfo>,
+}
+
+#[derive(Properties, Clone, PartialEq)]
+pub struct Props {
+ pub children: Children,
+}
+
+#[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 {
+ let user_info: Option<UserInfo> = Request::get("/api/user/info")
+ .send()
+ .await
+ .unwrap()
+ .json()
+ .await
+ .unwrap();
+ context.set(Some(Rc::new(AppContext { user: user_info })));
+ });
+ || ()
+ },
+ (),
+ );
+ }
+
+ match &*context {
+ None => html! {
+ <p>{ "Loading app..." }</p>
+ },
+ Some(context) => html! {
+ <ContextProvider<Rc<AppContext>> {context}>
+ { for props.children.iter() }
+ </ContextProvider<Rc<AppContext>>>
+ },
+ }
+}