Started on HTMX support.

This commit is contained in:
mikestefanello 2021-12-22 21:51:18 -05:00
parent 3b41e1dfd8
commit b61077dac9
10 changed files with 123 additions and 27 deletions

View file

@ -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)

View file

@ -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")
}
}

View file

@ -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 {

View file

@ -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 {

View file

@ -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 {

View file

@ -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)
}