2024-06-09 09:31:30 -07:00
|
|
|
package handlers
|
2021-12-03 03:11:01 -08:00
|
|
|
|
|
|
|
import (
|
2022-01-14 12:42:32 -08:00
|
|
|
"fmt"
|
|
|
|
|
2024-06-09 09:31:30 -07:00
|
|
|
"github.com/labstack/echo/v4"
|
2022-11-02 16:23:26 -07:00
|
|
|
"github.com/mikestefanello/pagoda/pkg/context"
|
|
|
|
"github.com/mikestefanello/pagoda/pkg/controller"
|
2024-06-09 09:31:30 -07:00
|
|
|
"github.com/mikestefanello/pagoda/pkg/services"
|
2023-12-16 08:07:20 -08:00
|
|
|
"github.com/mikestefanello/pagoda/templates"
|
2024-06-09 09:31:30 -07:00
|
|
|
)
|
2021-12-03 03:11:01 -08:00
|
|
|
|
2024-06-09 09:31:30 -07:00
|
|
|
const (
|
|
|
|
routeNameContact = "contact"
|
|
|
|
routeNameContactSubmit = "contact.submit"
|
2021-12-03 03:11:01 -08:00
|
|
|
)
|
|
|
|
|
2021-12-22 18:51:18 -08:00
|
|
|
type (
|
2024-06-09 09:31:30 -07:00
|
|
|
Contact struct {
|
|
|
|
mail *services.MailClient
|
2021-12-22 18:51:18 -08:00
|
|
|
controller.Controller
|
|
|
|
}
|
2021-12-03 03:11:01 -08:00
|
|
|
|
2022-02-10 05:56:07 -08:00
|
|
|
contactForm struct {
|
2021-12-22 20:40:08 -08:00
|
|
|
Email string `form:"email" validate:"required,email"`
|
2024-05-18 13:19:27 -07:00
|
|
|
Department string `form:"department" validate:"required,oneof=sales marketing hr"`
|
2021-12-22 20:40:08 -08:00
|
|
|
Message string `form:"message" validate:"required"`
|
|
|
|
Submission controller.FormSubmission
|
2021-12-22 18:51:18 -08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2024-06-09 09:31:30 -07:00
|
|
|
func init() {
|
|
|
|
Register(new(Contact))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Contact) Init(ct *services.Container) error {
|
|
|
|
c.Controller = controller.NewController(ct)
|
|
|
|
c.mail = ct.Mail
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Contact) Routes(g *echo.Group) {
|
|
|
|
g.GET("/contact", c.Page).Name = routeNameContact
|
|
|
|
g.POST("/contact", c.Submit).Name = routeNameContactSubmit
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Contact) Page(ctx echo.Context) error {
|
2021-12-28 19:05:20 -08:00
|
|
|
page := controller.NewPage(ctx)
|
2023-12-16 08:07:20 -08:00
|
|
|
page.Layout = templates.LayoutMain
|
|
|
|
page.Name = templates.PageContact
|
2021-12-28 19:05:20 -08:00
|
|
|
page.Title = "Contact us"
|
2022-02-10 05:56:07 -08:00
|
|
|
page.Form = contactForm{}
|
2021-12-22 18:51:18 -08:00
|
|
|
|
|
|
|
if form := ctx.Get(context.FormKey); form != nil {
|
2022-02-10 05:56:07 -08:00
|
|
|
page.Form = form.(*contactForm)
|
2021-12-22 18:51:18 -08:00
|
|
|
}
|
|
|
|
|
2021-12-28 19:05:20 -08:00
|
|
|
return c.RenderPage(ctx, page)
|
2021-12-03 03:11:01 -08:00
|
|
|
}
|
|
|
|
|
2024-06-09 09:31:30 -07:00
|
|
|
func (c *Contact) Submit(ctx echo.Context) error {
|
2022-02-10 05:56:07 -08:00
|
|
|
var form contactForm
|
2021-12-23 20:04:00 -08:00
|
|
|
ctx.Set(context.FormKey, &form)
|
|
|
|
|
|
|
|
// Parse the form values
|
2021-12-22 18:51:18 -08:00
|
|
|
if err := ctx.Bind(&form); err != nil {
|
2022-05-17 05:45:18 -07:00
|
|
|
return c.Fail(err, "unable to bind form")
|
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 {
|
2022-05-17 05:45:18 -07:00
|
|
|
return c.Fail(err, "unable to process form submission")
|
2021-12-22 20:40:08 -08:00
|
|
|
}
|
|
|
|
|
2021-12-23 14:09:44 -08:00
|
|
|
if !form.Submission.HasErrors() {
|
2024-06-09 09:31:30 -07:00
|
|
|
err := c.mail.
|
2022-01-14 12:42:32 -08:00
|
|
|
Compose().
|
|
|
|
To(form.Email).
|
|
|
|
Subject("Contact form submitted").
|
|
|
|
Body(fmt.Sprintf("The message is: %s", form.Message)).
|
|
|
|
Send(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
2022-05-17 05:45:18 -07:00
|
|
|
return c.Fail(err, "unable to send email")
|
2021-12-23 14:09:44 -08:00
|
|
|
}
|
2021-12-22 18:51:18 -08:00
|
|
|
}
|
|
|
|
|
2024-06-09 09:31:30 -07:00
|
|
|
return c.Page(ctx)
|
2021-12-03 03:11:01 -08:00
|
|
|
}
|