cards/client/src/components/browser.rs

185 lines
8 KiB
Rust
Raw Normal View History

2024-07-28 02:36:04 -04:00
use crate::components::websocket::WebSocketContext;
2024-08-02 02:35:31 -04:00
use leptos::html::Input;
2024-07-28 02:36:04 -04:00
use leptos::*;
use leptos_use::core::ConnectionReadyState;
2024-08-04 18:14:10 -04:00
use lib::*;
2024-08-01 20:32:49 -04:00
use serde_json::to_string;
2024-08-04 04:01:31 -04:00
use std::collections::BTreeSet;
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-08-04 03:13:34 -04:00
let card_packs = expect_context::<ReadSignal<CardPacksMeta>>();
2024-07-28 02:36:04 -04:00
let (active_games, set_active_games) = create_signal::<Vec<String>>(vec![]);
2024-08-04 03:13:34 -04:00
let new_game_name_ref = create_node_ref::<Input>();
let (selected_packs, set_selected_packs) = create_signal::<BTreeSet<u8>>(BTreeSet::new());
// create_effect(move |_| {
// logging::log!("{:#?}", selected_packs().iter().collect::<Vec<_>>());
// });
2024-08-01 20:32:49 -04:00
// Game stuff
2024-08-02 02:35:31 -04:00
let new_game = move |_| {
(websocket.send)(
&to_string(&NewGameRequest {
2024-08-04 03:13:34 -04:00
name: new_game_name_ref.get().unwrap().value(),
packs: selected_packs()
.into_iter()
.map(|n| n.clone()) // hax
.collect::<Vec<_>>(),
2024-08-02 02:35:31 -04:00
})
.unwrap(),
);
2024-08-04 03:13:34 -04:00
new_game_name_ref.get().unwrap().set_value("");
2024-08-01 20:32:49 -04:00
};
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
});
2024-08-04 03:13:34 -04:00
let (show_packs, set_show_packs) = create_signal(false);
let show_packs_button = move |_| set_show_packs(!show_packs());
2024-07-28 02:36:04 -04:00
view! {
<div class="p-1">
2024-08-04 03:13:34 -04:00
<h2 class="text-2xl">Game Browser</h2>
2024-07-28 02:36:04 -04:00
<ul>
{move || active_games().into_iter().map(|n| view! { <li>{n}</li> }).collect_view()}
</ul>
2024-08-04 03:13:34 -04:00
</div>
<hr/>
<div class="flex p-1">
2024-08-02 02:35:31 -04:00
<form onsubmit="return false" on:submit=new_game>
2024-08-04 03:13:34 -04:00
<h2 class="text-2xl">Create Game</h2>
2024-08-02 02:35:31 -04:00
<input
class="w-80 h-11 font-mono rounded-sm bg-slate-900 text-slate-200"
placeholder="Name"
disabled=move || !connected()
2024-08-04 03:13:34 -04:00
node_ref=new_game_name_ref
2024-08-02 02:35:31 -04:00
/>
2024-08-04 03:13:34 -04:00
handle empty
<h2 class="text-2xl">Packs</h2>
<div class="p-2">
<button type="button" class="bg-slate-600">
<input type="checkbox" id="all"/>
<label for="all">All</label>
</button>
<button type="button" class="bg-slate-600">
<input type="checkbox" id="official" checked/>
<label for="official">Official</label>
</button>
<button type="button" class="bg-slate-600" on:click=show_packs_button>
<label>Custom</label>
</button>
finish this
</div>
<Show when=move || show_packs()>
<span class="flex">
<div>
<h2 class="text-xl">Official</h2>
{move || {
card_packs()
.official_meta
.into_iter()
.map(|n| {
view! {
<input
type="checkbox"
value=n.pack
id=n.pack
checked
on:change=move |e| {
if event_target_checked(&e) {
logging::log!("insert");
set_selected_packs
.update(|packs| {
packs.insert(n.pack);
})
} else {
logging::log!("remove");
set_selected_packs
.update(|packs| {
packs.remove(&n.pack);
})
}
}
/>
<label for=n.pack>{n.name}</label>
<br/>
{set_selected_packs
.update(|packs| {
packs.insert(n.pack);
})}
}
})
.collect_view()
}}
</div>
<div>
<h2 class="text-xl">Unofficial</h2>
{move || {
card_packs()
.unofficial_meta
.into_iter()
.map(|n| {
view! {
<input
type="checkbox"
value=n.pack
id=n.pack
on:change=move |e| {
if event_target_checked(&e) {
logging::log!("insert");
set_selected_packs
.update(|packs| {
packs.insert(n.pack);
})
} else {
logging::log!("remove");
set_selected_packs
.update(|packs| {
packs.remove(&n.pack);
})
}
}
/>
<label for=n.pack>{n.name}</label>
<br/>
}
})
.collect_view()
}}
</div>
</span>
</Show>
2024-08-02 02:35:31 -04:00
<input
class="py-2 px-4 pl-4 font-bold text-white rounded border-b-4 bg-slate-600 border-slate-800 hover:bg-slate-700 hover:border-slate-500"
type="submit"
disabled=move || !connected()
value="New Game"
/>
</form>
2024-07-28 02:36:04 -04:00
</div>
}
}