feat(hosts): replace inline add form with modal dialog
The add-host form is now opened via a "+ Add host" button in the page header. The modal closes on Cancel, backdrop click, × button, or automatically after a successful creation. Adds modal CSS with backdrop blur and entry animation, .btn-primary / .btn-secondary shared button styles, and a .page-header flex layout reusable across list pages. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,15 +1,14 @@
|
||||
// client/hosts.rs — Hosts list page
|
||||
//
|
||||
// Displays all hosts across every network with:
|
||||
// - Add form : create a host inside a chosen network
|
||||
// - Filter bar : name (substring), network, open port, application
|
||||
// - Table : name, IP, network, port count, application count, delete
|
||||
// - Pagination : configurable page size (15 / 25 / 50 / 100 / All)
|
||||
// - Add button : opens a modal form to create a host inside a chosen network
|
||||
// - Filter bar : name (substring), network, open port, application
|
||||
// - Table : name, IP, network, port count, application count, delete
|
||||
// - Pagination : configurable page size (15 / 25 / 50 / 100 / All)
|
||||
//
|
||||
// Each sub-component calls `.into_any()` on its view to return `AnyView`
|
||||
// (a type-erased wrapper). This prevents Rust from composing all the nested
|
||||
// generic types into a single enormous type in the parent's monomorphization,
|
||||
// which would otherwise overflow the compiler's query depth limit.
|
||||
// Sub-components call `.into_any()` on their views to erase the concrete
|
||||
// Leptos type, preventing the parent from accumulating a deeply-nested
|
||||
// generic type that overflows the compiler's query depth limit.
|
||||
|
||||
use leptos::prelude::*;
|
||||
use leptos::form::ActionForm;
|
||||
@@ -28,46 +27,76 @@ const PER_PAGE_OPTIONS: &[(i64, &str)] = &[
|
||||
(0, "All"),
|
||||
];
|
||||
|
||||
// ─── Add host form ────────────────────────────────────────────────────────────
|
||||
// ─── Add host modal ───────────────────────────────────────────────────────────
|
||||
|
||||
#[component]
|
||||
fn AddHostForm(
|
||||
fn AddHostModal(
|
||||
create_action: ServerAction<CreateHost>,
|
||||
networks_res: Resource<Result<Vec<crate::models::Network>, ServerFnError>>,
|
||||
show_modal: RwSignal<bool>,
|
||||
) -> impl IntoView {
|
||||
// Close the modal automatically after a successful creation.
|
||||
Effect::new(move |_| {
|
||||
if let Some(Ok(_)) = create_action.value().get() {
|
||||
show_modal.set(false);
|
||||
}
|
||||
});
|
||||
|
||||
view! {
|
||||
<section class="add-form">
|
||||
<h2>"Add a host"</h2>
|
||||
<ActionForm action=create_action>
|
||||
<div class="add-form__fields">
|
||||
<label>
|
||||
"Name"
|
||||
<input type="text" name="name" placeholder="e.g. web-server-01" required/>
|
||||
</label>
|
||||
<label>
|
||||
"IP address"
|
||||
<input type="text" name="ip" placeholder="e.g. 192.168.1.10" required/>
|
||||
</label>
|
||||
<label>
|
||||
"Network"
|
||||
<select name="network_id" required>
|
||||
<option value="">"— choose —"</option>
|
||||
{move || networks_res.get()
|
||||
.and_then(|r| r.ok())
|
||||
.map(|nets| nets.into_iter().map(|n| {
|
||||
view! { <option value=n.id.to_string()>{n.cidr}</option> }
|
||||
}).collect_view())
|
||||
}
|
||||
</select>
|
||||
</label>
|
||||
<button type="submit">"Add"</button>
|
||||
// Backdrop — click outside the card to close
|
||||
<div class="modal-backdrop" on:click=move |_| show_modal.set(false)>
|
||||
// stop_propagation keeps clicks inside the card from closing the modal
|
||||
<div class="modal" on:click=move |e| e.stop_propagation()>
|
||||
<div class="modal__header">
|
||||
<h2>"Add a host"</h2>
|
||||
<button
|
||||
class="modal__close"
|
||||
type="button"
|
||||
aria-label="Close"
|
||||
on:click=move |_| show_modal.set(false)
|
||||
>"×"</button>
|
||||
</div>
|
||||
</ActionForm>
|
||||
{move || create_action.value().get()
|
||||
.and_then(|r| r.err())
|
||||
.map(|e| view! { <p class="error">{e.to_string()}</p> })
|
||||
}
|
||||
</section>
|
||||
|
||||
<ActionForm action=create_action>
|
||||
<div class="add-form__fields">
|
||||
<label>
|
||||
"Name"
|
||||
<input type="text" name="name" placeholder="e.g. web-server-01" required/>
|
||||
</label>
|
||||
<label>
|
||||
"IP address"
|
||||
<input type="text" name="ip" placeholder="e.g. 192.168.1.10" required/>
|
||||
</label>
|
||||
<label>
|
||||
"Network"
|
||||
<select name="network_id" required>
|
||||
<option value="">"— choose —"</option>
|
||||
{move || networks_res.get()
|
||||
.and_then(|r| r.ok())
|
||||
.map(|nets| nets.into_iter().map(|n| {
|
||||
view! { <option value=n.id.to_string()>{n.cidr}</option> }
|
||||
}).collect_view())
|
||||
}
|
||||
</select>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<div class="modal__actions">
|
||||
<button
|
||||
class="btn-secondary"
|
||||
type="button"
|
||||
on:click=move |_| show_modal.set(false)
|
||||
>"Cancel"</button>
|
||||
<button type="submit">"Add host"</button>
|
||||
</div>
|
||||
</ActionForm>
|
||||
|
||||
{move || create_action.value().get()
|
||||
.and_then(|r| r.err())
|
||||
.map(|e| view! { <p class="error">{e.to_string()}</p> })
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
}.into_any()
|
||||
}
|
||||
|
||||
@@ -194,7 +223,7 @@ fn PaginationBar(
|
||||
{move || format!("Page {} of {}", page.get(), total_pages.get().max(1))}
|
||||
</span>
|
||||
<button
|
||||
disabled=move || page.get() >= total_pages.get()
|
||||
disabled={move || page.get() >= total_pages.get()}
|
||||
on:click=move |_| {
|
||||
let max = total_pages.get_untracked();
|
||||
page.update(|p| *p = (*p + 1).min(max));
|
||||
@@ -209,7 +238,6 @@ fn PaginationBar(
|
||||
|
||||
// ─── Host table ───────────────────────────────────────────────────────────────
|
||||
|
||||
// Separate component for the table body to further reduce type depth in HostsPage.
|
||||
#[component]
|
||||
fn HostTable(
|
||||
hosts: Resource<Result<HostsPageData, ServerFnError>>,
|
||||
@@ -284,6 +312,9 @@ pub fn HostsPage() -> impl IntoView {
|
||||
let create_action = ServerAction::<CreateHost>::new();
|
||||
let delete_action = ServerAction::<DeleteHost>::new();
|
||||
|
||||
// Controls the add-host modal
|
||||
let show_modal = RwSignal::new(false);
|
||||
|
||||
// Filter signals (0 / "" = no filter)
|
||||
let name_filter = RwSignal::new(String::new());
|
||||
let network_id_filter = RwSignal::new(0i64);
|
||||
@@ -323,9 +354,21 @@ pub fn HostsPage() -> impl IntoView {
|
||||
|
||||
view! {
|
||||
<div class="hosts-page">
|
||||
<h1>"Hosts"</h1>
|
||||
<div class="page-header">
|
||||
<h1>"Hosts"</h1>
|
||||
<button class="btn-primary" on:click=move |_| show_modal.set(true)>
|
||||
"+ Add host"
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<AddHostForm create_action=create_action networks_res=networks_res/>
|
||||
// Modal — only rendered when show_modal is true
|
||||
{move || show_modal.get().then(|| view! {
|
||||
<AddHostModal
|
||||
create_action=create_action
|
||||
networks_res=networks_res
|
||||
show_modal=show_modal
|
||||
/>
|
||||
})}
|
||||
|
||||
<FilterBar
|
||||
networks_res=networks_res
|
||||
|
||||
Reference in New Issue
Block a user