| 1 | package calendar |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "strconv" |
| 6 | "strings" |
| 7 | |
| 8 | "github.com/go-chi/chi/v5" |
| 9 | ) |
| 10 | |
| 11 | // The host's side of booking (booking spec §2.1, §3.4, §7.4): the list |
| 12 | // of pages, the publish switch, and the live pane's fragment. The forms |
| 13 | // are in handlers_booking_form.go. |
| 14 | |
| 15 | // bookingPageRow is one page as the list prints it. |
| 16 | type bookingPageRow struct { |
| 17 | BookingPageView |
| 18 | Lines []string |
| 19 | PublicURL string // empty until published AND the host has a handle |
| 20 | } |
| 21 | |
| 22 | func (h *handlers) bookingPages(w http.ResponseWriter, r *http.Request) { |
| 23 | p, _ := CurrentPerson(r) |
| 24 | pages, err := h.store.BookingPages(r.Context(), p.ID) |
| 25 | if err != nil { |
| 26 | h.fail(w, r, "list booking pages", err) |
| 27 | return |
| 28 | } |
| 29 | handle := "" |
| 30 | if p.Handle != nil { |
| 31 | handle = *p.Handle |
| 32 | } |
| 33 | rows := make([]bookingPageRow, 0, len(pages)) |
| 34 | for _, pg := range pages { |
| 35 | row := bookingPageRow{BookingPageView: pg, Lines: DescribeWindows(pg.Windows)} |
| 36 | if pg.Published && handle != "" { |
| 37 | row.PublicURL = h.origin + "/book/" + handle + "/" + pg.Code |
| 38 | } |
| 39 | rows = append(rows, row) |
| 40 | } |
| 41 | render(w, "booking_pages", h.page(r, "Booking pages", map[string]any{ |
| 42 | "Pages": rows, |
| 43 | "Handle": handle, |
| 44 | "Saved": r.URL.Query().Get("saved") == "1", |
| 45 | })) |
| 46 | } |
| 47 | |
| 48 | // publishBookingPage flips the switch. Publishing is deliberate: the |
| 49 | // button's own sentence says a published page is an oracle for when the |
| 50 | // host is free (spec §7.4). |
| 51 | func (h *handlers) publishBookingPage(w http.ResponseWriter, r *http.Request) { |
| 52 | p, _ := CurrentPerson(r) |
| 53 | id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64) |
| 54 | _ = r.ParseForm() |
| 55 | err := h.store.PublishBookingPage(r.Context(), p.ID, id, r.FormValue("publish") == "1") |
| 56 | if NotFound(err) { |
| 57 | h.errorPage(w, r, http.StatusNotFound, "") |
| 58 | return |
| 59 | } |
| 60 | if err != nil { |
| 61 | h.fail(w, r, "publish booking page", err) |
| 62 | return |
| 63 | } |
| 64 | http.Redirect(w, r, "/settings/booking", http.StatusSeeOther) |
| 65 | } |
| 66 | |
| 67 | // bookingParse answers the live pane: one parser, the Go one, exactly |
| 68 | // as quick add (B7). |
| 69 | func (h *handlers) bookingParse(w http.ResponseWriter, r *http.Request) { |
| 70 | _ = r.ParseForm() |
| 71 | renderFragment(w, "bookingpane", bookingPaneFor(strings.TrimSpace(r.FormValue("text")))) |
| 72 | } |
| 73 | |