2021-12-14 08:13:53 -08:00
|
|
|
package routes
|
2021-12-03 03:11:01 -08:00
|
|
|
|
|
|
|
import (
|
2021-12-22 18:51:18 -08:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"goweb/context"
|
2021-12-12 19:09:13 -08:00
|
|
|
"goweb/controller"
|
2021-12-03 03:11:01 -08:00
|
|
|
"goweb/msg"
|
|
|
|
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
2021-12-22 18:51:18 -08:00
|
|
|
type (
|
|
|
|
Contact struct {
|
|
|
|
controller.Controller
|
|
|
|
}
|
2021-12-03 03:11:01 -08:00
|
|
|
|
2021-12-22 18:51:18 -08:00
|
|
|
ContactForm struct {
|
2021-12-22 20:40:08 -08:00
|
|
|
Email string `form:"email" validate:"required,email"`
|
|
|
|
Message string `form:"message" validate:"required"`
|
|
|
|
Submission controller.FormSubmission
|
2021-12-22 18:51:18 -08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func (c *Contact) Get(ctx echo.Context) error {
|
|
|
|
p := controller.NewPage(ctx)
|
2021-12-03 03:11:01 -08:00
|
|
|
p.Layout = "main"
|
|
|
|
p.Name = "contact"
|
2021-12-03 05:49:07 -08:00
|
|
|
p.Title = "Contact us"
|
2021-12-23 05:57:27 -08:00
|
|
|
p.Form = ContactForm{}
|
2021-12-22 18:51:18 -08:00
|
|
|
|
|
|
|
if form := ctx.Get(context.FormKey); form != nil {
|
2021-12-23 05:57:27 -08:00
|
|
|
p.Form = form.(ContactForm)
|
2021-12-22 18:51:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.RenderPage(ctx, p)
|
2021-12-03 03:11:01 -08:00
|
|
|
}
|
|
|
|
|
2021-12-22 18:51:18 -08:00
|
|
|
func (c *Contact) Post(ctx echo.Context) error {
|
2021-12-22 20:40:08 -08:00
|
|
|
//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)
|
|
|
|
//}
|
2021-12-22 18:51:18 -08:00
|
|
|
|
|
|
|
// Parse the form values
|
|
|
|
var form ContactForm
|
|
|
|
if err := ctx.Bind(&form); err != nil {
|
2021-12-22 20:40:08 -08:00
|
|
|
ctx.Logger().Error(err)
|
2021-12-22 18:51:18 -08:00
|
|
|
}
|
2021-12-22 20:40:08 -08:00
|
|
|
|
|
|
|
if err := form.Submission.Process(ctx, form); err != nil {
|
|
|
|
// TOOD
|
|
|
|
}
|
|
|
|
|
2021-12-22 18:51:18 -08:00
|
|
|
ctx.Set(context.FormKey, form)
|
|
|
|
|
2021-12-22 20:40:08 -08:00
|
|
|
if form.Submission.HasErrors() {
|
2021-12-22 18:51:18 -08:00
|
|
|
return c.Get(ctx)
|
|
|
|
}
|
|
|
|
|
2021-12-23 05:57:27 -08:00
|
|
|
htmx := controller.GetHTMXRequest(ctx)
|
2021-12-22 18:51:18 -08:00
|
|
|
|
2021-12-22 20:40:08 -08:00
|
|
|
if htmx.Enabled {
|
2021-12-22 18:51:18 -08:00
|
|
|
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")
|
|
|
|
}
|
2021-12-03 03:11:01 -08:00
|
|
|
}
|