Started on HTMX support.
This commit is contained in:
parent
3b41e1dfd8
commit
b61077dac9
10 changed files with 123 additions and 27 deletions
48
controller/htmx.go
Normal file
48
controller/htmx.go
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
package controller
|
||||
|
||||
import (
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
// HTMX headers (https://htmx.org/docs/#requests)
|
||||
const (
|
||||
HTMXHeaderRequest = "HX-Request"
|
||||
HTMXHeaderTrigger = "HX-Trigger"
|
||||
HTMXHeaderTriggerName = "HX-Trigger-Name"
|
||||
HTMXHeaderTriggerAfterSwap = "HX-Trigger-After-Swap"
|
||||
HTMXHeaderTriggerAfterSettle = "HX-Trigger-After-Settle"
|
||||
HTMXHeaderTarget = "HX-Target"
|
||||
HTMXHeaderPrompt = "HX-Prompt"
|
||||
HTMXHeaderPush = "HX-Push"
|
||||
HTMXHeaderRedirect = "HX-Redirect"
|
||||
HTMXHeaderRefresh = "HX-Refresh"
|
||||
)
|
||||
|
||||
type (
|
||||
HTMXRequest struct {
|
||||
Enabled bool
|
||||
Trigger string
|
||||
TriggerName string
|
||||
Target string
|
||||
Prompt string
|
||||
}
|
||||
|
||||
HTMXResponse struct {
|
||||
Push string
|
||||
Redirect string
|
||||
Refresh bool
|
||||
Trigger string
|
||||
TriggerAfterSwap string
|
||||
TriggerAfterSettle string
|
||||
}
|
||||
)
|
||||
|
||||
func NewHTMXRequest(ctx echo.Context) HTMXRequest {
|
||||
return HTMXRequest{
|
||||
Enabled: ctx.Request().Header.Get(HTMXHeaderRequest) == "true",
|
||||
Trigger: ctx.Request().Header.Get(HTMXHeaderTrigger),
|
||||
TriggerName: ctx.Request().Header.Get(HTMXHeaderTriggerName),
|
||||
Target: ctx.Request().Header.Get(HTMXHeaderTarget),
|
||||
Prompt: ctx.Request().Header.Get(HTMXHeaderPrompt),
|
||||
}
|
||||
}
|
||||
|
|
@ -16,7 +16,7 @@ func (a *About) Get(c echo.Context) error {
|
|||
p.Name = "about"
|
||||
p.Title = "About"
|
||||
p.Data = "This is the about page"
|
||||
p.Cache.Enabled = true
|
||||
p.Cache.Enabled = false
|
||||
p.Cache.Tags = []string{"page_about", "page:list"}
|
||||
|
||||
return a.RenderPage(c, p)
|
||||
|
|
|
|||
|
|
@ -1,27 +1,67 @@
|
|||
package routes
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"goweb/context"
|
||||
"goweb/controller"
|
||||
"goweb/msg"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
type Contact struct {
|
||||
controller.Controller
|
||||
}
|
||||
type (
|
||||
Contact struct {
|
||||
controller.Controller
|
||||
}
|
||||
|
||||
func (a *Contact) Get(c echo.Context) error {
|
||||
p := controller.NewPage(c)
|
||||
ContactForm struct {
|
||||
Email string `form:"email" validate:"required,email" label:"Email address"`
|
||||
Message string `form:"message" validate:"required" label:"Message"`
|
||||
}
|
||||
)
|
||||
|
||||
func (c *Contact) Get(ctx echo.Context) error {
|
||||
p := controller.NewPage(ctx)
|
||||
p.Layout = "main"
|
||||
p.Name = "contact"
|
||||
p.Title = "Contact us"
|
||||
p.Data = "This is the contact page"
|
||||
return a.RenderPage(c, p)
|
||||
p.Data = ContactForm{}
|
||||
|
||||
if form := ctx.Get(context.FormKey); form != nil {
|
||||
p.Data = form.(ContactForm)
|
||||
}
|
||||
|
||||
return c.RenderPage(ctx, p)
|
||||
}
|
||||
|
||||
func (a *Contact) Post(c echo.Context) error {
|
||||
msg.Success(c, "Thank you for contacting us!")
|
||||
msg.Info(c, "We will respond to you shortly.")
|
||||
return a.Redirect(c, "home")
|
||||
func (c *Contact) Post(ctx echo.Context) error {
|
||||
fail := func(message string, err error) error {
|
||||
ctx.Logger().Errorf("%s: %v", message, err)
|
||||
msg.Danger(ctx, "An error occurred. Please try again.")
|
||||
return c.Get(ctx)
|
||||
}
|
||||
|
||||
// Parse the form values
|
||||
var form ContactForm
|
||||
if err := ctx.Bind(&form); err != nil {
|
||||
return fail("unable to parse contact form", err)
|
||||
}
|
||||
ctx.Set(context.FormKey, form)
|
||||
|
||||
// Validate the form
|
||||
if err := ctx.Validate(form); err != nil {
|
||||
c.SetValidationErrorMessages(ctx, err, form)
|
||||
return c.Get(ctx)
|
||||
}
|
||||
|
||||
p := controller.NewHTMX(ctx)
|
||||
|
||||
if p.Request.Enabled {
|
||||
return ctx.String(http.StatusOK, "<b>HELLO!</b>")
|
||||
} else {
|
||||
msg.Success(ctx, "Thank you for contacting us!")
|
||||
msg.Info(ctx, "We will respond to you shortly.")
|
||||
return c.Redirect(ctx, "home")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -50,11 +50,11 @@ func (f *ForgotPassword) Post(c echo.Context) error {
|
|||
}
|
||||
|
||||
// Parse the form values
|
||||
form := new(ForgotPasswordForm)
|
||||
if err := c.Bind(form); err != nil {
|
||||
var form ForgotPasswordForm
|
||||
if err := c.Bind(&form); err != nil {
|
||||
return fail("unable to parse forgot password form", err)
|
||||
}
|
||||
c.Set(context.FormKey, *form)
|
||||
c.Set(context.FormKey, form)
|
||||
|
||||
// Validate the form
|
||||
if err := c.Validate(form); err != nil {
|
||||
|
|
|
|||
|
|
@ -45,11 +45,11 @@ func (l *Login) Post(c echo.Context) error {
|
|||
}
|
||||
|
||||
// Parse the form values
|
||||
form := new(LoginForm)
|
||||
if err := c.Bind(form); err != nil {
|
||||
var form LoginForm
|
||||
if err := c.Bind(&form); err != nil {
|
||||
return fail("unable to parse login form", err)
|
||||
}
|
||||
c.Set(context.FormKey, *form)
|
||||
c.Set(context.FormKey, form)
|
||||
|
||||
// Validate the form
|
||||
if err := c.Validate(form); err != nil {
|
||||
|
|
|
|||
|
|
@ -44,11 +44,11 @@ func (r *Register) Post(c echo.Context) error {
|
|||
}
|
||||
|
||||
// Parse the form values
|
||||
form := new(RegisterForm)
|
||||
if err := c.Bind(form); err != nil {
|
||||
var form RegisterForm
|
||||
if err := c.Bind(&form); err != nil {
|
||||
return fail("unable to parse form values", err)
|
||||
}
|
||||
c.Set(context.FormKey, *form)
|
||||
c.Set(context.FormKey, form)
|
||||
|
||||
// Validate the form
|
||||
if err := c.Validate(form); err != nil {
|
||||
|
|
|
|||
|
|
@ -37,8 +37,8 @@ func (r *ResetPassword) Post(c echo.Context) error {
|
|||
}
|
||||
|
||||
// Parse the form values
|
||||
form := new(ResetPasswordForm)
|
||||
if err := c.Bind(form); err != nil {
|
||||
var form ResetPasswordForm
|
||||
if err := c.Bind(&form); err != nil {
|
||||
return fail("unable to parse forgot password form", err)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -17,5 +17,6 @@
|
|||
{{end}}
|
||||
|
||||
{{define "js"}}
|
||||
<script src="https://unpkg.com/htmx.org@1.6.1"></script>
|
||||
<script defer src="https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js"></script>
|
||||
{{end}}
|
||||
|
|
@ -7,7 +7,7 @@
|
|||
</head>
|
||||
<body>
|
||||
<nav class="navbar is-dark">
|
||||
<div class="container">
|
||||
<div class="container" hx-boost="true">
|
||||
<div class="navbar-brand">
|
||||
<a href="{{call .ToURL "home"}}" class="navbar-item">{{.AppName}}</a>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -1,9 +1,16 @@
|
|||
{{define "content"}}
|
||||
<form method="post">
|
||||
<form id="contact" method="post" hx-post>
|
||||
<div class="field">
|
||||
<label class="label">Message</label>
|
||||
<label for="email" class="label">Email address</label>
|
||||
<div class="control">
|
||||
<textarea class="textarea" placeholder="Textarea"></textarea>
|
||||
<input id="email" name="email" type="email" class="input" value="{{.Data.Email}}">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label for="message" class="label">Message</label>
|
||||
<div class="control">
|
||||
<textarea id="message" name="message" class="textarea">{{.Data.Message}}</textarea>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue