cards/client/src/components/browser.rs

51 lines
1.5 KiB
Rust
Raw Normal View History

2024-07-28 02:36:04 -04:00
use crate::components::websocket::WebSocketContext;
use leptos::*;
use leptos_use::core::ConnectionReadyState;
2024-08-01 20:32:49 -04:00
use lib::models::*;
use serde_json::to_string;
2024-07-28 02:36:04 -04:00
#[component]
pub fn Browser() -> impl IntoView {
let websocket = expect_context::<WebSocketContext>();
2024-08-01 20:32:49 -04:00
let connected = move || websocket.ready_state.get() == ConnectionReadyState::Open;
2024-07-28 02:36:04 -04:00
2024-08-01 20:32:49 -04:00
let game_update_context = expect_context::<ReadSignal<Option<GamesUpdate>>>();
2024-07-28 02:36:04 -04:00
let (active_games, set_active_games) = create_signal::<Vec<String>>(vec![]);
2024-08-01 20:32:49 -04:00
let fake_new_game_request = NewGameRequest {
name: String::from("Ligma"),
};
// Game stuff
let new_game_test = move |_| {
(websocket.send)(&to_string(&fake_new_game_request).unwrap());
};
2024-07-28 02:36:04 -04:00
create_effect(move |_| {
game_update_context.with(move |games| {
if let Some(games) = games {
set_active_games(games.games.clone());
}
})
});
// Clear games list on disconnect
create_effect(move |_| {
2024-08-01 20:32:49 -04:00
if !connected() {
set_active_games(vec![]);
}
2024-07-28 02:36:04 -04:00
});
view! {
<div class="p-1">
<ul>
<h2 class="text-2xl">Game Browser</h2>
{move || active_games().into_iter().map(|n| view! { <li>{n}</li> }).collect_view()}
</ul>
2024-08-01 20:32:49 -04:00
<button on:click=new_game_test disabled=move || !connected()>
"Test New Game"
</button>
2024-07-28 02:36:04 -04:00
</div>
}
}