feat(db): move function to lead items from db outside the items list component.
This commit is contained in:
parent
eba20abf5a
commit
63f11f6a2d
2 changed files with 46 additions and 46 deletions
|
@ -2,7 +2,7 @@ use leptos::*;
|
||||||
use leptos_meta::*;
|
use leptos_meta::*;
|
||||||
use leptos_router::*;
|
use leptos_router::*;
|
||||||
use leptos::logging::log;
|
use leptos::logging::log;
|
||||||
use crate::components::items_list::ItemsList;
|
use crate::components::items_list::{ItemsList, load_items_from_db};
|
||||||
use crate::models::item::Item;
|
use crate::models::item::Item;
|
||||||
use crate::nostr::NostrClient;
|
use crate::nostr::NostrClient;
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
|
|
|
@ -27,6 +27,51 @@ struct DbItem {
|
||||||
custom_properties: String,
|
custom_properties: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//function to load items from database
|
||||||
|
pub async fn load_items_from_db(current_url: &str) -> Result<Vec<Item>, String> {
|
||||||
|
let response = gloo_net::http::Request::get("/api/items?url={}")
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.map_err(|err| format!("Failed to fetch items: {:?}", err))?;
|
||||||
|
|
||||||
|
if response.status() == 200 {
|
||||||
|
// Deserialize into Vec<DbItem>
|
||||||
|
log!("Loading items from DB...");
|
||||||
|
let db_items = response
|
||||||
|
.json::<Vec<DbItem>>()
|
||||||
|
.await
|
||||||
|
.map_err(|err| format!("Failed to parse items: {:?}", err))?;
|
||||||
|
|
||||||
|
// log!("Deserialized DB items: {:?}", db_items);
|
||||||
|
|
||||||
|
// Convert DbItem to Item
|
||||||
|
let items = db_items
|
||||||
|
.into_iter()
|
||||||
|
.map(|db_item| {
|
||||||
|
// Deserialize `custom_properties` from a JSON string to a HashMap
|
||||||
|
let custom_properties: HashMap<String, String> =
|
||||||
|
serde_json::from_str(&db_item.custom_properties)
|
||||||
|
.unwrap_or_default(); // Fallback to an empty HashMap if deserialization fails
|
||||||
|
|
||||||
|
log!("Loaded item: {:?}", db_item.id);
|
||||||
|
log!("Custom properties: {:?}", custom_properties);
|
||||||
|
|
||||||
|
Item {
|
||||||
|
id: db_item.id,
|
||||||
|
name: db_item.name,
|
||||||
|
description: db_item.description,
|
||||||
|
wikidata_id: db_item.wikidata_id,
|
||||||
|
custom_properties,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
// log!("Converted items: {:?}", items);
|
||||||
|
Ok(items)
|
||||||
|
} else {
|
||||||
|
Err(format!("Failed to fetch items: {}", response.status_text()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[component]
|
#[component]
|
||||||
pub fn ItemsList(
|
pub fn ItemsList(
|
||||||
items: ReadSignal<Vec<Item>>,
|
items: ReadSignal<Vec<Item>>,
|
||||||
|
@ -177,51 +222,6 @@ pub fn ItemsList(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//function to load items from database
|
|
||||||
async fn load_items_from_db(url: &str) -> Result<Vec<Item>, String> {
|
|
||||||
let response = gloo_net::http::Request::get("/api/items?url={}")
|
|
||||||
.send()
|
|
||||||
.await
|
|
||||||
.map_err(|err| format!("Failed to fetch items: {:?}", err))?;
|
|
||||||
|
|
||||||
if response.status() == 200 {
|
|
||||||
// Deserialize into Vec<DbItem>
|
|
||||||
log!("Loading items from DB...");
|
|
||||||
let db_items = response
|
|
||||||
.json::<Vec<DbItem>>()
|
|
||||||
.await
|
|
||||||
.map_err(|err| format!("Failed to parse items: {:?}", err))?;
|
|
||||||
|
|
||||||
// log!("Deserialized DB items: {:?}", db_items);
|
|
||||||
|
|
||||||
// Convert DbItem to Item
|
|
||||||
let items = db_items
|
|
||||||
.into_iter()
|
|
||||||
.map(|db_item| {
|
|
||||||
// Deserialize `custom_properties` from a JSON string to a HashMap
|
|
||||||
let custom_properties: HashMap<String, String> =
|
|
||||||
serde_json::from_str(&db_item.custom_properties)
|
|
||||||
.unwrap_or_default(); // Fallback to an empty HashMap if deserialization fails
|
|
||||||
|
|
||||||
log!("Loaded item: {:?}", db_item.id);
|
|
||||||
log!("Custom properties: {:?}", custom_properties);
|
|
||||||
|
|
||||||
Item {
|
|
||||||
id: db_item.id,
|
|
||||||
name: db_item.name,
|
|
||||||
description: db_item.description,
|
|
||||||
wikidata_id: db_item.wikidata_id,
|
|
||||||
custom_properties,
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
// log!("Converted items: {:?}", items);
|
|
||||||
Ok(items)
|
|
||||||
} else {
|
|
||||||
Err(format!("Failed to fetch items: {}", response.status_text()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Function to remove an item
|
// Function to remove an item
|
||||||
let remove_item = move |index: usize| {
|
let remove_item = move |index: usize| {
|
||||||
let item_id = items.get()[index].id.clone();
|
let item_id = items.get()[index].id.clone();
|
||||||
|
|
Loading…
Add table
Reference in a new issue