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-07-09 17:57:05 -07:00
|
|
|
|
2024-06-14 09:35:35 -07:00
|
|
|
"github.com/go-playground/validator/v10"
|
2024-06-09 09:31:30 -07:00
|
|
|
"github.com/labstack/echo/v4"
|
2024-07-09 17:57:05 -07:00
|
|
|
|
|
|
|
"git.grosinger.net/tgrosinger/saasitone/pkg/form"
|
|
|
|
"git.grosinger.net/tgrosinger/saasitone/pkg/page"
|
|
|
|
"git.grosinger.net/tgrosinger/saasitone/pkg/services"
|
2024-07-14 19:50:58 -07:00
|
|
|
"git.grosinger.net/tgrosinger/saasitone/templ/layouts"
|
|
|
|
"git.grosinger.net/tgrosinger/saasitone/templ/pages"
|
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
|
2024-06-15 12:34:24 -07:00
|
|
|
*services.TemplateRenderer
|
2021-12-22 18:51:18 -08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2024-06-09 09:31:30 -07:00
|
|
|
func init() {
|
|
|
|
Register(new(Contact))
|
|
|
|
}
|
|
|
|
|
2024-06-15 12:34:24 -07:00
|
|
|
func (h *Contact) Init(c *services.Container) error {
|
|
|
|
h.TemplateRenderer = c.TemplateRenderer
|
|
|
|
h.mail = c.Mail
|
2024-06-09 09:31:30 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-15 12:34:24 -07:00
|
|
|
func (h *Contact) Routes(g *echo.Group) {
|
|
|
|
g.GET("/contact", h.Page).Name = routeNameContact
|
|
|
|
g.POST("/contact", h.Submit).Name = routeNameContactSubmit
|
2024-06-09 09:31:30 -07:00
|
|
|
}
|
|
|
|
|
2024-06-15 12:34:24 -07:00
|
|
|
func (h *Contact) Page(ctx echo.Context) error {
|
|
|
|
p := page.New(ctx)
|
|
|
|
p.Title = "Contact us"
|
2024-07-27 12:28:45 -07:00
|
|
|
p.LayoutComponent = layouts.Main
|
2021-12-22 18:51:18 -08:00
|
|
|
|
2024-07-14 19:50:58 -07:00
|
|
|
f := form.Get[pages.ContactForm](ctx)
|
|
|
|
component := pages.Contact(p, f)
|
|
|
|
|
|
|
|
return h.RenderPageTempl(ctx, p, component)
|
2021-12-03 03:11:01 -08:00
|
|
|
}
|
|
|
|
|
2024-06-15 12:34:24 -07:00
|
|
|
func (h *Contact) Submit(ctx echo.Context) error {
|
2024-07-14 19:50:58 -07:00
|
|
|
var input pages.ContactForm
|
2021-12-23 20:04:00 -08:00
|
|
|
|
2024-06-14 09:35:35 -07:00
|
|
|
err := form.Submit(ctx, &input)
|
|
|
|
|
|
|
|
switch err.(type) {
|
|
|
|
case nil:
|
|
|
|
case validator.ValidationErrors:
|
2024-06-15 12:34:24 -07:00
|
|
|
return h.Page(ctx)
|
2024-06-14 09:35:35 -07:00
|
|
|
default:
|
2024-06-09 18:39:04 -07:00
|
|
|
return err
|
2021-12-22 18:51:18 -08:00
|
|
|
}
|
2021-12-22 20:40:08 -08:00
|
|
|
|
2024-06-15 12:34:24 -07:00
|
|
|
err = h.mail.
|
2024-06-14 09:35:35 -07:00
|
|
|
Compose().
|
|
|
|
To(input.Email).
|
|
|
|
Subject("Contact form submitted").
|
|
|
|
Body(fmt.Sprintf("The message is: %s", input.Message)).
|
|
|
|
Send(ctx)
|
|
|
|
if err != nil {
|
2024-06-15 12:34:24 -07:00
|
|
|
return fail(err, "unable to send email")
|
2021-12-22 18:51:18 -08:00
|
|
|
}
|
|
|
|
|
2024-06-15 12:34:24 -07:00
|
|
|
return h.Page(ctx)
|
2021-12-03 03:11:01 -08:00
|
|
|
}
|