Files
vibetorrent/frontend/src/components/torrent/add_torrent.rs
spinline cffc88443a
All checks were successful
Build MIPS Binary / build (push) Successful in 5m18s
feat: add centralized API service layer for frontend
- Create frontend/src/api/mod.rs with centralized HTTP client and error handling
- Implement api::auth module (login, logout, check_auth, get_user)
- Implement api::torrent module (add, action, delete, start, stop, set_label, set_priority)
- Implement api::setup module (get_status, setup)
- Implement api::settings module (set_global_limits)
- Implement api::push module (get_public_key, subscribe)
- Update all components to use api service layer instead of direct gloo_net calls
- Add thiserror dependency for error handling
2026-02-08 23:27:13 +03:00

105 lines
3.8 KiB
Rust

use leptos::*;
use leptos::html::Dialog;
use crate::store::{show_toast_with_signal, TorrentStore};
use crate::api;
use shared::NotificationLevel;
#[component]
pub fn AddTorrentModal(
#[prop(into)]
on_close: Callback<()>,
) -> impl IntoView {
let store = use_context::<TorrentStore>().expect("TorrentStore not provided");
let notifications = store.notifications;
let dialog_ref = create_node_ref::<Dialog>();
let (uri, set_uri) = create_signal(String::new());
let (is_loading, set_loading) = create_signal(false);
let (error_msg, set_error_msg) = create_signal(Option::<String>::None);
create_effect(move |_| {
if let Some(dialog) = dialog_ref.get() {
let _ = dialog.show_modal();
}
});
let handle_submit = move |_| {
let uri_val = uri.get();
if uri_val.is_empty() {
show_toast_with_signal(notifications, NotificationLevel::Warning, "Lütfen bir Magnet URI veya URL girin");
set_error_msg.set(Some("Please enter a Magnet URI or URL".to_string()));
return;
}
set_loading.set(true);
set_error_msg.set(None);
let uri_val = uri_val;
spawn_local(async move {
match api::torrent::add(&uri_val).await {
Ok(_) => {
logging::log!("Torrent added successfully");
show_toast_with_signal(notifications, NotificationLevel::Success, "Torrent eklendi");
set_loading.set(false);
if let Some(dialog) = dialog_ref.get() {
dialog.close();
}
on_close.call(());
}
Err(e) => {
logging::error!("Failed to add torrent: {:?}", e);
show_toast_with_signal(notifications, NotificationLevel::Error, "Torrent eklenemedi");
set_error_msg.set(Some(format!("Error: {:?}", e)));
set_loading.set(false);
}
}
});
};
let handle_close = move |_| {
if let Some(dialog) = dialog_ref.get() {
dialog.close();
}
on_close.call(());
};
view! {
<dialog node_ref=dialog_ref class="modal modal-bottom sm:modal-middle">
<div class="modal-box">
<h3 class="font-bold text-lg">"Add Torrent"</h3>
<p class="py-4">"Enter a Magnet URI or direct URL to a .torrent file."</p>
<div class="form-control w-full">
<input
type="text"
placeholder="magnet:?xt=urn:btih:..."
class="input input-bordered w-full"
prop:value=uri
on:input=move |ev| set_uri.set(event_target_value(&ev))
disabled=is_loading
/>
</div>
<div class="modal-action">
<button class="btn" on:click=handle_close disabled=is_loading>"Cancel"</button>
<button class="btn btn-primary" on:click=handle_submit disabled=is_loading>
{move || if is_loading.get() {
view! { <span class="loading loading-spinner"></span> "Adding..." }.into_view()
} else {
view! { "Add" }.into_view()
}}
</button>
</div>
{move || error_msg.get().map(|msg| view! {
<div class="text-error text-sm mt-2">{msg}</div>
})}
</div>
<form method="dialog" class="modal-backdrop">
<button type="button" on:click=handle_close>"close"</button>
</form>
</dialog>
}
}