feat(autocomplete name): autofill name with wikidata suggestions

This commit is contained in:
ryan 2024-12-23 17:56:34 +03:00
parent 22e6ae38d4
commit e9b4c12a6d
8 changed files with 90 additions and 42 deletions

11
.idea/Compware.iml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="EMPTY_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/Compware.iml" filepath="$PROJECT_DIR$/.idea/Compware.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

View File

@ -7,7 +7,12 @@
<cargoProject FILE="$PROJECT_DIR$/Cargo.toml" /> <cargoProject FILE="$PROJECT_DIR$/Cargo.toml" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="eb7658c0-6daa-4def-a5ad-3d491e9b76c8" name="Changes" comment="" /> <list default="true" id="eb7658c0-6daa-4def-a5ad-3d491e9b76c8" name="Changes" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/app.rs" beforeDir="false" afterPath="$PROJECT_DIR$/src/app.rs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/components/editable_cell.rs" beforeDir="false" afterPath="$PROJECT_DIR$/src/components/editable_cell.rs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/components/items_list.rs" beforeDir="false" afterPath="$PROJECT_DIR$/src/components/items_list.rs" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" /> <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
@ -28,26 +33,31 @@
}</component> }</component>
<component name="ProjectId" id="2pngw94pMiCvlaySWR2RUofHfwc" /> <component name="ProjectId" id="2pngw94pMiCvlaySWR2RUofHfwc" />
<component name="ProjectViewState"> <component name="ProjectViewState">
<option name="autoscrollToSource" value="true" />
<option name="hideEmptyMiddlePackages" value="true" /> <option name="hideEmptyMiddlePackages" value="true" />
<option name="openDirectoriesWithSingleClick" value="true" />
<option name="showLibraryContents" value="true" /> <option name="showLibraryContents" value="true" />
</component> </component>
<component name="PropertiesComponent">{ <component name="PropertiesComponent"><![CDATA[{
&quot;keyToString&quot;: { "keyToString": {
&quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;, "Cargo.Run compareware.executor": "Run",
&quot;RunOnceActivity.git.unshallow&quot;: &quot;true&quot;, "RunOnceActivity.ShowReadmeOnStart": "true",
&quot;RunOnceActivity.rust.reset.selective.auto.import&quot;: &quot;true&quot;, "RunOnceActivity.git.unshallow": "true",
&quot;git-widget-placeholder&quot;: &quot;main&quot;, "RunOnceActivity.rust.reset.selective.auto.import": "true",
&quot;node.js.detected.package.eslint&quot;: &quot;true&quot;, "git-widget-placeholder": "editable-cells-wiki",
&quot;node.js.detected.package.tslint&quot;: &quot;true&quot;, "last_opened_file_path": "/home/noah/Desktop/Internship_tasks/Compware",
&quot;node.js.selected.package.eslint&quot;: &quot;(autodetect)&quot;, "node.js.detected.package.eslint": "true",
&quot;node.js.selected.package.tslint&quot;: &quot;(autodetect)&quot;, "node.js.detected.package.tslint": "true",
&quot;nodejs_package_manager_path&quot;: &quot;npm&quot;, "node.js.selected.package.eslint": "(autodetect)",
&quot;org.rust.cargo.project.model.PROJECT_DISCOVERY&quot;: &quot;true&quot;, "node.js.selected.package.tslint": "(autodetect)",
&quot;org.rust.cargo.project.model.impl.CargoExternalSystemProjectAware.subscribe.first.balloon&quot;: &quot;&quot;, "nodejs_package_manager_path": "npm",
&quot;org.rust.first.attach.projects&quot;: &quot;true&quot;, "org.rust.cargo.project.model.PROJECT_DISCOVERY": "true",
&quot;vue.rearranger.settings.migration&quot;: &quot;true&quot; "org.rust.cargo.project.model.impl.CargoExternalSystemProjectAware.subscribe.first.balloon": "",
"org.rust.first.attach.projects": "true",
"settings.editor.selected.configurable": "settings.sync",
"vue.rearranger.settings.migration": "true"
} }
}</component> }]]></component>
<component name="RunManager" selected="Cargo.Run compareware"> <component name="RunManager" selected="Cargo.Run compareware">
<configuration name="Run compareware" type="CargoCommandRunConfiguration" factoryName="Cargo Command"> <configuration name="Run compareware" type="CargoCommandRunConfiguration" factoryName="Cargo Command">
<option name="buildProfileId" value="dev" /> <option name="buildProfileId" value="dev" />
@ -97,6 +107,7 @@
<option name="presentableId" value="Default" /> <option name="presentableId" value="Default" />
<updated>1733407491778</updated> <updated>1733407491778</updated>
<workItem from="1733407498746" duration="5306000" /> <workItem from="1733407498746" duration="5306000" />
<workItem from="1734955316600" duration="1248000" />
</task> </task>
<servers /> <servers />
</component> </component>

