From 76f56360715e343f8b008c3ef4888b0066df4cd2 Mon Sep 17 00:00:00 2001 From: ryan Date: Tue, 7 Jan 2025 15:52:25 +0300 Subject: [PATCH] fix(ItemsList): scope Wikidata suggestions to focused cell - Modified `fetch_wikidata_suggestions` to accept a `key` parameter, tying suggestions to specific cells. - Updated state to use `HashMap>` for per-cell suggestion storage. - Adjusted rendering logic to only display suggestions for the currently focused cell. --- src/components/items_list.rs | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/components/items_list.rs b/src/components/items_list.rs index df1cb6b..34d47b4 100644 --- a/src/components/items_list.rs +++ b/src/components/items_list.rs @@ -38,14 +38,15 @@ pub fn ItemsList( custom_properties: HashMap::new(), }]); - let (wikidata_suggestions, set_wikidata_suggestions) = - create_signal(Vec::::new()); + let (wikidata_suggestions, set_wikidata_suggestions) = create_signal(HashMap::>::new()); // Fetch Wikidata suggestions - let fetch_wikidata_suggestions = move |query: String| { + let fetch_wikidata_suggestions = move |key:String, query: String| { spawn_local(async move { if query.is_empty() { - set_wikidata_suggestions.set(Vec::new()); + set_wikidata_suggestions.update(|suggestions| { + suggestions.remove(&key); + }); return; } @@ -57,7 +58,9 @@ pub fn ItemsList( match gloo_net::http::Request::get(&url).send().await { Ok(response) => { if let Ok(data) = response.json::().await { - set_wikidata_suggestions.set(data.search); + set_wikidata_suggestions.update(|suggestions| { + suggestions.insert(key, data.search); + }); } } Err(_) => log!("Failed to fetch Wikidata suggestions"), @@ -72,7 +75,7 @@ pub fn ItemsList( match field { "name" => { item.name = value.clone(); - fetch_wikidata_suggestions(value.clone()); + fetch_wikidata_suggestions(format!("name-{}", index), value.clone()); } "description" => { item.description = value.clone(); @@ -162,14 +165,20 @@ pub fn ItemsList( "Name" => view! {
    {move || { - let suggestions = wikidata_suggestions.get().to_vec(); + let suggestions = wikidata_suggestions.get() + .get(&format!("name-{}", index)) + .cloned() + .unwrap_or_default(); suggestions.into_iter().map(|suggestion| { let label_for_click = suggestion.label.clone(); let label_for_display = suggestion.label.clone();