summaryrefslogtreecommitdiff
path: root/webapp
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
parent01753ebd32e4e0fa8adb11fb02a77720773e3018 (diff)
Start working on authentication
Diffstat (limited to 'webapp')
-rw-r--r--webapp/Cargo.toml2
-rw-r--r--webapp/src/components.rs2
-rw-r--r--webapp/src/components/app_context_provider.rs50
-rw-r--r--webapp/src/main.rs60
4 files changed, 80 insertions, 34 deletions
diff --git a/webapp/Cargo.toml b/webapp/Cargo.toml
index fe7addb..5411ccd 100644
--- a/webapp/Cargo.toml
+++ b/webapp/Cargo.toml
@@ -18,7 +18,7 @@ regex = "1.0"
lazy_static = "1.4"
gloo-net = "0.2.4"
wasm-bindgen-futures = "0.4.33"
-data = { path = "../data" }
+protocol = { path = "../protocol" }
yew-router = "0.16.0"
[dev-dependencies]
diff --git a/webapp/src/components.rs b/webapp/src/components.rs
index 80b5a91..224177b 100644
--- a/webapp/src/components.rs
+++ b/webapp/src/components.rs
@@ -1,5 +1,6 @@
use crate::card::Suit;
+mod app_context_provider;
mod bidding;
mod bidding_box;
mod bidding_table;
@@ -10,6 +11,7 @@ mod show_bid;
mod trick_in_play;
mod tricks_played;
+pub use self::app_context_provider::*;
pub use self::bidding::*;
pub use self::bidding_box::*;
pub use self::bidding_table::*;
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>>>
+ },
+ }
+}
diff --git a/webapp/src/main.rs b/webapp/src/main.rs
index 6fcf59f..a3fcebc 100644
--- a/webapp/src/main.rs
+++ b/webapp/src/main.rs
@@ -1,4 +1,5 @@
-use data::MyMessage;
+use std::rc::Rc;
+
#[allow(unused_imports)]
use log::{debug, error, info, warn};
use yew::prelude::*;
@@ -6,7 +7,7 @@ use yew_router::prelude::*;
pub mod bridge_engine;
pub mod card;
pub mod components;
-use components::Game;
+use components::{AppContextProvider, AppContext, Game};
use gloo_net::http::Request;
extern crate wee_alloc;
@@ -30,48 +31,41 @@ fn main() {
#[function_component(App)]
pub fn app() -> Html {
- let msg = use_state(|| "".to_string());
- {
- let msg = msg.clone();
- use_effect_with_deps(
- move |_| {
- wasm_bindgen_futures::spawn_local(async move {
- let m: MyMessage = Request::get("/api/test")
- .send()
- .await
- .unwrap()
- .json()
- .await
- .unwrap();
- msg.set(m.message)
- });
- || ()
- },
- (),
- )
+ html! {
+ <>
+ <AppContextProvider>
+ <BrowserRouter>
+ <Switch<Route> render={Switch::render(switch)} />
+ </BrowserRouter>
+ </AppContextProvider>
+ </>
}
+}
+
+#[function_component(Home)]
+fn home() -> Html {
+ let ctx = use_context::<Rc<AppContext>>().unwrap();
- info!("Got message from server: {}", &*msg);
+ let user = match &ctx.user {
+ Some(userinfo) => html! {
+ <p>{ format!("Logged in as {}", userinfo.username) }</p>
+ },
+ None => html! { <p>{ "Not logged in" }</p> },
+ };
html! {
<>
- <BrowserRouter>
- <Switch<Route> render={Switch::render(switch)} />
- </BrowserRouter>
+ { user }
+ <p>
+ <Link<Route> to={Route::Playground}>{ "Playground" }</Link<Route>>
+ </p>
</>
}
}
fn switch(routes: &Route) -> Html {
match routes {
- Route::Home => html! {
- <>
- <p>{ "Hello!" }</p>
- <p>
- <Link<Route> to={Route::Playground}>{ "Playground" }</Link<Route>>
- </p>
- </>
- },
+ Route::Home => html!{ <Home/> },
Route::Playground => html! {
<div class="game-layout"><Game /></div>
},