fix(auto search): retrace my steps to resolve ownership errors
This commit is contained in:
parent
8c1cab3615
commit
3126d90f5a
1 changed files with 38 additions and 43 deletions
|
@ -352,44 +352,32 @@ pub fn ItemsList(
|
||||||
let (wikidata_suggestions, set_wikidata_suggestions) = create_signal(HashMap::<String, Vec<WikidataSuggestion>>::new());
|
let (wikidata_suggestions, set_wikidata_suggestions) = create_signal(HashMap::<String, Vec<WikidataSuggestion>>::new());
|
||||||
|
|
||||||
// Function to fetch Wikidata suggestions
|
// Function to fetch Wikidata suggestions
|
||||||
let fetch_wikidata_suggestions = {
|
let fetch_wikidata_suggestions = move |key: String, query: String| {
|
||||||
let mut debounce_timers = std::collections::HashMap::<String, gloo_timers::future::TimeoutFuture>::new();
|
log!("Fetching suggestions for key: {}, query: {}", key, query);
|
||||||
Rc::new(move |key: String, query: String| {
|
spawn_local(async move {
|
||||||
log!("Fetching suggestions for key: {}, query: {}", key, query);
|
if query.is_empty() {
|
||||||
|
set_wikidata_suggestions.update(|suggestions| {
|
||||||
|
suggestions.remove(&key);
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Cancel previous timer for this key
|
let url = format!(
|
||||||
debounce_timers.remove(&key);
|
"https://www.wikidata.org/w/api.php?action=wbsearchentities&search={}&language=en&limit=5&format=json&origin=*",
|
||||||
|
query
|
||||||
|
);
|
||||||
|
|
||||||
// Store new timer
|
match gloo_net::http::Request::get(&url).send().await {
|
||||||
let timer = gloo_timers::future::TimeoutFuture::new(300);
|
Ok(response) => {
|
||||||
debounce_timers.insert(key.clone(), timer);
|
if let Ok(data) = response.json::<WikidataResponse>().await {
|
||||||
|
set_wikidata_suggestions.update(|suggestions| {
|
||||||
spawn_local(async move {
|
suggestions.insert(key, data.search);
|
||||||
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::<WikidataResponse>().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
|
//function to fetch properties
|
||||||
|
@ -736,7 +724,6 @@ pub fn ItemsList(
|
||||||
<td>{ property }</td>
|
<td>{ property }</td>
|
||||||
{move || items.get().iter().enumerate().map(|(index, item)| {
|
{move || items.get().iter().enumerate().map(|(index, item)| {
|
||||||
let update_item_clone = Arc::clone(&update_item_cloned);
|
let update_item_clone = Arc::clone(&update_item_cloned);
|
||||||
let fetch_wikidata_suggestions_clone = Rc::clone(&fetch_wikidata_suggestions);
|
|
||||||
view! {
|
view! {
|
||||||
<td>
|
<td>
|
||||||
{match property {
|
{match property {
|
||||||
|
@ -745,13 +732,8 @@ pub fn ItemsList(
|
||||||
<EditableCell
|
<EditableCell
|
||||||
value=item.name.clone()
|
value=item.name.clone()
|
||||||
on_input=move |value| {
|
on_input=move |value| {
|
||||||
let fetch_wiki = Rc::clone(&fetch_wikidata_suggestions_clone);
|
|
||||||
update_item_clone(index, "name", value.clone());
|
update_item_clone(index, "name", value.clone());
|
||||||
// Show suggestions when typing starts
|
fetch_wikidata_suggestions(format!("name-{}", index), value);
|
||||||
set_show_suggestions.update(|suggestions| {
|
|
||||||
suggestions.insert(format!("name-{}", index), !value.is_empty());
|
|
||||||
});
|
|
||||||
fetch_wiki(format!("name-{}", index), value);
|
|
||||||
}
|
}
|
||||||
key=Arc::new(format!("name-{}", index))
|
key=Arc::new(format!("name-{}", index))
|
||||||
focused_cell=focused_cell
|
focused_cell=focused_cell
|
||||||
|
@ -763,12 +745,25 @@ pub fn ItemsList(
|
||||||
});
|
});
|
||||||
}))
|
}))
|
||||||
on_blur=Some(Callback::new(move |_| {
|
on_blur=Some(Callback::new(move |_| {
|
||||||
set_show_suggestions.update(|suggestions| {
|
log!("Input blurred, delaying hiding suggestions");
|
||||||
suggestions.insert(format!("name-{}", index), false);
|
spawn_local(async move {
|
||||||
|
gloo_timers::future::sleep(std::time::Duration::from_millis(500)).await;
|
||||||
|
log!("Hiding suggestions after delay");
|
||||||
|
set_show_suggestions.update(|suggestions| {
|
||||||
|
suggestions.insert(format!("name-{}", index), false);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}))
|
}))
|
||||||
input_type=InputType::Text
|
input_type=InputType::Text
|
||||||
/>
|
/>
|
||||||
|
<button class="search-icon" on:click=move |_| {
|
||||||
|
log!("Search icon clicked, showing suggestions");
|
||||||
|
set_show_suggestions.update(|suggestions| {
|
||||||
|
suggestions.insert(format!("name-{}", index), true);
|
||||||
|
});
|
||||||
|
}>
|
||||||
|
<i class="fas fa-search"></i> Search Wiki
|
||||||
|
</button>
|
||||||
{move || {
|
{move || {
|
||||||
if *show_suggestions.get().get(&format!("name-{}", index)).unwrap_or(&false) {
|
if *show_suggestions.get().get(&format!("name-{}", index)).unwrap_or(&false) {
|
||||||
log!("Rendering suggestions list");
|
log!("Rendering suggestions list");
|
||||||
|
|
Loading…
Add table
Reference in a new issue