fix(reviews): handle review item_id to stop panic while serving

This commit is contained in:
Ryan Mwangi 2024-12-16 22:16:55 +03:00
parent b42afb0bfc
commit bbc4f9a2cd
1 changed files with 19 additions and 10 deletions

View File

@ -13,27 +13,34 @@ use nostr_sdk::serde_json;
#[component] #[component]
pub fn App() -> impl IntoView { pub fn App() -> impl IntoView {
provide_meta_context(); provide_meta_context();
// Signal to store and update the list of items. // Signal to store and update the list of items.
let (items_signal, set_items) = create_signal(Vec::<Item>::new()); let (items_signal, set_items) = create_signal(Vec::<Item>::new());
// Signal to store the ID of the current item for reviews
let (current_item_id, set_current_item_id) = create_signal(String::new());
let (tx, mut rx) = mpsc::channel::<String>(100); let (tx, mut rx) = mpsc::channel::<String>(100);
spawn_local(async move { spawn_local(async move {
//initialize nostr client // Initialize Nostr client
let nostr_client = NostrClient::new("wss://relay.damus.io").await.unwrap(); let nostr_client = NostrClient::new("wss://relay.damus.io").await.unwrap();
nostr_client.subscribe_to_items(tx.clone()).await.unwrap(); nostr_client.subscribe_to_items(tx.clone()).await.unwrap();
//handle incoming events // Handle incoming events
while let Some(content) = rx.recv().await { while let Some(content) = rx.recv().await {
if let Ok(item) = serde_json::from_str::<Item>(&content) { if let Ok(item) = serde_json::from_str::<Item>(&content) {
set_items.update(|items| items.push(item)); set_items.update(|items| items.push(item));
} }
} }
}); });
// Function to handle adding a new item to the list. // Function to handle adding a new item to the list.
let add_item = move |name: String, description: String, tags: Vec<(String, String)>| { 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
set_items.update(|items| { set_items.update(|items| {
let item = Item { let item = Item {
id: Uuid::new_v4().to_string(), id: new_id,
name: name.clone(), name: name.clone(),
description: description.clone(), description: description.clone(),
tags: tags.clone(), tags: tags.clone(),
@ -49,8 +56,9 @@ pub fn App() -> impl IntoView {
}; };
// Handle review submission // Handle review submission
let submit_review = move |_| { let submit_review = move |review_content: String| {
// Handle the review submission logic // Logic for submitting a review
println!("Review submitted: {}", review_content);
}; };
view! { view! {
@ -60,10 +68,11 @@ pub fn App() -> impl IntoView {
<h1>{ "CompareWare" }</h1> <h1>{ "CompareWare" }</h1>
// Form component for adding new items. // Form component for adding new items.
<ItemForm on_submit=Box::new(add_item) /> <ItemForm on_submit=Box::new(add_item) />
// Reviews form
<ReviewForm item_id={current_item_id.get()} on_submit={Box::new(submit_review)} />
// Component to display the list of items. // Component to display the list of items.
<ItemsList items=items_signal /> <ItemsList items=items_signal />
// Reviews form and list // Component to display the list of reviews for the current item.
<ReviewForm item_id={items_signal.get().first().unwrap().id.clone()} on_submit={Box::new(submit_review)} />
<ReviewsList reviews={vec![]} /> <ReviewsList reviews={vec![]} />
</div> </div>
</> </>