summaryrefslogtreecommitdiff
path: root/webapp/src/components/table.rs
blob: 3561131ed07f9b2048c118917dcbac0274483640 (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
use gloo_net::http::Request;
use log::info;
use yew::prelude::*;

use crate::use_app_context;

#[function_component(Table)]
pub fn table(props: &TableProps) -> Html {
    let ctx = use_app_context();

    let table_state: UseStateHandle<Option<String>> = use_state(|| None);
    {
        // TODO update this from server state
        let table_state = table_state.clone();
        let props = props.clone();
        let ctx = ctx.clone();
        use_effect_with_deps(
            move |_| {
                ctx.spawn_async(async move {
                    let response = Request::get(&format!("/api/table/{}", props.table.id))
                        .send()
                        .await?;
                    let table: protocol::bridge_engine::GameState = response.json().await?;
                    table_state.set(Some(format!("{:#?}", table)));
                    Ok(())
                });
                || ()
            },
            (),
        );
    }

    let leave_table = {
        let ctx = ctx.clone();
        Callback::from(move |_| {
            ctx.leave_table();
        })
    };

    html! {
    <>
        <p>{ format!("This is table {}", props.table.id) }</p>
        <button onclick={leave_table}>
            { "Leave table" }
        </button>
            <pre>{ table_state.as_ref().map_or("".to_string(), |t| format!("{}", t)) }</pre>
    </>
    }
}

#[derive(PartialEq, Properties, Clone)]
pub struct TableProps {
    pub table: protocol::Table,
}