summaryrefslogtreecommitdiff
path: root/webapp/src/main.rs
blob: b275b49965e465f752f510ba8873a309156bd923 (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use std::rc::Rc;

#[allow(unused_imports)]
use log::{debug, error, info, warn};
use uuid::Uuid;
use yew::prelude::*;
use yew_router::prelude::*;
pub mod bridge_engine;
pub mod card;
pub mod components;
use components::{AppContextProvider, AppContext, Game, ErrorInfo, Table};
use gloo_net::http::Request;
extern crate wee_alloc;

// Use `wee_alloc` as the global allocator.
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
#[derive(Clone, Routable, PartialEq)]
 
enum Route {
    #[at("/")]
    Home,
    #[at("/playground")]
    Playground,
    #[at("/table/:id")]
    Table { id: Uuid, },
}

fn main() {
    std::panic::set_hook(Box::new(console_error_panic_hook::hook));
    wasm_logger::init(wasm_logger::Config::new(log::Level::Debug));
    yew::start_app::<App>();
}

#[function_component(App)]
pub fn app() -> Html {
    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!("User: {:#?}", ctx.user);

    let user = match &ctx.user {
        Some(userinfo) => html! {
	    <p>{ format!("Logged in as {}", userinfo.username) }</p>
	},
        None => html! { <p><a href="/api/login">{ "Log in" }</a></p> },
    };

    if let Some(table) = ctx.user.as_ref().and_then(|u| u.table.as_ref() ) {
	let history = use_history().unwrap();
	history.push(Route::Table { id: table.id });
    }

    let create_table = Callback::from(|_| {
	wasm_bindgen_futures::spawn_local(async move {
	    let response = Request::post("/api/table").send().await.unwrap();
	    let table_id: Uuid = response.json().await.unwrap();
	    info!("Created table {table_id}");
	});
    });

    html! {
        <>
	    if let Some(error) = &ctx.error {
		<ErrorInfo ..error.clone()/>
            }
	    <ul>
	    <li>{ user }</li>
            <li><Link<Route> to={Route::Playground}>{ "Playground" }</Link<Route>></li>
	    <li><button onclick={create_table}>{ "Create table" }</button></li>
	    </ul>
        </>
    }
}

fn switch(routes: &Route) -> Html {
    match routes {
        Route::Home => html!{ <Home/> },
        Route::Playground => html! {
            <div class="game-layout"><Game /></div>
        },
	Route::Table { id } => html! {
	    <Table table={ protocol::Table { id: *id } } />
	},
    }
}

#[cfg(test)]
mod tests {
    pub fn test_setup() {
        dotenv::dotenv().ok();
        let _ = env_logger::builder().is_test(true).try_init();
    }
}