lyrics

lyrics
git clone git://brookjeynes.dev/bjeynes/lyrics.git
Log | Files | Refs

htmx.go (629B)


      1 package htmx
      2 
      3 import (
      4 	"lyrics/internal/server/toast"
      5 	"net/http"
      6 )
      7 
      8 func HxError(w http.ResponseWriter, status int, msg string) {
      9 	t := toast.Toast{
     10 		Type: toast.ToastTypeError,
     11 		Body: msg,
     12 	}
     13 	json, err := t.Marshal()
     14 	if err != nil {
     15 		json = []byte(`{"HXToast":{"type":"error","body":"An error occurred"}}`)
     16 	}
     17 
     18 	w.Header().Set("HX-Trigger", string(json))
     19 	w.Header().Set("HX-Reswap", "none")
     20 	w.WriteHeader(status)
     21 	_, _ = w.Write([]byte(msg))
     22 }
     23 
     24 func HxRedirect(w http.ResponseWriter, status int, location string) {
     25 	w.Header().Set("HX-Redirect", location)
     26 	w.Header().Set("Location", location)
     27 	w.WriteHeader(status)
     28 }