From 9f28d30d480e7b3633d7f32cd7310b28b9a93046 Mon Sep 17 00:00:00 2001 From: ryan Date: Tue, 24 Dec 2024 15:26:52 +0300 Subject: [PATCH] feat(list): Add initial empty row and auto-add new row on editing last row --- src/app.rs | 15 -------- src/components/items_list.rs | 69 ++++++++++++++++++++---------------- 2 files changed, 39 insertions(+), 45 deletions(-) diff --git a/src/app.rs b/src/app.rs index 8cf312b..e9a0812 100644 --- a/src/app.rs +++ b/src/app.rs @@ -4,7 +4,6 @@ use crate::components::items_list::ItemsList; use crate::models::item::Item; use crate::nostr::NostrClient; use tokio::sync::mpsc; -use uuid::Uuid; use leptos::spawn_local; use nostr_sdk::serde_json; @@ -28,24 +27,10 @@ pub fn App() -> impl IntoView { } }); - // Handle adding a new item - let add_item = move || { - let new_item = Item { - id: Uuid::new_v4().to_string(), - name: "New Item".to_string(), - description: String::new(), - tags: vec![], - reviews: vec![], - wikidata_id: None, - }; - set_items.update(|items| items.push(new_item)); - }; - view! {

{ "CompareWare" }

-
} diff --git a/src/components/items_list.rs b/src/components/items_list.rs index b6f8d5e..c279b3b 100644 --- a/src/components/items_list.rs +++ b/src/components/items_list.rs @@ -14,12 +14,22 @@ struct WikidataSuggestion { description: Option, } - #[component] pub fn ItemsList( items: ReadSignal>, set_items: WriteSignal>, ) -> impl IntoView { + + // Ensure there's an initial empty row + set_items.set(vec![Item { + id: Uuid::new_v4().to_string(), + name: String::new(), + description: String::new(), + tags: vec![], + reviews: vec![], + wikidata_id: None, + }]); + let (wikidata_suggestions, set_wikidata_suggestions) = create_signal(Vec::::new()); @@ -54,12 +64,26 @@ pub fn ItemsList( match field { "name" => { item.name = value.clone(); - fetch_wikidata_suggestions(value); + fetch_wikidata_suggestions(value.clone()); + } + "description" => { + item.description = value.clone(); } - "description" => item.description = value, _ => (), } } + + // Automatically add a new row when editing the last row + if index == items.len() - 1 && !value.is_empty() { + items.push(Item { + id: Uuid::new_v4().to_string(), + name: String::new(), + description: String::new(), + tags: vec![], + reviews: vec![], + wikidata_id: None, + }); + } }); }; @@ -81,20 +105,6 @@ pub fn ItemsList( }); }; - // Add a new item - let add_item = move |_: web_sys::MouseEvent|{ - set_items.update(|items| { - items.push(Item { - id: Uuid::new_v4().to_string(), - name: String::new(), - description: String::new(), - tags: vec![], - reviews: vec![], - wikidata_id: None, - }); - }); - }; - // Remove an item let remove_item = move |index: usize| { set_items.update(|items| { @@ -105,7 +115,6 @@ pub fn ItemsList( view! {

{ "Items List" }

- @@ -121,11 +130,11 @@ pub fn ItemsList( // Editable Name Field with Wikidata Integration // Tag Editor
- +
    {move || { let suggestions = wikidata_suggestions.get().to_vec(); @@ -141,7 +150,7 @@ pub fn ItemsList( ("source".to_string(), "wikidata".to_string()), ("wikidata_id".to_string(), id.clone()), ]; - + view! {
  • // Editable Description Field
- + @@ -192,4 +201,4 @@ pub fn ItemsList( #[derive(Deserialize, Clone, Debug)] struct WikidataResponse { search: Vec, -} +} \ No newline at end of file