From d71c94a4b419cd164021a5db5fe6b22f914d99e4 Mon Sep 17 00:00:00 2001 From: itsscb Date: Fri, 30 Aug 2024 15:01:55 +0200 Subject: [PATCH 1/3] feat(frontend): set index to next empty field on disabled --- frontend/src/pages/home.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/frontend/src/pages/home.rs b/frontend/src/pages/home.rs index 5539518..d46899b 100644 --- a/frontend/src/pages/home.rs +++ b/frontend/src/pages/home.rs @@ -235,9 +235,12 @@ pub fn Home() -> Html { let on_disabled = { let curr_index = curr_index.clone(); + let input_values = input_values.clone(); Callback::from(move |_e: MouseEvent| { - set_focus(*curr_index); + let index = input_values.iter().enumerate().find(|(_, v)| v.is_empty()).map_or(0, |(i,_)| i); + set_focus(index); + curr_index.set(index); }) }; From 83b216ab49ca798c264e7e3b00daefca1d2e9454 Mon Sep 17 00:00:00 2001 From: itsscb Date: Fri, 30 Aug 2024 15:21:56 +0200 Subject: [PATCH 2/3] chore(frontend): house keeping --- frontend/src/pages/home.rs | 29 +++++------------------------ 1 file changed, 5 insertions(+), 24 deletions(-) diff --git a/frontend/src/pages/home.rs b/frontend/src/pages/home.rs index d46899b..bd6ab2c 100644 --- a/frontend/src/pages/home.rs +++ b/frontend/src/pages/home.rs @@ -104,7 +104,6 @@ fn string_to_html(input: &[CharStatus]) -> Html { #[allow(clippy::too_many_arguments)] fn fetch_new_word( - word_list: &UseStateHandle, word: &UseStateHandle, loading: &UseStateHandle, submitted_words: &UseStateHandle>>>, @@ -122,7 +121,7 @@ fn fetch_new_word( let node_refs = node_refs.clone(); let result = result.clone(); let word = word.clone(); - // let word_list = word_list.clone(); + wasm_bindgen_futures::spawn_local(async move { loading.set(true); let res = Request::get(NEW_WORD_URI).send().await; @@ -136,13 +135,12 @@ fn fetch_new_word( game_over.set(false); result.set(GameResult::Lose); loading.set(false); - // sleep(Duration::from_secs(5)).await; - // word.set(word_list.get_word().to_uppercase()); } } }); } +#[allow(dead_code)] fn fetch_words(state: &UseStateHandle) { let state = state.clone(); wasm_bindgen_futures::spawn_local(async move { @@ -157,8 +155,6 @@ fn fetch_words(state: &UseStateHandle) { #[function_component] pub fn Home() -> Html { - let word_list: UseStateHandle = use_state(WordList::new); - let word: UseStateHandle = use_state(String::new); let loading: UseStateHandle = use_state(|| true); let curr_index: UseStateHandle = use_state(|| 0usize); @@ -174,8 +170,6 @@ pub fn Home() -> Html { let result = use_state(|| GameResult::Lose); { - let word_list = word_list.clone(); - let wl = word_list.clone(); let handle = word.clone(); let loading = loading.clone(); @@ -186,16 +180,8 @@ pub fn Home() -> Html { let node_refs = node_refs.clone(); let result = result.clone(); - // use_effect_with((), move |()| { - // spawn_local(async move { - // fetch_words(&wl); - // }); - // }); - use_effect_with((), move |()| { - // sleep(Duration::from_millis(200)); fetch_new_word( - &word_list, &handle, &loading, &submitted_words, @@ -211,18 +197,18 @@ pub fn Home() -> Html { let game_over_check = { let word = word.clone(); let submitted_words = submitted_words.clone(); - let iv = input_values.clone(); + let input_values = input_values.clone(); let game_over = game_over.clone(); let length = length.clone(); let result = result.clone(); Callback::from(move |_| { if submitted_words.iter().count() >= *length - 1 - || crate::compare_strings(&word, &iv.join("")) + || crate::compare_strings(&word, &input_values.join("")) .iter() .all(|v| matches!(v, CharStatus::Match(_))) { - if crate::compare_strings(&word, &iv.join("")) + if crate::compare_strings(&word, &input_values.join("")) .iter() .all(|v| matches!(v, CharStatus::Match(_))) { @@ -254,12 +240,10 @@ pub fn Home() -> Html { let loading = loading.clone(); let result = result.clone(); let curr_index = curr_index.clone(); - let word_list = word_list.clone(); Callback::from(move |_e: MouseEvent| { if *game_over { curr_index.set(0); - let word_list = word_list.clone(); let input_values = input_values.clone(); let submitted_words = submitted_words.clone(); let game_over = game_over.clone(); @@ -269,7 +253,6 @@ pub fn Home() -> Html { let node_refs = node_refs.clone(); let result = result.clone(); fetch_new_word( - &word_list, &word, &loading, &submitted_words, @@ -384,7 +367,6 @@ pub fn Home() -> Html { ) } > - //

{format!("{:?}",*word_list)}

Html { } >