fix: host modal — single POST endpoint for create+edit, remove hx-put conflict
- hx-put="" (empty) was causing HTMX to send wrong requests - Now use single hx-post="/hosts" with hidden hostId field - Server checks hostId: empty → create, present → update - UpdateHost reads ID from both URL param and form data
This commit is contained in:
@@ -51,6 +51,12 @@ func parsePort(s string, def int) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func CreateHost(c *fiber.Ctx) error {
|
func CreateHost(c *fiber.Ctx) error {
|
||||||
|
hostID := c.FormValue("hostId")
|
||||||
|
|
||||||
|
if hostID != "" {
|
||||||
|
return UpdateHost(c)
|
||||||
|
}
|
||||||
|
|
||||||
name := c.FormValue("name")
|
name := c.FormValue("name")
|
||||||
ip := c.FormValue("ip")
|
ip := c.FormValue("ip")
|
||||||
if name == "" || ip == "" {
|
if name == "" || ip == "" {
|
||||||
@@ -77,9 +83,8 @@ func CreateHost(c *fiber.Ctx) error {
|
|||||||
h.AuthType = "key"
|
h.AuthType = "key"
|
||||||
}
|
}
|
||||||
|
|
||||||
created := hostStore.AddFull(h)
|
hostStore.AddFull(h)
|
||||||
hosts := hostStore.All()
|
hosts := hostStore.All()
|
||||||
_ = created
|
|
||||||
return c.Render("host_list", fiber.Map{
|
return c.Render("host_list", fiber.Map{
|
||||||
"Hosts": hosts,
|
"Hosts": hosts,
|
||||||
"Active": countByStatus(hosts, "active"),
|
"Active": countByStatus(hosts, "active"),
|
||||||
@@ -89,6 +94,9 @@ func CreateHost(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
func UpdateHost(c *fiber.Ctx) error {
|
func UpdateHost(c *fiber.Ctx) error {
|
||||||
id := c.Params("id")
|
id := c.Params("id")
|
||||||
|
if id == "" {
|
||||||
|
id = c.FormValue("hostId")
|
||||||
|
}
|
||||||
existing, found := hostStore.Get(id)
|
existing, found := hostStore.Get(id)
|
||||||
if !found {
|
if !found {
|
||||||
return c.Status(404).SendString("host not found")
|
return c.Status(404).SendString("host not found")
|
||||||
|
|||||||
@@ -15,11 +15,11 @@
|
|||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<form :hx-post="editing ? null : '/hosts'"
|
<form hx-post="/hosts"
|
||||||
:hx-put="editing ? '/hosts/' + hostId : null"
|
|
||||||
hx-target="#host-list" hx-swap="innerHTML"
|
hx-target="#host-list" hx-swap="innerHTML"
|
||||||
@submit="open = false; $dispatch('toast-message', { message: editing ? 'Host updated' : 'Host added' })"
|
@submit="open = false; $dispatch('toast-message', { message: editing ? 'Host updated' : 'Host added' })"
|
||||||
class="p-6 space-y-4 text-sm">
|
class="p-6 space-y-4 text-sm">
|
||||||
|
<input type="hidden" name="hostId" :value="editing ? hostId : ''">
|
||||||
|
|
||||||
<div class="grid grid-cols-2 gap-4">
|
<div class="grid grid-cols-2 gap-4">
|
||||||
<div class="flex flex-col gap-1.5">
|
<div class="flex flex-col gap-1.5">
|
||||||
|
|||||||
Reference in New Issue
Block a user