diff --git a/client/src/components/auth.rs b/client/src/components/auth.rs index 0ee9eef..3057751 100644 --- a/client/src/components/auth.rs +++ b/client/src/components/auth.rs @@ -1,14 +1,16 @@ use crate::components::websocket::WebSocketContext; use html::Input; use leptos::*; +use leptos_use::core::ConnectionReadyState; use lib::*; use serde_json::to_string; #[component] pub fn Auth() -> impl IntoView { let websocket = expect_context::(); - let user_context = expect_context::>>(); let (username, set_username) = create_signal("".to_string()); + let user_context = expect_context::>>(); + let connected = move || websocket.ready_state.get() == ConnectionReadyState::Open; create_effect(move |_| { user_context.with(|new_user| { @@ -20,17 +22,28 @@ pub fn Auth() -> impl IntoView { let username_input_ref = create_node_ref::(); let send_login = move |_| { - websocket.send( - &to_string(&UserLogIn { - username: username_input_ref.get().unwrap().value(), - }) - .unwrap(), - ); - set_username.set(username_input_ref.get().unwrap().value()); - username_input_ref.get().unwrap().set_value(""); - logging::log!("sent"); + if let Some(input) = username_input_ref.get() { + if input.value() != String::from("") { + logging::log!("send"); + websocket.send( + &to_string(&UserLogIn { + username: input.value(), + }) + .unwrap(), + ); + set_username.set(input.value()); + input.set_value(""); + } + } }; + // Clear user name on disconnect + create_effect(move |_| { + if !connected() { + set_username(String::from("")); + } + }); + view! {

Sign in:

@@ -40,13 +53,14 @@ pub fn Auth() -> impl IntoView { class="w-96 font-mono rounded-sm bg-slate-900 text-slate-200" placeholder=move || username.get() node_ref=username_input_ref + disabled=move || !connected() /> - handle empty
diff --git a/client/src/components/browser.rs b/client/src/components/browser.rs index 69b852b..257ac55 100644 --- a/client/src/components/browser.rs +++ b/client/src/components/browser.rs @@ -23,17 +23,21 @@ pub fn Browser() -> impl IntoView { // Game stuff let new_game = move |_| { - (websocket.send)( - &to_string(&NewGameRequest { - name: new_game_name_ref.get().unwrap().value(), - packs: selected_packs() - .into_iter() - .map(|n| n.clone()) // hax - .collect::>(), - }) - .unwrap(), - ); - new_game_name_ref.get().unwrap().set_value(""); + if let Some(input) = new_game_name_ref.get() { + if input.value() != String::from("") { + (websocket.send)( + &to_string(&NewGameRequest { + name: input.value(), + packs: selected_packs() + .into_iter() + .map(|n| n.clone()) // hax + .collect::>(), + }) + .unwrap(), + ); + input.set_value(""); + } + } }; create_effect(move |_| { @@ -72,7 +76,6 @@ pub fn Browser() -> impl IntoView { disabled=move || !connected() node_ref=new_game_name_ref /> - handle empty

Packs