Skip to content

Commit

Permalink
htmx.Post handler (experimental)
Browse files Browse the repository at this point in the history
  • Loading branch information
yznts committed Apr 24, 2024
1 parent 98ff829 commit 069f55a
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions htmx/post.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package htmx

import "go.kyoto.codes/v3/component"

// Post is a helper function that simplifies the handling of stateful htmx POST requests.
func Post(ctx *component.Context, state component.State, handler func()) {
// We are only interested in POST requests here
if ctx.Request.Method == "POST" {
// Parse the form to get the state
ctx.Request.ParseForm()
// If no state is present in the form, we ignore.
// Porbably this is a regular POST request, not related to htmx.
if ctx.Request.FormValue("hx-state") == "" {
return
}
// If the state is disposable, we panic.
// This is a safety measure to prevent misuse of disposable components.
if ctx.Request.FormValue("hx-state") == "disposable" {
panic("incorrect use of disposable component")
}
// Unmarshal the state from the form
state.Unmarshal(state, ctx.Request.FormValue("hx-state"))
// Call the handler
handler()
}
}

0 comments on commit 069f55a

Please sign in to comment.