refactor and order template
This commit is contained in:
parent
922478bdeb
commit
2465956fc2
5 changed files with 126 additions and 99 deletions
10
server/dto/pages.go
Normal file
10
server/dto/pages.go
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
package dto
|
||||||
|
|
||||||
|
import "git.bronku.xyz/bronku/cake-order-tracker/models"
|
||||||
|
|
||||||
|
type OrderPage struct {
|
||||||
|
Order models.Order
|
||||||
|
Catalogue []models.Cake
|
||||||
|
Tags []models.Tag
|
||||||
|
BlockedDates []models.BlockedDate
|
||||||
|
}
|
||||||
|
|
@ -10,6 +10,7 @@ import (
|
||||||
|
|
||||||
"git.bronku.xyz/bronku/cake-order-tracker/config"
|
"git.bronku.xyz/bronku/cake-order-tracker/config"
|
||||||
"git.bronku.xyz/bronku/cake-order-tracker/models"
|
"git.bronku.xyz/bronku/cake-order-tracker/models"
|
||||||
|
"git.bronku.xyz/bronku/cake-order-tracker/server/dto"
|
||||||
"git.bronku.xyz/bronku/cake-order-tracker/server/templates"
|
"git.bronku.xyz/bronku/cake-order-tracker/server/templates"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -158,41 +159,53 @@ func (h *Server) blocked(r *http.Request) (templ.Component, int, error) {
|
||||||
return templates.BlockedPage(dates), http.StatusOK, nil
|
return templates.BlockedPage(dates), http.StatusOK, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Server) order(r *http.Request) (templ.Component, int, error) {
|
func internalError(err error) *httpError {
|
||||||
catalogue, err := h.s.GetCakes()
|
return &httpError{http.StatusInternalServerError, err}
|
||||||
if err != nil {
|
|
||||||
return nil, http.StatusInternalServerError, err
|
|
||||||
}
|
}
|
||||||
tags, err := h.s.GetTags()
|
func badRequest(err error) *httpError {
|
||||||
if err != nil {
|
return &httpError{http.StatusBadRequest, err}
|
||||||
return nil, http.StatusInternalServerError, err
|
|
||||||
}
|
}
|
||||||
blockedDates, err := h.s.GetBlockedDates()
|
func notFound(err error) *httpError {
|
||||||
if err != nil {
|
return &httpError{http.StatusNotFound, err}
|
||||||
return nil, http.StatusInternalServerError, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
url := strings.Split(r.URL.Path, "/")
|
func (h *Server) newOrder(r *http.Request) (dto.OrderPage, *httpError) {
|
||||||
if len(url) < 3 || url[2] == "" {
|
var dto dto.OrderPage
|
||||||
if r.Header.Get("HX-Request") == "true" {
|
var err error
|
||||||
return templates.MainContent("Nowe zamówienie", templates.OrderForm(models.Order{Date: time.Now()}, catalogue, tags, blockedDates)), http.StatusOK, nil
|
|
||||||
}
|
|
||||||
return templates.OrderPage(models.Order{Date: time.Now()}, catalogue, tags, blockedDates), http.StatusOK, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
id, err := strconv.Atoi(url[2])
|
dto.Catalogue, err = h.s.GetCakes()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, http.StatusBadRequest, err
|
return dto, internalError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
order, err := h.s.GetOrder(id)
|
dto.Tags, err = h.s.GetTags()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, http.StatusNotFound, err
|
return dto, internalError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.Header.Get("HX-Request") == "true" {
|
dto.BlockedDates, err = h.s.GetBlockedDates()
|
||||||
return templates.MainContent(templates.OrderTitle(order), templates.OrderForm(order, catalogue, tags, blockedDates)), http.StatusOK, nil
|
if err != nil {
|
||||||
|
return dto, internalError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return templates.OrderPage(order, catalogue, tags, blockedDates), http.StatusOK, nil
|
return dto, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Server) order(r *http.Request) (dto.OrderPage, *httpError) {
|
||||||
|
dto, httpErr := h.newOrder(r)
|
||||||
|
if httpErr != nil {
|
||||||
|
return dto, httpErr
|
||||||
|
}
|
||||||
|
|
||||||
|
id, err := strconv.Atoi(r.PathValue("id"))
|
||||||
|
if err != nil {
|
||||||
|
return dto, badRequest(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
dto.Order, err = h.s.GetOrder(id)
|
||||||
|
if err != nil {
|
||||||
|
return dto, notFound(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return dto, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,12 +4,43 @@ import (
|
||||||
"embed"
|
"embed"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/a-h/templ"
|
"git.bronku.xyz/bronku/cake-order-tracker/logging"
|
||||||
|
"git.bronku.xyz/bronku/cake-order-tracker/server/templates"
|
||||||
"git.bronku.xyz/bronku/cake-order-tracker/store"
|
"git.bronku.xyz/bronku/cake-order-tracker/store"
|
||||||
|
"github.com/a-h/templ"
|
||||||
)
|
)
|
||||||
|
|
||||||
type fetcher func(r *http.Request) (templ.Component, int, error)
|
type httpError struct {
|
||||||
|
code int
|
||||||
|
error
|
||||||
|
}
|
||||||
|
|
||||||
|
type fetcher[T any] func(r *http.Request) (T, *httpError)
|
||||||
|
type templateBuilder[T any] func(data T) templ.Component
|
||||||
|
|
||||||
|
func render[T any](title string, fetch fetcher[T], builder templateBuilder[T]) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
data, fetchErr := fetch(r)
|
||||||
|
if fetchErr != nil {
|
||||||
|
logging.ErrorPage(fetchErr, fetchErr.code).ServeHTTP(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Header().Set("content-type", "text/html")
|
||||||
|
|
||||||
|
var component templ.Component
|
||||||
|
if r.Header.Get("HX-Request") == "true" {
|
||||||
|
component = templates.MainContent(title, builder(data))
|
||||||
|
} else {
|
||||||
|
component = templates.Layout(title, builder(data))
|
||||||
|
}
|
||||||
|
|
||||||
|
err := component.Render(r.Context(), w)
|
||||||
|
if err != nil {
|
||||||
|
logging.ErrorPage(err, http.StatusInternalServerError).ServeHTTP(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
s *store.Store
|
s *store.Store
|
||||||
|
|
@ -31,16 +62,17 @@ func New(store *store.Store) *Server {
|
||||||
|
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
mux.HandleFunc("GET /", redirect("/orders", http.StatusSeeOther))
|
mux.HandleFunc("GET /", redirect("/orders", http.StatusSeeOther))
|
||||||
mux.HandleFunc("GET /order", server.render(server.order))
|
mux.HandleFunc("GET /order", render("Nowe zamówienie", server.newOrder, templates.OrderForm))
|
||||||
mux.HandleFunc("GET /orders", server.render(server.orders))
|
mux.HandleFunc("GET /order/{id}", render("Zamówienie", server.order, templates.OrderForm))
|
||||||
mux.HandleFunc("GET /cake", server.render(server.cake))
|
// mux.HandleFunc("GET /orders", render(server.orders))
|
||||||
mux.HandleFunc("GET /cakes", server.render(server.cakes))
|
// mux.HandleFunc("GET /cake", render(server.cake))
|
||||||
mux.HandleFunc("POST /order", server.render(server.postOrder))
|
// mux.HandleFunc("GET /cakes", render(server.cakes))
|
||||||
mux.HandleFunc("POST /order/{id}/status", server.render(server.updateStatus))
|
// mux.HandleFunc("POST /order", render(server.postOrder))
|
||||||
mux.HandleFunc("POST /cake", server.render(server.postCake))
|
// mux.HandleFunc("POST /order/{id}/status", render(server.updateStatus))
|
||||||
mux.HandleFunc("GET /blocked", server.render(server.blocked))
|
// mux.HandleFunc("POST /cake", render(server.postCake))
|
||||||
mux.HandleFunc("POST /blocked", server.render(server.postBlocked))
|
// mux.HandleFunc("GET /blocked", render(server.blocked))
|
||||||
mux.HandleFunc("POST /blocked/{id}/delete", server.render(server.deleteBlocked))
|
// mux.HandleFunc("POST /blocked", render(server.postBlocked))
|
||||||
|
// mux.HandleFunc("POST /blocked/{id}/delete", render(server.deleteBlocked))
|
||||||
|
|
||||||
fs := http.FileServerFS(static)
|
fs := http.FileServerFS(static)
|
||||||
mux.Handle("GET /static/", fs)
|
mux.Handle("GET /static/", fs)
|
||||||
|
|
|
||||||
|
|
@ -1,23 +0,0 @@
|
||||||
package server
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"git.bronku.xyz/bronku/cake-order-tracker/logging"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (h *Server) render(fetch fetcher) http.HandlerFunc {
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
component, code, err := fetch(r)
|
|
||||||
if err != nil {
|
|
||||||
logging.ErrorPage(err, code).ServeHTTP(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
w.Header().Set("content-type", "text/html")
|
|
||||||
err = component.Render(r.Context(), w)
|
|
||||||
if err != nil {
|
|
||||||
logging.ErrorPage(err, http.StatusInternalServerError).ServeHTTP(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -5,15 +5,12 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"git.bronku.xyz/bronku/cake-order-tracker/models"
|
"git.bronku.xyz/bronku/cake-order-tracker/models"
|
||||||
|
"git.bronku.xyz/bronku/cake-order-tracker/server/dto"
|
||||||
)
|
)
|
||||||
|
|
||||||
templ OrderPage(order models.Order, catalogue []models.Cake, allTags []models.Tag, blockedDates []models.BlockedDate) {
|
templ OrderForm(dto dto.OrderPage) {
|
||||||
@Layout(OrderTitle(order), OrderForm(order, catalogue, allTags, blockedDates))
|
<form method="post" class="split-pane order-form" onsubmit="return confirmBlockedDate(this)" data-blocked-dates={ blockedDatesJSON(dto.BlockedDates) } data-state={ orderStateJSON(dto.Order) }>
|
||||||
}
|
<input type="hidden" name="id" value={ dto.Order.ID }/>
|
||||||
|
|
||||||
templ OrderForm(order models.Order, catalogue []models.Cake, allTags []models.Tag, blockedDates []models.BlockedDate) {
|
|
||||||
<form method="post" class="split-pane order-form" onsubmit="return confirmBlockedDate(this)" data-blocked-dates={ blockedDatesJSON(blockedDates) } data-state={ orderStateJSON(order) }>
|
|
||||||
<input type="hidden" name="id" value={ strconv.Itoa(order.ID) }/>
|
|
||||||
<div class="order-left">
|
<div class="order-left">
|
||||||
<section class="card">
|
<section class="card">
|
||||||
<header>
|
<header>
|
||||||
|
|
@ -26,33 +23,32 @@ templ OrderForm(order models.Order, catalogue []models.Cake, allTags []models.Ta
|
||||||
<div class="form-grid">
|
<div class="form-grid">
|
||||||
<div>
|
<div>
|
||||||
<label>Imię *</label>
|
<label>Imię *</label>
|
||||||
<input type="text" name="name" value={ order.Name } placeholder="Jan"/>
|
<input type="text" name="name" value={ dto.Order.Name } placeholder="Jan"/>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label>Nazwisko *</label>
|
<label>Nazwisko *</label>
|
||||||
<input type="text" name="surname" value={ order.Surname } placeholder="Kowalski"/>
|
<input type="text" name="surname" value={ dto.Order.Surname } placeholder="Kowalski"/>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label>Telefon</label>
|
<label>Telefon</label>
|
||||||
<input type="tel" name="phone" value={ order.Phone } placeholder="+48 123 456 789"/>
|
<input type="tel" name="phone" value={ dto.Order.Phone } placeholder="+48 123 456 789"/>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label>Lokalizacja</label>
|
<label>Lokalizacja</label>
|
||||||
<div class="toggle-group">
|
<div class="toggle-group">
|
||||||
<input type="hidden" name="location" value={ order.Location }/>
|
<input type="hidden" name="location" value={ dto.Order.Location }/>
|
||||||
<button type="button" class={ "toggle-btn", templ.KV("active", order.Location == "Kartuzy") } onclick="setLocation(this, 'Kartuzy')">Kartuzy</button>
|
<button type="button" class={ "toggle-btn", templ.KV("active", dto.Order.Location == "Kartuzy") } onclick="setLocation(this, 'Kartuzy')">Kartuzy</button>
|
||||||
<button type="button" class={ "toggle-btn", templ.KV("active", order.Location == "Somonino") } onclick="setLocation(this, 'Somonino')">Somonino</button>
|
<button type="button" class={ "toggle-btn", templ.KV("active", dto.Order.Location == "Somonino") } onclick="setLocation(this, 'Somonino')">Somonino</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label>Data dostawy *</label>
|
<label>Data dostawy *</label>
|
||||||
<input type="date" name="date" value={ order.Date.Format("2006-01-02") }/>
|
<input type="date" name="date" value={ dto.Order.Date.Format("2006-01-02") }/>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label>Zaliczka</label>
|
<label>Zaliczka</label>
|
||||||
<input type="number" name="paid" min="0" step="0.01" value={ order.Paid.String() } oninput={ templ.JSFuncCall("updatePrepaid") }/>
|
<input type="number" name="paid" min="0" step="0.01" value={ dto.Order.Paid.String() } oninput={ templ.JSFuncCall("updatePrepaid") }/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
|
@ -81,16 +77,16 @@ templ OrderForm(order models.Order, catalogue []models.Cake, allTags []models.Ta
|
||||||
<input type="text" placeholder="Filtruj produkty..." oninput="filterCatalogue(this)"/>
|
<input type="text" placeholder="Filtruj produkty..." oninput="filterCatalogue(this)"/>
|
||||||
</div>
|
</div>
|
||||||
</header>
|
</header>
|
||||||
if len(allTags) > 0 {
|
if len(dto.Tags) > 0 {
|
||||||
<div class="tag-row">
|
<div class="tag-row">
|
||||||
for _, t := range allTags {
|
for _, t := range dto.Tags {
|
||||||
<button type="button" class="tag-chip" data-tag={ t.Name } onclick="toggleCatalogueTagFilter(this)">{ t.Name }</button>
|
<button type="button" class="tag-chip" data-tag={ t.Name } onclick="toggleCatalogueTagFilter(this)">{ t.Name }</button>
|
||||||
}
|
}
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
<div class="catalogue-scroll">
|
<div class="catalogue-scroll">
|
||||||
<ul id="catalogue-grid">
|
<ul id="catalogue-grid">
|
||||||
for _, c := range catalogue {
|
for _, c := range dto.Catalogue {
|
||||||
<li data-name={ c.Name } data-tags={ tagNames(c.Tags) }>
|
<li data-name={ c.Name } data-tags={ tagNames(c.Tags) }>
|
||||||
<div>
|
<div>
|
||||||
<div class="icon cake">
|
<div class="icon cake">
|
||||||
|
|
@ -125,7 +121,7 @@ templ OrderForm(order models.Order, catalogue []models.Cake, allTags []models.Ta
|
||||||
</h2>
|
</h2>
|
||||||
</header>
|
</header>
|
||||||
<ul id="basket-items">
|
<ul id="basket-items">
|
||||||
for _, c := range order.Cakes {
|
for _, c := range dto.Order.Cakes {
|
||||||
<li data-cake-id={ strconv.Itoa(c.ID) }>
|
<li data-cake-id={ strconv.Itoa(c.ID) }>
|
||||||
<div class="basket-item-body">
|
<div class="basket-item-body">
|
||||||
<div class="name">{ c.Name }</div>
|
<div class="name">{ c.Name }</div>
|
||||||
|
|
@ -138,7 +134,7 @@ templ OrderForm(order models.Order, catalogue []models.Cake, allTags []models.Ta
|
||||||
<input type="hidden" name={ "cake[" + strconv.Itoa(c.ID) + "]" } value={ strconv.Itoa(c.Amount) }/>
|
<input type="hidden" name={ "cake[" + strconv.Itoa(c.ID) + "]" } value={ strconv.Itoa(c.Amount) }/>
|
||||||
</li>
|
</li>
|
||||||
}
|
}
|
||||||
for _, s := range order.SpecialCakes {
|
for _, s := range dto.Order.SpecialCakes {
|
||||||
<li data-special-id={ strconv.Itoa(s.ID) }>
|
<li data-special-id={ strconv.Itoa(s.ID) }>
|
||||||
<div class="basket-item-body">
|
<div class="basket-item-body">
|
||||||
<div class="name">{ s.Name }</div>
|
<div class="name">{ s.Name }</div>
|
||||||
|
|
@ -173,15 +169,15 @@ templ OrderForm(order models.Order, catalogue []models.Cake, allTags []models.Ta
|
||||||
<div class="summary-totals">
|
<div class="summary-totals">
|
||||||
<div class="total-row">
|
<div class="total-row">
|
||||||
<span>Subtotal</span>
|
<span>Subtotal</span>
|
||||||
<span id="subtotal-price">{ order.Subtotal().String() } PLN</span>
|
<span id="subtotal-price">{ dto.Order.Subtotal().String() } PLN</span>
|
||||||
</div>
|
</div>
|
||||||
<div class="total-row">
|
<div class="total-row">
|
||||||
<span>Zaliczka</span>
|
<span>Zaliczka</span>
|
||||||
<span id="paid-amount">{ order.Paid.String() } PLN</span>
|
<span id="paid-amount">{ dto.Order.Paid.String() } PLN</span>
|
||||||
</div>
|
</div>
|
||||||
<div class="total-row grand">
|
<div class="total-row grand">
|
||||||
<span>Do zapłaty</span>
|
<span>Do zapłaty</span>
|
||||||
<span id="total-price">{ order.Total().String() } PLN</span>
|
<span id="total-price">{ dto.Order.Total().String() } PLN</span>
|
||||||
</div>
|
</div>
|
||||||
<button type="submit" class="btn-finalize">
|
<button type="submit" class="btn-finalize">
|
||||||
<span class="material-symbols-outlined">check_circle</span>
|
<span class="material-symbols-outlined">check_circle</span>
|
||||||
|
|
@ -191,8 +187,7 @@ templ OrderForm(order models.Order, catalogue []models.Cake, allTags []models.Ta
|
||||||
</section>
|
</section>
|
||||||
</aside>
|
</aside>
|
||||||
</form>
|
</form>
|
||||||
|
@SpecialCakeOverlay(dto.Tags)
|
||||||
@SpecialCakeOverlay(allTags)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func orderStateJSON(order models.Order) string {
|
func orderStateJSON(order models.Order) string {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue