From 5bd19803fe2c73b88833b5fbb0f789428ffef47c Mon Sep 17 00:00:00 2001 From: ryan Date: Wed, 22 Jan 2025 02:50:00 +0300 Subject: [PATCH] feat(ssr): add SSR feature to ItemsList component --- Cargo.toml | 3 +- items.db | Bin 0 -> 12288 bytes src/components/items_list.rs | 55 +++++++++++++++++++++-------------- 3 files changed, 35 insertions(+), 23 deletions(-) create mode 100644 items.db diff --git a/Cargo.toml b/Cargo.toml index d011d68..db698c2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,6 +31,7 @@ thiserror = "2.0.9" zerofrom = "0.1" [features] +default = ["ssr"] csr = ["leptos/csr", "leptos_meta/csr", "leptos_router/csr"] hydrate = ["leptos/hydrate", "leptos_meta/hydrate", "leptos_router/hydrate"] ssr = [ @@ -40,7 +41,7 @@ ssr = [ "leptos/ssr", "leptos_meta/ssr", "leptos_router/ssr", - "rusqlite" + "dep:rusqlite" ] # Override secp256k1's default features diff --git a/items.db b/items.db new file mode 100644 index 0000000000000000000000000000000000000000..d0da44d58baefb6e826b432ffab981a092c25868 GIT binary patch literal 12288 zcmeI#yH3L}6b4{BTve%;E$bPngpd$3f`Bq0LV;i;Iz^^2l0|bNaYbi%wvIdiZ^B4y z=tNNW>OYcgo#Z&l`7-(LHqu7XQ&DBo&=DIj&eS-U{9POQI91JoHmlv_ zoyTDyLK4AQ6q36{dmU;CostM21l`0JS3x|c%W%BkTA0gB{gjTb1&!{bsJ$*#^`z3J z(M9gc+pAvnvreUv$?yByt0uGB6j@SMMX4&IRqdYm=kta`Z`(iiIn#Nn=Jm_8X*iKH zQ@A{F?I(wBvFG|->)Ex_z)lDVKmY;|fB*y_009U<00Izz00h=mpko`u`F~x1FZzN2 W1Rwwb2tWV=5P$##AOHafIDu~hjaG#K literal 0 HcmV?d00001 diff --git a/src/components/items_list.rs b/src/components/items_list.rs index 8f1eaf5..a649390 100644 --- a/src/components/items_list.rs +++ b/src/components/items_list.rs @@ -8,6 +8,7 @@ use crate::models::item::Item; use std::collections::HashMap; use std::sync::Arc; use wasm_bindgen::JsCast; + #[cfg(feature = "ssr")] use crate::db::{Database, DbItem}; @@ -38,25 +39,23 @@ pub fn ItemsList( //signal to store the fetched property labels let (property_labels, set_property_labels) = create_signal(HashMap::::new()); + #[cfg(feature = "ssr")] + { + log!("SSR feature is enabled, attempting to update database..."); + log!("Is SSR enabled? {}", cfg!(feature = "ssr")); let db_path = "items.db"; // path to the database file let db = Database::new(db_path).unwrap(); db.create_schema().unwrap(); let db_items = db.get_items().unwrap(); let loaded_items: Vec = db_items.into_iter().map(|db_item| { - Item { - id: db_item.id, - name: db_item.name, - description: db_item.description, - wikidata_id: db_item.wikidata_id, - custom_properties: serde_json::from_str(&db_item.custom_properties).unwrap(), - } + serde_json::from_str(&db_item.custom_properties).unwrap() }).collect(); spawn_local(async move { set_items.set(loaded_items); }); - + } // Ensure there's an initial empty row if items.get().is_empty() { set_items.set(vec![Item { @@ -215,7 +214,7 @@ pub fn ItemsList( let property_clone = property.clone(); spawn_local(async move { let properties = fetch_item_properties(&wikidata_id, set_fetched_properties, set_property_labels).await; - log!("Fetched properties for Wikidata ID {}: {:?}", wikidata_id, properties); + // log!("Fetched properties for Wikidata ID {}: {:?}", wikidata_id, properties); if let Some(value) = properties.get(&property_clone) { set_items.update(|items| { if let Some(item) = items.iter_mut().find(|item| item.wikidata_id.as_ref().unwrap() == &wikidata_id) { @@ -231,6 +230,8 @@ pub fn ItemsList( // Update item fields let update_item = move |index: usize, field: &str, value: String| { + log!("Updating item at index {}: {}, {}", index, field, value); + log!("Is SSR enabled? {}", cfg!(feature = "ssr")); set_items.update(|items| { if let Some(item) = items.get_mut(index) { match field { @@ -246,7 +247,7 @@ pub fn ItemsList( let set_property_labels = set_property_labels.clone(); spawn_local(async move { let properties = fetch_item_properties(&wikidata_id, set_fetched_properties, set_property_labels).await; - log!("Fetched properties for index {}: {:?}", index, properties); + // log!("Fetched properties for index {}: {:?}", index, properties); }); } } @@ -260,17 +261,27 @@ pub fn ItemsList( } } } - // //update items in the database - // let db_item = DbItem { - // id: items[index].id.clone(), - // name: items[index].name.clone(), - // description: items[index].description.clone(), - // wikidata_id: items[index].wikidata_id.clone(), - // custom_properties: serde_json::to_string(&items[index].custom_properties).unwrap(), - // }; - // db.insert_item(&db_item).unwrap(); - - + #[cfg(feature = "ssr")] + { + log!("SSR block in update_item is executing"); + // Update items in the database + let db_item = DbItem { + id: items[index].id.clone(), + name: items[index].name.clone(), + description: items[index].description.clone(), + wikidata_id: items[index].wikidata_id.clone(), + custom_properties: serde_json::to_string(&items[index].custom_properties).unwrap(), + }; + let db = Database::new("items.db").expect("Failed to open database"); + match db.insert_item(&db_item) { + Ok(_) => { + log!("Item inserted successfully"); + } + Err(e) => { + log!("Error inserting item: {}", e); + } + } + } // Automatically add a new row when editing the last row if index == items.len() - 1 && !value.is_empty() { items.push(Item { @@ -392,7 +403,7 @@ pub fn ItemsList( let set_property_labels = set_property_labels.clone(); spawn_local(async move { let properties = fetch_item_properties(&wikidata_id, set_fetched_properties, set_property_labels).await; - log!("Fetched properties for Wikidata ID {}: {:?}", wikidata_id, properties); + // log!("Fetched properties for Wikidata ID {}: {:?}", wikidata_id, properties); // Populate the custom properties for the new item set_items.update(|items| {