From c1207f613d13796fc97aa2783c7b7bdb3056c68b Mon Sep 17 00:00:00 2001 From: ryan Date: Fri, 24 Jan 2025 02:23:24 +0300 Subject: [PATCH] feat(db): add selected properties state and update save_item_to_db function to include selected properties --- compareware.db | Bin 12288 -> 12288 bytes src/components/items_list.rs | 32 +++++++++++++++++++++++++------- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/compareware.db b/compareware.db index d0da44d58baefb6e826b432ffab981a092c25868..02e08960ce35dac766f720676e70c2fcf6e4feac 100644 GIT binary patch delta 504 zcma)&F-yZh7={xgr3&d;L>v@64g`fBq`6#^6a=*{I$3COiO_4j)Sy;Pu3#ypt-rt` z4&tO+2M6(=xD;3ahKrXvbS>QQ-OKwv-^bgp^=l7nYI?X{Qqv!`SD=G8Max_$;5+l4 zx%kX3oo0HwTB@q+x?8th%chQl9m}_{MQnp%ot_3z!-P9&)oteM?7xDK5vSMUBaLjHd+yf z*8wb@M`0{N)(k}(ZWZ8l1TDBj8hoO&$oEipda}&O^>Pgs?VX!{Pbm1+B-w0T@hJ3hcqX%^K?(+yyvff) z<$ocebrnUihqr`oswuER^en0HSinQcsho!~l#fA?NC~>rU6g(ej1=$-M&L_!{{l|7 Tw5mEw?^::new()); - //signal to store the fetched property labels + // Signal to store the fetched property labels let (property_labels, set_property_labels) = create_signal(HashMap::::new()); + // State to track selected properties + let (selected_properties, set_selected_properties) = create_signal(HashMap::::new()); // Ensure there's an initial empty row if items.get().is_empty() { @@ -47,11 +49,20 @@ pub fn ItemsList( custom_properties: HashMap::new(), }]); } - + // Function to send an item to the backend API - async fn save_item_to_db(item: Item) { + async fn save_item_to_db(item: Item, selected_properties: ReadSignal>) { + // Use a reactive closure to access `selected_properties` + let custom_properties: HashMap = (move || { + let selected_props = selected_properties.get(); // Access the signal inside a reactive closure + item.custom_properties + .into_iter() + .filter(|(key, _)| selected_props.contains_key(key)) // Use the extracted value + .collect() + })(); + // Serialize `custom_properties` to a JSON string - let custom_properties = serde_json::to_string(&item.custom_properties).unwrap(); + let custom_properties = serde_json::to_string(&custom_properties).unwrap(); // Create a new struct to send to the backend #[derive(Serialize, Debug)] @@ -206,14 +217,21 @@ pub fn ItemsList( set_custom_properties.update(|props| { if !props.contains(&property) && !property.is_empty() { props.push(property.clone()); + + //update the selected_properties state when a new property is added + set_selected_properties.update(|selected| { + selected.insert(property.clone(), true); + }); + // Ensure the grid updates reactively set_items.update(|items| { for item in items { item.custom_properties.entry(property.clone()).or_insert_with(|| "".to_string()); + // Save the updated item to the database let item_clone = item.clone(); spawn_local(async move { - save_item_to_db(item_clone).await; + save_item_to_db(item_clone, selected_properties).await; }); } }); @@ -288,7 +306,7 @@ pub fn ItemsList( // Save the updated item to the database let item_clone = item.clone(); spawn_local(async move { - save_item_to_db(item_clone).await; + save_item_to_db(item_clone, selected_properties).await; }); } // Automatically add a new row when editing the last row @@ -304,7 +322,7 @@ pub fn ItemsList( // Save the new item to the database spawn_local(async move { - save_item_to_db(new_item).await; + save_item_to_db(new_item, selected_properties).await; }); } log!("Items updated: {:?}", items);