Compware/src/app.rs

64 lines
2.1 KiB
Rust
Raw Normal View History

2024-12-06 14:45:14 +03:00
use leptos::*;
2024-12-10 18:35:39 +03:00
use leptos_meta::*;
use crate::components::{item_form::ItemForm, items_list::ItemsList};
2024-12-17 13:39:41 +03:00
use crate::models::item::{Item, ReviewWithRating};
2024-12-10 22:28:38 +03:00
use crate::nostr::NostrClient;
use tokio::sync::mpsc;
use uuid::Uuid;
2024-12-10 22:28:38 +03:00
use leptos::spawn_local;
use nostr_sdk::serde_json;
2024-12-06 14:45:14 +03:00
#[component]
pub fn App() -> impl IntoView {
2024-12-10 18:35:39 +03:00
provide_meta_context();
// Signal to manage the list of items
let (items_signal, set_items) = create_signal(Vec::<Item>::new());
let (tx, mut rx) = mpsc::channel::<String>(100);
2024-12-10 22:28:38 +03:00
// Nostr client subscription for items
2024-12-10 22:28:38 +03:00
spawn_local(async move {
2024-12-16 13:31:11 +03:00
let nostr_client = NostrClient::new("wss://relay.damus.io").await.unwrap();
2024-12-10 22:28:38 +03:00
nostr_client.subscribe_to_items(tx.clone()).await.unwrap();
while let Some(content) = rx.recv().await {
2024-12-10 22:28:38 +03:00
if let Ok(item) = serde_json::from_str::<Item>(&content) {
set_items.update(|items| items.push(item));
}
}
});
// Add a new item and review using the unified form
2024-12-17 13:39:41 +03:00
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| {
2024-12-10 22:28:38 +03:00
let item = Item {
id: new_id.clone(),
2024-12-17 13:39:41 +03:00
name,
description,
tags,
reviews: vec![ReviewWithRating { content: review.clone(), rating }],
wikidata_id: None,
2024-12-10 22:28:38 +03:00
};
items.push(item);
});
2024-12-17 13:39:41 +03:00
2024-12-10 22:28:38 +03:00
spawn_local(async move {
let nostr_client = NostrClient::new("wss://relay.example.com").await.unwrap();
2024-12-17 13:39:41 +03:00
nostr_client.publish_item("New item added!".to_string(), "".to_string(), vec![]).await.unwrap();
2024-12-06 14:45:14 +03:00
});
};
2024-12-17 13:39:41 +03:00
2024-12-06 14:45:14 +03:00
view! {
<Stylesheet href="/assets/style.css" />
<div>
<h1>{ "CompareWare" }</h1>
// Unified form for adding an item and its first review
<ItemForm on_submit=Box::new(add_item) />
// Display all items, including reviews
<ItemsList items=items_signal />
</div>
2024-12-06 14:45:14 +03:00
}
}