use leptos::prelude::*;
use leptos::html;
use leptos::task::spawn_local;
use crate::store::TorrentStore;
use crate::api;
#[component]
pub fn AddTorrentDialog(
on_close: Callback<()>,
) -> impl IntoView {
let store = use_context::().expect("TorrentStore not provided");
let notifications = store.notifications;
let dialog_ref = NodeRef::::new();
let uri = signal(String::new());
let is_loading = signal(false);
let error_msg = signal(Option::::None);
Effect::new(move |_| {
if let Some(dialog) = dialog_ref.get() {
let _ = dialog.show_modal();
}
});
let handle_submit = move |ev: web_sys::SubmitEvent| {
ev.prevent_default();
let uri_val = uri.0.get();
if uri_val.is_empty() {
error_msg.1.set(Some("Please enter a Magnet URI or URL".to_string()));
return;
}
is_loading.1.set(true);
error_msg.1.set(None);
let on_close = on_close.clone();
spawn_local(async move {
match api::torrent::add(&uri_val).await {
Ok(_) => {
log::info!("Torrent added successfully");
crate::store::show_toast_with_signal(
notifications,
shared::NotificationLevel::Success,
"Torrent başarıyla eklendi"
);
if let Some(dialog) = dialog_ref.get() {
dialog.close();
}
on_close.run(());
}
Err(e) => {
log::error!("Failed to add torrent: {:?}", e);
error_msg.1.set(Some(format!("Hata: {:?}", e)));
is_loading.1.set(false);
}
}
});
};
let handle_cancel = move |_| {
if let Some(dialog) = dialog_ref.get() {
dialog.close();
}
on_close.run(());
};
view! {
}
}