diff --git a/src/app.rs b/src/app.rs index 03b593c..880bab6 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,7 +1,7 @@ use leptos::*; use leptos_meta::*; -use crate::components::{item_form::ItemForm, items_list::ItemsList, review_form::ReviewForm, reviews_list::ReviewsList}; -use crate::models::{item::Item, review::Review}; // Ensure Review is imported +use crate::components::{item_form::ItemForm, items_list::ItemsList}; +use crate::models::item::Item; use crate::nostr::NostrClient; use tokio::sync::mpsc; use uuid::Uuid; @@ -12,20 +12,15 @@ use nostr_sdk::serde_json; pub fn App() -> impl IntoView { provide_meta_context(); - // Signal to store and update the list of items. + // Signal to manage the list of items let (items_signal, set_items) = create_signal(Vec::::new()); - // Signal to store the ID of the current item for reviews - let (current_item_id, set_current_item_id) = create_signal(String::new()); - // Signal to store reviews for the current item - let (reviews_signal, set_reviews) = create_signal(Vec::::new()); let (tx, mut rx) = mpsc::channel::(100); + // Nostr client subscription for items spawn_local(async move { - // Initialize Nostr client let nostr_client = NostrClient::new("wss://relay.damus.io").await.unwrap(); nostr_client.subscribe_to_items(tx.clone()).await.unwrap(); - // Handle incoming events while let Some(content) = rx.recv().await { if let Ok(item) = serde_json::from_str::(&content) { set_items.update(|items| items.push(item)); @@ -33,60 +28,36 @@ pub fn App() -> impl IntoView { } }); - // Function to handle adding a new item to the list. - let add_item = move |name: String, description: String, tags: Vec<(String, String)>| { - let new_id = Uuid::new_v4().to_string(); // Generate a new UUID for the item - set_current_item_id.set(new_id.clone()); // Update the current item ID + // Add a new item and review using the unified form + let add_item = move |name: String, description: String, tags: Vec<(String, String)>, review: String| { + let new_id = Uuid::new_v4().to_string(); // Generate a unique ID set_items.update(|items| { let item = Item { - id: new_id, + id: new_id.clone(), name: name.clone(), description: description.clone(), tags: tags.clone(), - reviews: vec![], + reviews: vec![review.clone()], // Initialize reviews }; items.push(item); }); + // Publish item to Nostr spawn_local(async move { let nostr_client = NostrClient::new("wss://relay.example.com").await.unwrap(); nostr_client.publish_item(name, description, tags).await.unwrap(); }); }; - // Handle review submission - let submit_review = { - let current_item_id = current_item_id.clone(); // Clone the current item ID - let user_id = "some_user_id".to_string(); // Replace with actual user ID logic - - move |review_content: String| { - // Create a new review and add it to the reviews list - let new_review = Review { - content: review_content.clone(), - item_id: current_item_id.get().clone(), // Use the current item ID - user_id: user_id.clone(), // Use the user ID - }; - set_reviews.update(|reviews| reviews.push(new_review)); - println!("Review submitted: {}", review_content); - println!("Current reviews: {:?}", reviews_signal.get()); - } - }; - view! { - <> - -
-

{ "CompareWare" }

- // Form component for adding new items. - - // Reviews form - - // Component to display the list of items. - - // Component to display the list of reviews for the current item. - -
- + +
+

{ "CompareWare" }

+ // Unified form for adding an item and its first review + + // Display all items, including reviews + +
} -} \ No newline at end of file +} diff --git a/src/components/item_form.rs b/src/components/item_form.rs index 67763e8..0c1114c 100644 --- a/src/components/item_form.rs +++ b/src/components/item_form.rs @@ -2,12 +2,15 @@ use leptos::*; use leptos_dom::ev::SubmitEvent; #[component] -pub fn ItemForm(on_submit: Box)>) -> impl IntoView { +pub fn ItemForm( + on_submit: Box, String) + 'static> +) -> impl IntoView { let (name, set_name) = create_signal(String::new()); let (description, set_description) = create_signal(String::new()); let (tags, set_tags) = create_signal(Vec::<(String, String)>::new()); let (tag_key, set_tag_key) = create_signal(String::new()); let (tag_value, set_tag_value) = create_signal(String::new()); + let (review, set_review) = create_signal(String::new()); // Handle adding a new tag let add_tag = move |_| { @@ -21,27 +24,38 @@ pub fn ItemForm(on_submit: Box)>) - // Handle form submission. let handle_submit = move |ev: SubmitEvent| { ev.prevent_default(); - on_submit(name.get(), description.get(), tags.get().clone()); + on_submit( + name.get(), // Item name + description.get(), // Item description + tags.get().clone(), // Tags + review.get(), // Review + ); // Reset values after submission - set_name.update(|n| *n = String::new()); - set_description.update(|d| *d = String::new()); - set_tags.update(|t| t.clear()); + set_name.set(String::new()); + set_description.set(String::new()); + set_tags.set(vec![]); + set_review.set(String::new()); }; view! {
+ // Item Name Input + + // Item Description Input