From fddec7f728a902f4ddd1c4bc00457126f2544cf8 Mon Sep 17 00:00:00 2001 From: ryan Date: Thu, 13 Feb 2025 23:06:26 +0300 Subject: [PATCH] feat(url): edit items list component to include current URL in database query --- src/components/items_list.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/components/items_list.rs b/src/components/items_list.rs index e095b5f..d1c9db8 100644 --- a/src/components/items_list.rs +++ b/src/components/items_list.rs @@ -8,9 +8,7 @@ use crate::models::item::Item; use std::collections::HashMap; use std::sync::Arc; use wasm_bindgen::JsCast; -use urlencoding::encode; -use gloo_net::http::Request; -use serde_json::Value; +use web_sys::window; #[derive(Deserialize, Clone, Debug)] struct WikidataSuggestion { @@ -52,8 +50,16 @@ pub fn ItemsList( // Signal to store the fetched property labels let (property_labels, set_property_labels) = create_signal(HashMap::::new()); + fn get_current_url() -> String { + window() + .and_then(|win| win.location().href().ok()) + .unwrap_or_else(|| "".to_string()) + } + + let current_url = get_current_url(); + spawn_local(async move { - match load_items_from_db().await { + match load_items_from_db(¤t_url).await { Ok(loaded_items) => { // Set the loaded items if loaded_items.is_empty() { @@ -172,8 +178,8 @@ pub fn ItemsList( } //function to load items from database - async fn load_items_from_db() -> Result, String> { - let response = gloo_net::http::Request::get("/api/items") + async fn load_items_from_db(url: &str) -> Result, String> { + let response = gloo_net::http::Request::get("/api/items?url={}") .send() .await .map_err(|err| format!("Failed to fetch items: {:?}", err))?;