summaryrefslogtreecommitdiff
path: root/webapp/src/main.rs
blob: ef394a6a3dd80a2d354e1b8fb4547387d82dde19 (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
use data::MyMessage;
#[allow(unused_imports)]
use log::{debug, error, info, warn};
use yew::prelude::*;
pub mod bridge_engine;
pub mod card;
pub mod components;
use components::Game;
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;

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

#[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! {
        <>
        <p>{ &*msg }</p>
            <div class="app">
                <Game />
            </div>
        </>
    }
}

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