diff --git a/internal/server/handle_public.go b/internal/server/handle_public.go index 743d94f..820babd 100644 --- a/internal/server/handle_public.go +++ b/internal/server/handle_public.go @@ -1,7 +1,6 @@ package server import ( - "errors" "fmt" "net/http" "time" @@ -20,27 +19,36 @@ func handleHomePage() echo.HandlerFunc { func handleRequestBooking() echo.HandlerFunc { return func(c echo.Context) error { - // validate the form request - // no time travelling + itemStr := c.FormValue("item") fromStr := c.FormValue("from") toStr := c.FormValue("to") + name := c.FormValue("item") + phoneNumber := c.FormValue("phone") + email := c.FormValue("email") + from, fErr := myTime.ParseFromForm(fromStr) to, tErr := myTime.ParseFromForm(toStr) if fErr != nil || tErr != nil { return fmt.Errorf("error parsing booking request time: %q %q", fErr, tErr) } - errs := make([]error, 0) + errs := make([]string, 0) if to.Sub(from) < time.Duration(0) { - errs = append(errs, errors.New("invalid_time_range")) + errs = append(errs, "invalid_time_range") } - phoneNumber := c.FormValue("phone") - email := c.FormValue("email") if phoneNumber == "" && email == "" { - errs = append(errs, errors.New("missing_communication_method")) + errs = append(errs, "missing_communication_method") } - return renderTempl(c, http.StatusOK, view.RequestBookingForm(errs)) + return renderTempl(c, http.StatusOK, view.RequestBookingForm(&view.RequestBookingViewModel{ + Item: itemStr, + From: fromStr, + To: toStr, + Name: name, + PhoneNumber: phoneNumber, + Email: email, + Errors: errs, + })) } } diff --git a/internal/view/index.templ b/internal/view/index.templ index 344cb18..107bad6 100644 --- a/internal/view/index.templ +++ b/internal/view/index.templ @@ -15,25 +15,35 @@ func makeItems() []string { var items = makeItems() -func invalidTimeRange(errs []error) bool { - for _, e := range errs { - if e.Error() == "invalid_time_range" { +type RequestBookingViewModel struct { + Item string + From string + To string + Name string + PhoneNumber string + Email string + Errors []string +} + +func (rbvm RequestBookingViewModel) invalidTimeRange() bool { + for _, e := range rbvm.Errors { + if e == "invalid_time_range" { return true } } return false } -func missingCommunicationMethod(errs []error) bool { - for _, e := range errs { - if e.Error() == "missing_communication_method" { +func (rbvm RequestBookingViewModel) missingCommunicationMethod() bool { + for _, e := range rbvm.Errors { + if e == "missing_communication_method" { return true } } return false } -templ RequestBookingForm(errors []error) { +templ RequestBookingForm(rbvm *RequestBookingViewModel) {
") + templ_7745c5c3_Err = templ.WriteWatchModeString(templ_7745c5c3_Buffer, 22) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -162,15 +236,23 @@ func Index() templ.Component { templ_7745c5c3_Buffer = templ.GetBuffer() defer templ.ReleaseBuffer(templ_7745c5c3_Buffer) } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("