From e90866fbd99434ec4117d3314704307c858b268d Mon Sep 17 00:00:00 2001 From: Ryan Mwangi Date: Tue, 17 Dec 2024 13:39:41 +0300 Subject: [PATCH] feat(rating): add rating system 1-5 --- src/app.rs | 23 +++++----- src/components/item_form.rs | 81 ++++++++++++------------------------ src/components/items_list.rs | 11 ++--- src/models/item.rs | 10 ++++- 4 files changed, 51 insertions(+), 74 deletions(-) diff --git a/src/app.rs b/src/app.rs index 880bab6..d12ac5e 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}; -use crate::models::item::Item; +use crate::models::item::{Item, ReviewWithRating}; use crate::nostr::NostrClient; use tokio::sync::mpsc; use uuid::Uuid; @@ -29,27 +29,26 @@ pub fn App() -> impl IntoView { }); // 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 - + let add_item = move |name: String, description: String, tags: Vec<(String, String)>, review: String, rating: u8| { + let new_id = Uuid::new_v4().to_string(); + set_items.update(|items| { let item = Item { id: new_id.clone(), - name: name.clone(), - description: description.clone(), - tags: tags.clone(), - reviews: vec![review.clone()], // Initialize reviews + name, + description, + tags, + reviews: vec![ReviewWithRating { content: review.clone(), rating }], }; 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(); + nostr_client.publish_item("New item added!".to_string(), "".to_string(), vec![]).await.unwrap(); }); }; - + view! {
diff --git a/src/components/item_form.rs b/src/components/item_form.rs index 0c1114c..873352c 100644 --- a/src/components/item_form.rs +++ b/src/components/item_form.rs @@ -2,17 +2,15 @@ use leptos::*; use leptos_dom::ev::SubmitEvent; #[component] -pub fn ItemForm( - on_submit: Box, String) + 'static> -) -> impl IntoView { +pub fn ItemForm(on_submit: Box, String, u8)>) -> 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()); + let (rating, set_rating) = create_signal(5u8); // Default rating to 5 - // Handle adding a new tag let add_tag = move |_| { if !tag_key.get().is_empty() && !tag_value.get().is_empty() { set_tags.update(|t| t.push((tag_key.get(), tag_value.get()))); @@ -21,75 +19,48 @@ pub fn ItemForm( } }; - // Handle form submission. let handle_submit = move |ev: SubmitEvent| { ev.prevent_default(); on_submit( - name.get(), // Item name - description.get(), // Item description - tags.get().clone(), // Tags - review.get(), // Review + name.get(), + description.get(), + tags.get().clone(), + review.get(), + rating.get(), ); - // Reset values after submission + // Reset values set_name.set(String::new()); set_description.set(String::new()); set_tags.set(vec![]); set_review.set(String::new()); + set_rating.set(5); }; view! {
- // Item Name Input - - - // Item Description Input -