View File

@ -1,7 +1,7 @@
use leptos::*; use leptos::*;
use leptos_meta::*; use leptos_meta::*;
use crate::components::items_list::ItemsList; use crate::components::items_list::ItemsList;
use crate::models::item::{Item, ReviewWithRating}; use crate::models::item::Item;
use crate::nostr::NostrClient; use crate::nostr::NostrClient;
use tokio::sync::mpsc; use tokio::sync::mpsc;
use uuid::Uuid; use uuid::Uuid;

View File

@ -9,7 +9,7 @@ pub fn EditableCell(
let handle_input = move |e: web_sys::Event| { let handle_input = move |e: web_sys::Event| {
let new_value = event_target_value(&e); let new_value = event_target_value(&e);
set_input_value(new_value.clone()); set_input_value.set(new_value.clone());
on_input(new_value); on_input(new_value);
}; };

View File

@ -3,6 +3,9 @@ use crate::components::tag_editor::TagEditor;
use leptos::*; use leptos::*;
use serde::Deserialize; use serde::Deserialize;
use uuid::Uuid; use uuid::Uuid;
use leptos::logging::log;
use crate::models::item::Item;
use std::sync::{Arc, Mutex};
#[derive(Deserialize, Clone, Debug)] #[derive(Deserialize, Clone, Debug)]
struct WikidataSuggestion { struct WikidataSuggestion {
@ -11,14 +14,6 @@ struct WikidataSuggestion {
description: Option<String>, description: Option<String>,
} }
#[derive(Clone, Debug)]
struct Item {
id: String,
name: String,
description: String,
tags: Vec<(String, String)>,
wikidata_id: Option<String>,
}
#[component] #[component]
pub fn ItemsList( pub fn ItemsList(
@ -32,7 +27,7 @@ pub fn ItemsList(
let fetch_wikidata_suggestions = move |query: String| { let fetch_wikidata_suggestions = move |query: String| {
spawn_local(async move { spawn_local(async move {
if query.is_empty() { if query.is_empty() {
set_wikidata_suggestions(Vec::new()); set_wikidata_suggestions.set(Vec::new());
return; return;
} }
@ -44,7 +39,7 @@ pub fn ItemsList(
match gloo_net::http::Request::get(&url).send().await { match gloo_net::http::Request::get(&url).send().await {
Ok(response) => { Ok(response) => {
if let Ok(data) = response.json::<WikidataResponse>().await { if let Ok(data) = response.json::<WikidataResponse>().await {
set_wikidata_suggestions(data.search); set_wikidata_suggestions.set(data.search);
} }
} }
Err(_) => log!("Failed to fetch Wikidata suggestions"), Err(_) => log!("Failed to fetch Wikidata suggestions"),
@ -87,13 +82,14 @@ pub fn ItemsList(
}; };
// Add a new item // Add a new item
let add_item = move || { let add_item = move |_: web_sys::MouseEvent|{
set_items.update(|items| { set_items.update(|items| {
items.push(Item { items.push(Item {
id: Uuid::new_v4().to_string(), id: Uuid::new_v4().to_string(),
name: String::new(), name: String::new(),
description: String::new(), description: String::new(),
tags: vec![], tags: vec![],
reviews:vec![],
wikidata_id: None, wikidata_id: None,
}); });
}); });
@ -131,17 +127,24 @@ pub fn ItemsList(
/> />
<ul> <ul>
{move || { {move || {
wikidata_suggestions.get().iter().map(|suggestion| { let suggestions = wikidata_suggestions.get().to_vec();
suggestions.into_iter().map(|suggestion| {
// Clone all necessary fields upfront
let label_for_click = suggestion.label.clone();
let label_for_display = suggestion.label.clone();
let description = suggestion.description.clone().unwrap_or_default();
let id = suggestion.id.clone();
view! { view! {
<li on:click=move |_| { <li on:click=move |_| {
set_items.update(|items| { set_items.update(|items| {
if let Some(item) = items.get_mut(index) { if let Some(item) = items.get_mut(index) {
item.wikidata_id = Some(suggestion.id.clone()); item.wikidata_id = Some(id.clone());
item.name = suggestion.label.clone(); item.name = label_for_click.clone(); // Use the cloned version for updating
} }
}); });
}> }>
{ format!("{} - {}", suggestion.label, suggestion.description.clone().unwrap_or_default()) } { format!("{} - {}", label_for_display, description) } // Use the cloned version for display
</li> </li>
} }
@ -161,7 +164,7 @@ pub fn ItemsList(
<TagEditor <TagEditor
tags=item.tags.clone() tags=item.tags.clone()
on_add=move |key, value| add_tag(index, key, value) on_add=move |key, value| add_tag(index, key, value)
on_remove=move |tag_index| remove_tag(index, tag_index) on_remove=Arc::new (Mutex:: new (move |tag_index:usize| remove_tag(index, tag_index)))
/> />
</td> </td>
// Actions // Actions

View File

@ -1,19 +1,22 @@
use leptos::*; use leptos::*;
use std::sync::{Arc, Mutex};
#[component] #[component]
pub fn TagEditor( pub fn TagEditor(
tags: Vec<(String, String)>, tags: Vec<(String, String)>,
on_add: impl Fn(String, String) + 'static, on_add: impl Fn(String, String) + 'static,
on_remove: impl Fn(usize) + 'static, on_remove: Arc<Mutex<dyn FnMut(usize) + Send + Sync>>,
) -> impl IntoView { ) -> impl IntoView {
let (key, set_key) = create_signal(String::new()); let (key, set_key) = create_signal(String::new());
let (value, set_value) = create_signal(String::new()); let (value, set_value) = create_signal(String::new());
let add_tag = move || { let add_tag = move |_| {
if !key.get().is_empty() && !value.get().is_empty() { if !key.get().is_empty() && !value.get().is_empty() {
on_add(key.get(), value.get()); on_add(key.get(), value.get());
set_key(String::new()); set_key.set(String::new());
set_value(String::new()); set_value.set(String::new());
} }
}; };
@ -21,10 +24,16 @@ pub fn TagEditor(
<div> <div>
<ul> <ul>
{tags.iter().enumerate().map(|(index, (k, v))| { {tags.iter().enumerate().map(|(index, (k, v))| {
let on_remove = on_remove.clone();
view! { view! {
<li> <li>
{format!("{}: {}", k, v)} {format!("{}: {}", k, v)}
<button on:click=move |_| on_remove(index)>{ "Remove" }</button> <button on:click=move |_| {
let mut on_remove = on_remove.lock().unwrap();
on_remove(index);
}>
{ "Remove" }
</button>
</li> </li>
} }
}).collect::<Vec<_>>()} }).collect::<Vec<_>>()}
@ -32,12 +41,12 @@ pub fn TagEditor(
<input <input
placeholder="Key" placeholder="Key"
value={key.get()} value={key.get()}
on:input=move |e| set_key(event_target_value(&e)) on:input=move |e| set_key.set(event_target_value(&e))
/> />
<input <input
placeholder="Value" placeholder="Value"
value={value.get()} value={value.get()}
on:input=move |e| set_value(event_target_value(&e)) on:input=move |e| set_value.set(event_target_value(&e))
/> />
<button on:click=add_tag>{ "Add Tag" }</button> <button on:click=add_tag>{ "Add Tag" }</button>
</div> </div>