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/controller"
|
2024-06-09 18:39:04 -07:00
|
|
|
"github.com/mikestefanello/pagoda/pkg/form"
|
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"`
|
2024-06-09 18:39:04 -07:00
|
|
|
Submission form.Submission
|
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"
|
2024-06-09 18:39:04 -07:00
|
|
|
page.Form = form.Get[contactForm](ctx)
|
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 {
|
2024-06-09 18:39:04 -07:00
|
|
|
var input contactForm
|
2021-12-23 20:04:00 -08:00
|
|
|
|
2024-06-09 18:39:04 -07:00
|
|
|
// Store in context and parse the form values
|
|
|
|
if err := form.Set(ctx, &input); err != nil {
|
|
|
|
return err
|
2021-12-22 18:51:18 -08:00
|
|
|
}
|
2021-12-22 20:40:08 -08:00
|
|
|
|
2024-06-09 18:39:04 -07:00
|
|
|
if err := input.Submission.Process(ctx, input); 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
|
|
|
}
|
|
|
|
|
2024-06-09 18:39:04 -07:00
|
|
|
if !input.Submission.HasErrors() {
|
2024-06-09 09:31:30 -07:00
|
|
|
err := c.mail.
|
2022-01-14 12:42:32 -08:00
|
|
|
Compose().
|
2024-06-09 18:39:04 -07:00
|
|
|
To(input.Email).
|
2022-01-14 12:42:32 -08:00
|
|
|
Subject("Contact form submitted").
|
2024-06-09 18:39:04 -07:00
|
|
|
Body(fmt.Sprintf("The message is: %s", input.Message)).
|
2022-01-14 12:42:32 -08:00
|
|
|
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
|
|
|
}
|