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:
swanadiva
2026-07-07 15:56:54 +07:00
parent 301115b74d
commit a2412b7cd2
2 changed files with 12 additions and 4 deletions
+10 -2
View File
@@ -51,6 +51,12 @@ func parsePort(s string, def int) int {
}
func CreateHost(c *fiber.Ctx) error {
hostID := c.FormValue("hostId")
if hostID != "" {
return UpdateHost(c)
}
name := c.FormValue("name")
ip := c.FormValue("ip")
if name == "" || ip == "" {
@@ -77,9 +83,8 @@ func CreateHost(c *fiber.Ctx) error {
h.AuthType = "key"
}
created := hostStore.AddFull(h)
hostStore.AddFull(h)
hosts := hostStore.All()
_ = created
return c.Render("host_list", fiber.Map{
"Hosts": hosts,
"Active": countByStatus(hosts, "active"),
@@ -89,6 +94,9 @@ func CreateHost(c *fiber.Ctx) error {
func UpdateHost(c *fiber.Ctx) error {
id := c.Params("id")
if id == "" {
id = c.FormValue("hostId")
}
existing, found := hostStore.Get(id)
if !found {
return c.Status(404).SendString("host not found")
+2 -2
View File
@@ -15,11 +15,11 @@
</button>
</div>
<form :hx-post="editing ? null : '/hosts'"
:hx-put="editing ? '/hosts/' + hostId : null"
<form hx-post="/hosts"
hx-target="#host-list" hx-swap="innerHTML"
@submit="open = false; $dispatch('toast-message', { message: editing ? 'Host updated' : 'Host added' })"
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="flex flex-col gap-1.5">