From 3126d90f5af29b55be8685360244bf56a2cf07e1 Mon Sep 17 00:00:00 2001 From: ryan Date: Tue, 18 Mar 2025 23:47:47 +0300 Subject: [PATCH] fix(auto search): retrace my steps to resolve ownership errors --- src/components/items_list.rs | 81 +++++++++++++++++------------------- 1 file changed, 38 insertions(+), 43 deletions(-) diff --git a/src/components/items_list.rs b/src/components/items_list.rs index 2acffa0..24e4fb1 100644 --- a/src/components/items_list.rs +++ b/src/components/items_list.rs @@ -352,44 +352,32 @@ pub fn ItemsList( let (wikidata_suggestions, set_wikidata_suggestions) = create_signal(HashMap::>::new()); // Function to fetch Wikidata suggestions - let fetch_wikidata_suggestions = { - let mut debounce_timers = std::collections::HashMap::::new(); - Rc::new(move |key: String, query: String| { - log!("Fetching suggestions for key: {}, query: {}", key, query); + let fetch_wikidata_suggestions = move |key: String, query: String| { + log!("Fetching suggestions for key: {}, query: {}", key, query); + spawn_local(async move { + if query.is_empty() { + set_wikidata_suggestions.update(|suggestions| { + suggestions.remove(&key); + }); + return; + } - // Cancel previous timer for this key - debounce_timers.remove(&key); + let url = format!( + "https://www.wikidata.org/w/api.php?action=wbsearchentities&search={}&language=en&limit=5&format=json&origin=*", + query + ); - // Store new timer - let timer = gloo_timers::future::TimeoutFuture::new(300); - debounce_timers.insert(key.clone(), timer); - - spawn_local(async move { - timer.await; - if query.is_empty() { - set_wikidata_suggestions.update(|suggestions| { - suggestions.remove(&key); - }); - return; - } - - let url = format!( - "https://www.wikidata.org/w/api.php?action=wbsearchentities&search={}&language=en&limit=5&format=json&origin=*", - query - ); - - match gloo_net::http::Request::get(&url).send().await { - Ok(response) => { - if let Ok(data) = response.json::().await { - set_wikidata_suggestions.update(|suggestions| { - suggestions.insert(key, data.search); - }); - } + match gloo_net::http::Request::get(&url).send().await { + Ok(response) => { + if let Ok(data) = response.json::().await { + set_wikidata_suggestions.update(|suggestions| { + suggestions.insert(key, data.search); + }); } - Err(_) => log!("Failed to fetch Wikidata suggestions"), } - }); - }) + Err(_) => log!("Failed to fetch Wikidata suggestions"), + } + }); }; //function to fetch properties @@ -736,7 +724,6 @@ pub fn ItemsList( { property } {move || items.get().iter().enumerate().map(|(index, item)| { let update_item_clone = Arc::clone(&update_item_cloned); - let fetch_wikidata_suggestions_clone = Rc::clone(&fetch_wikidata_suggestions); view! { {match property { @@ -745,13 +732,8 @@ pub fn ItemsList( + {move || { if *show_suggestions.get().get(&format!("name-{}", index)).unwrap_or(&false) { log!("Rendering suggestions list");