release: repair the catalog 404 and stop the page jumping (#3)
Showstopper. Adding, editing or deleting a dish redirected to /ruoat, which stopped existing when the tab was renamed to Ruuat — every one of those actions ended on a 404. Live in v20260905-4. The tests missed it because they asserted only a 303; a redirect to a dead URL is still a 303. They now assert the target. The page no longer jumps. Deleting a dish partway down the catalog, or opening a day in Kirjaa, sent the browser to the top. Both now patch in place via Datastar — bin, pencil, day rows, dish pills, save, delete, cancel and Näytä lisää. Links stay links and forms stay forms, so it works without JavaScript. Non-production tabs are labelled. ENV=dev gives dev · Foodster. Contributing guide added, and commits now take Conventional Commit types. ⚠️ Breaking: rewrite the server's .env in this deploy. Environment variables lost the FOODSTER_ prefix; the app refuses to start on an unset PASSWORD. REPO=… TAG=latest PASSWORD=… HOST=foodster.kessinen.com ENV=prod PUID=1000 PGID=1000 TZ=Europe/Helsinki PUID/PGID rather than UID/GID — UID is read-only in bash and would be silently overwritten. Co-authored-by: Esa Kataja <[email protected]> Reviewed-on: #3
This commit was merged in pull request #3.
This commit is contained in:
+162
-44
@@ -98,16 +98,56 @@ type logView struct {
|
||||
Confirming bool
|
||||
}
|
||||
|
||||
func (a *app) index(w http.ResponseWriter, r *http.Request) {
|
||||
date := a.date(r)
|
||||
v := logView{
|
||||
Date: date,
|
||||
Today: today(a.loc),
|
||||
Search: strings.TrimSpace(r.URL.Query().Get("haku")),
|
||||
Checked: map[int64]bool{},
|
||||
}
|
||||
// logOptions is what the Kirjaa screen is being asked to show. Pulled out of
|
||||
// the request for a page load or a patch, and set directly after a write,
|
||||
// where the answer is simply "that day, nothing else open".
|
||||
type logOptions struct {
|
||||
Date time.Time
|
||||
Dish string // ?ruoka=, opening the sides step
|
||||
Changing bool // ?muuta=, swapping the dish on a logged day
|
||||
Confirming bool // ?poista=, asking before deleting the entry
|
||||
Search string
|
||||
}
|
||||
|
||||
v.Confirming = r.URL.Query().Get("poista") != ""
|
||||
func (a *app) logOptionsFrom(r *http.Request) logOptions {
|
||||
q := r.URL.Query()
|
||||
return logOptions{
|
||||
Date: a.date(r),
|
||||
Dish: q.Get("ruoka"),
|
||||
Changing: q.Get("muuta") != "",
|
||||
Confirming: q.Get("poista") != "",
|
||||
Search: strings.TrimSpace(q.Get("haku")),
|
||||
}
|
||||
}
|
||||
|
||||
func (a *app) index(w http.ResponseWriter, r *http.Request) {
|
||||
render(w, r, logPage(a.buildLog(r, a.logOptionsFrom(r))))
|
||||
}
|
||||
|
||||
// day patches the list in place. Every link in it calls here rather than
|
||||
// loading a page, so opening a day leaves the scroll position alone.
|
||||
func (a *app) day(w http.ResponseWriter, r *http.Request) {
|
||||
fragment(w, r, dayList(a.buildLog(r, a.logOptionsFrom(r))))
|
||||
}
|
||||
|
||||
// finishDay answers a write: a patch for Datastar, a redirect otherwise.
|
||||
func (a *app) finishDay(w http.ResponseWriter, r *http.Request, date time.Time) {
|
||||
if isDatastar(r) {
|
||||
fragment(w, r, dayList(a.buildLog(r, logOptions{Date: date})))
|
||||
return
|
||||
}
|
||||
a.redirectToDay(w, r, date)
|
||||
}
|
||||
|
||||
func (a *app) buildLog(r *http.Request, o logOptions) logView {
|
||||
date := o.Date
|
||||
v := logView{
|
||||
Date: date,
|
||||
Today: today(a.loc),
|
||||
Search: o.Search,
|
||||
Checked: map[int64]bool{},
|
||||
Confirming: o.Confirming,
|
||||
}
|
||||
|
||||
entry, err := entryFor(a.db, date)
|
||||
if err != nil {
|
||||
@@ -118,8 +158,8 @@ func (a *app) index(w http.ResponseWriter, r *http.Request) {
|
||||
// ?ruoka= opens the sides step for that dish. When it is the dish already
|
||||
// logged, the existing sides come back ticked, which makes editing an
|
||||
// entry the same screen as creating one.
|
||||
if raw := r.URL.Query().Get("ruoka"); raw != "" {
|
||||
if id, err := strconv.ParseInt(raw, 10, 64); err == nil {
|
||||
if o.Dish != "" {
|
||||
if id, err := strconv.ParseInt(o.Dish, 10, 64); err == nil {
|
||||
if dish, err := dishByID(a.db, id); err == nil {
|
||||
v.Chosen = dish
|
||||
if entry != nil && entry.Main.ID == id {
|
||||
@@ -134,7 +174,7 @@ func (a *app) index(w http.ResponseWriter, r *http.Request) {
|
||||
// The board shows when there is nothing logged yet, or when the entry is
|
||||
// being changed. "Muokkaa" on a logged day sets ?muuta=1 and lands here,
|
||||
// so swapping the dish and picking one for the first time are one path.
|
||||
changing := r.URL.Query().Get("muuta") != "" || v.Search != ""
|
||||
changing := o.Changing || v.Search != ""
|
||||
if v.Chosen == nil && (v.Entry == nil || changing) {
|
||||
v.ShowBoard = true
|
||||
if v.Dishes, err = listDishes(a.db, v.Search); err != nil {
|
||||
@@ -157,7 +197,7 @@ func (a *app) index(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
a.loadDays(r, &v)
|
||||
render(w, r, logPage(v))
|
||||
return v
|
||||
}
|
||||
|
||||
// loadDays fills the day list. The selected day expands inside it rather than
|
||||
@@ -215,6 +255,50 @@ func fragment(w http.ResponseWriter, r *http.Request, c templ.Component) {
|
||||
}
|
||||
}
|
||||
|
||||
// isDatastar reports whether the request came from the client library, which
|
||||
// tags its own. Everything below keeps working without JavaScript: the same
|
||||
// handlers redirect instead of patching when the header is absent.
|
||||
func isDatastar(r *http.Request) bool {
|
||||
return r.Header.Get("Datastar-Request") != ""
|
||||
}
|
||||
|
||||
// patchElements sends one Datastar event carrying several elements, each
|
||||
// matched to the page by its id. A text/html response can only replace one
|
||||
// element, and the catalog has to move its list and its forms together —
|
||||
// opening an edit form also has to un-highlight whatever was open before.
|
||||
//
|
||||
// ponytail: about twenty lines instead of the SDK, which brought four modules
|
||||
// for an SSE generator we would otherwise never call.
|
||||
func patchElements(w http.ResponseWriter, r *http.Request, components ...templ.Component) {
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
w.Header().Set("Cache-Control", "no-cache")
|
||||
|
||||
var out strings.Builder
|
||||
out.WriteString("event: datastar-patch-elements\n")
|
||||
for _, c := range components {
|
||||
var html strings.Builder
|
||||
if err := c.Render(r.Context(), &html); err != nil {
|
||||
log.Printf("patch %s: %v", r.URL.Path, err)
|
||||
return
|
||||
}
|
||||
// One `data: elements` line per line of HTML, as the protocol wants.
|
||||
for _, line := range strings.Split(html.String(), "\n") {
|
||||
if strings.TrimSpace(line) == "" {
|
||||
continue
|
||||
}
|
||||
out.WriteString("data: elements ")
|
||||
out.WriteString(line)
|
||||
out.WriteString("\n")
|
||||
}
|
||||
}
|
||||
out.WriteString("\n")
|
||||
|
||||
io.WriteString(w, out.String())
|
||||
if f, ok := w.(http.Flusher); ok {
|
||||
f.Flush()
|
||||
}
|
||||
}
|
||||
|
||||
// searchBoard re-renders the dish board as the search box is typed into.
|
||||
func (a *app) searchBoard(w http.ResponseWriter, r *http.Request) {
|
||||
var signals searchSignals
|
||||
@@ -300,6 +384,12 @@ func (a *app) quickAdd(w http.ResponseWriter, r *http.Request) {
|
||||
log.Printf("quick add: %v", err)
|
||||
form.Err = "Tallennus epäonnistui."
|
||||
default:
|
||||
// Created: straight on to its sides step.
|
||||
opts := logOptions{Date: date, Dish: strconv.FormatInt(id, 10)}
|
||||
if isDatastar(r) {
|
||||
fragment(w, r, dayList(a.buildLog(r, opts)))
|
||||
return
|
||||
}
|
||||
a.redirectToPick(w, r, date, id)
|
||||
return
|
||||
}
|
||||
@@ -307,23 +397,12 @@ func (a *app) quickAdd(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// Rejected: back to the board with the form filled in and the search
|
||||
// still narrowed, so the add card stays on screen.
|
||||
v := logView{
|
||||
Date: date,
|
||||
Today: today(a.loc),
|
||||
Search: form.Name,
|
||||
Checked: map[int64]bool{},
|
||||
New: form,
|
||||
ShowBoard: true,
|
||||
v := a.buildLog(r, logOptions{Date: date, Search: form.Name})
|
||||
v.New = form
|
||||
if isDatastar(r) {
|
||||
fragment(w, r, dayList(v))
|
||||
return
|
||||
}
|
||||
var err error
|
||||
if v.Dishes, err = listDishes(a.db, v.Search); err != nil {
|
||||
log.Printf("list dishes: %v", err)
|
||||
}
|
||||
v.Groups = groupDishes(v.Dishes)
|
||||
if v.Special, err = listSpecial(a.db, v.Search); err != nil {
|
||||
log.Printf("list special: %v", err)
|
||||
}
|
||||
a.loadDays(r, &v)
|
||||
render(w, r, logPage(v))
|
||||
}
|
||||
|
||||
@@ -333,9 +412,7 @@ func (a *app) redirectToPick(w http.ResponseWriter, r *http.Request, date time.T
|
||||
if strings.Contains(target, "?") {
|
||||
sep = "&"
|
||||
}
|
||||
http.Redirect(w, r,
|
||||
target+sep+"ruoka="+strconv.FormatInt(id, 10)+"#"+dayAnchor(date),
|
||||
http.StatusSeeOther)
|
||||
http.Redirect(w, r, target+sep+"ruoka="+strconv.FormatInt(id, 10), http.StatusSeeOther)
|
||||
}
|
||||
|
||||
// save records the meal and redirects, so a refresh cannot double-post.
|
||||
@@ -360,7 +437,7 @@ func (a *app) save(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "tallennus epäonnistui", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
a.redirectToDay(w, r, date)
|
||||
a.finishDay(w, r, date)
|
||||
}
|
||||
|
||||
func (a *app) delete(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -370,13 +447,12 @@ func (a *app) delete(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "poisto epäonnistui", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
a.redirectToDay(w, r, date)
|
||||
a.finishDay(w, r, date)
|
||||
}
|
||||
|
||||
// redirectToDay returns to the day in the list, anchor included, so saving or
|
||||
// deleting leaves the viewport where the work was happening.
|
||||
// redirectToDay is the no-JavaScript path back after a write.
|
||||
func (a *app) redirectToDay(w http.ResponseWriter, r *http.Request, date time.Time) {
|
||||
http.Redirect(w, r, dayLink(date, today(a.loc)), http.StatusSeeOther)
|
||||
http.Redirect(w, r, dayURL("/", date, today(a.loc)), http.StatusSeeOther)
|
||||
}
|
||||
|
||||
// mainForm and sideForm carry what the user typed, so a rejected submission
|
||||
@@ -410,7 +486,20 @@ type catalogView struct {
|
||||
DeleteKind string
|
||||
}
|
||||
|
||||
// catalog renders the whole page. show patches the same state in place, so
|
||||
// nothing navigates: both build the view the same way.
|
||||
func (a *app) catalog(w http.ResponseWriter, r *http.Request) {
|
||||
a.renderCatalog(w, r, a.catalogState(r))
|
||||
}
|
||||
|
||||
// show is what every catalog link actually calls. It patches the list and both
|
||||
// forms rather than loading a page, so opening an edit form or asking to
|
||||
// delete a row leaves the scroll position exactly where it was.
|
||||
func (a *app) show(w http.ResponseWriter, r *http.Request) {
|
||||
a.patchCatalog(w, r, a.catalogState(r))
|
||||
}
|
||||
|
||||
func (a *app) catalogState(r *http.Request) catalogView {
|
||||
v := catalogView{
|
||||
Main: mainForm{Categories: map[string]bool{}, HasSides: true},
|
||||
Search: strings.TrimSpace(r.URL.Query().Get("haku")),
|
||||
@@ -446,10 +535,11 @@ func (a *app) catalog(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
a.renderCatalog(w, r, v)
|
||||
return v
|
||||
}
|
||||
|
||||
func (a *app) renderCatalog(w http.ResponseWriter, r *http.Request, v catalogView) {
|
||||
// fillCatalog loads the lists into a view built from the request.
|
||||
func (a *app) fillCatalog(v *catalogView) {
|
||||
if v.Main.Categories == nil {
|
||||
v.Main.Categories = map[string]bool{}
|
||||
}
|
||||
@@ -465,9 +555,20 @@ func (a *app) renderCatalog(w http.ResponseWriter, r *http.Request, v catalogVie
|
||||
if v.Sides, err = listSides(a.db, v.Search); err != nil {
|
||||
log.Printf("list sides: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *app) renderCatalog(w http.ResponseWriter, r *http.Request, v catalogView) {
|
||||
a.fillCatalog(&v)
|
||||
render(w, r, catalogPage(v))
|
||||
}
|
||||
|
||||
// patchCatalog swaps the list and both forms in one event. They move together:
|
||||
// opening an edit form also has to clear whatever delete was being confirmed.
|
||||
func (a *app) patchCatalog(w http.ResponseWriter, r *http.Request, v catalogView) {
|
||||
a.fillCatalog(&v)
|
||||
patchElements(w, r, catalogList(v), mainForm_(v.Main), sideForm_(v.Side))
|
||||
}
|
||||
|
||||
// saveMain adds or updates a main dish. A rejected form is re-rendered with
|
||||
// the values still in it; a good one redirects, so refresh cannot re-submit.
|
||||
func (a *app) saveMain(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -508,11 +609,28 @@ func (a *app) saveMain(w http.ResponseWriter, r *http.Request) {
|
||||
log.Printf("save main: %v", err)
|
||||
form.Err = "Tallennus epäonnistui."
|
||||
default:
|
||||
http.Redirect(w, r, "/ruoat", http.StatusSeeOther)
|
||||
// Saved: hand back a blank form so it collapses, and a list with
|
||||
// the dish in it.
|
||||
a.finishCatalog(w, r, catalogView{})
|
||||
return
|
||||
}
|
||||
}
|
||||
a.renderCatalog(w, r, catalogView{Main: form})
|
||||
a.finishCatalog(w, r, catalogView{Main: form})
|
||||
}
|
||||
|
||||
// finishCatalog answers a catalog write: a patch for Datastar, a redirect for
|
||||
// a plain form post. Without the redirect, submitting with JavaScript off
|
||||
// would leave the browser sitting on a POST it could not reload.
|
||||
func (a *app) finishCatalog(w http.ResponseWriter, r *http.Request, v catalogView) {
|
||||
if isDatastar(r) {
|
||||
a.patchCatalog(w, r, v)
|
||||
return
|
||||
}
|
||||
if v.Main.Err != "" || v.Side.Err != "" {
|
||||
a.renderCatalog(w, r, v)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, "/ruuat", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func (a *app) saveSide(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -537,11 +655,11 @@ func (a *app) saveSide(w http.ResponseWriter, r *http.Request) {
|
||||
log.Printf("save side: %v", err)
|
||||
form.Err = "Tallennus epäonnistui."
|
||||
default:
|
||||
http.Redirect(w, r, "/ruoat", http.StatusSeeOther)
|
||||
a.finishCatalog(w, r, catalogView{})
|
||||
return
|
||||
}
|
||||
}
|
||||
a.renderCatalog(w, r, catalogView{Side: form})
|
||||
a.finishCatalog(w, r, catalogView{Side: form})
|
||||
}
|
||||
|
||||
// deleteDish soft-deletes, so log entries keep resolving the name (PRD §6).
|
||||
@@ -565,7 +683,7 @@ func (a *app) deleteDish(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "poisto epäonnistui", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, "/ruoat", http.StatusSeeOther)
|
||||
a.finishCatalog(w, r, catalogView{})
|
||||
}
|
||||
|
||||
// importDishes takes a bundle either pasted into the textarea or uploaded as a
|
||||
|
||||
+25
-6
@@ -35,6 +35,21 @@ var staticFS embed.FS
|
||||
// version is replaced at build time with the CalVer tag (see `make image`).
|
||||
var version = "dev"
|
||||
|
||||
// envTag marks the browser tab of anything that is not production, so a dev
|
||||
// instance and the real one open side by side are told apart at a glance.
|
||||
// Empty in production, which is the default.
|
||||
var envTag string
|
||||
|
||||
func setEnvTag(value string) {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" || strings.EqualFold(value, "prod") || strings.EqualFold(value, "production") {
|
||||
envTag = ""
|
||||
return
|
||||
}
|
||||
// Whatever it says, so ENV=staging labels itself too.
|
||||
envTag = strings.ToLower(value)
|
||||
}
|
||||
|
||||
const (
|
||||
listenAddr = ":8080"
|
||||
defaultTZ = "Europe/Helsinki"
|
||||
@@ -55,7 +70,7 @@ func run() error {
|
||||
"import a JSON dish bundle (PRD §7.3 shape) and exit")
|
||||
flag.Parse()
|
||||
|
||||
db, err := openDB(cmp.Or(os.Getenv("FOODSTER_DB"), defaultDB))
|
||||
db, err := openDB(cmp.Or(os.Getenv("DB"), defaultDB))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -66,9 +81,9 @@ func run() error {
|
||||
return runImport(db, *importPath)
|
||||
}
|
||||
|
||||
password := os.Getenv("FOODSTER_PASSWORD")
|
||||
password := os.Getenv("PASSWORD")
|
||||
if password == "" {
|
||||
return errors.New("FOODSTER_PASSWORD is not set")
|
||||
return errors.New("PASSWORD is not set")
|
||||
}
|
||||
|
||||
// Fail rather than fall back to UTC: a silently wrong zone shifts logged
|
||||
@@ -79,9 +94,11 @@ func run() error {
|
||||
return fmt.Errorf("TZ: %w", err)
|
||||
}
|
||||
|
||||
// The container always publishes :8080; FOODSTER_ADDR exists so tests and
|
||||
// a second local instance can pick another port.
|
||||
addr := cmp.Or(os.Getenv("FOODSTER_ADDR"), listenAddr)
|
||||
setEnvTag(os.Getenv("ENV"))
|
||||
|
||||
// The container always publishes :8080; ADDR exists so tests and a second
|
||||
// local instance can pick another port.
|
||||
addr := cmp.Or(os.Getenv("ADDR"), listenAddr)
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: addr,
|
||||
@@ -159,9 +176,11 @@ func routes(db *sql.DB, loc *time.Location, password string) http.Handler {
|
||||
mux.HandleFunc("POST /kirjaa", a.save)
|
||||
mux.HandleFunc("POST /lisaa", a.quickAdd)
|
||||
mux.HandleFunc("GET /etsi", a.searchBoard)
|
||||
mux.HandleFunc("GET /paiva", a.day)
|
||||
mux.HandleFunc("POST /poista", a.delete)
|
||||
mux.HandleFunc("GET /ruuat", a.catalog)
|
||||
mux.HandleFunc("GET /ruuat/etsi", a.searchCatalog)
|
||||
mux.HandleFunc("GET /ruuat/nayta", a.show)
|
||||
mux.HandleFunc("POST /ruuat/paaruoka", a.saveMain)
|
||||
mux.HandleFunc("POST /ruuat/lisuke", a.saveSide)
|
||||
mux.HandleFunc("POST /ruuat/poista", a.deleteDish)
|
||||
|
||||
@@ -105,6 +105,29 @@ func TestReadSignals(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnvTagMarksNonProduction(t *testing.T) {
|
||||
t.Cleanup(func() { envTag = "" })
|
||||
|
||||
cases := []struct{ env, want string }{
|
||||
// Production is the default and must stay unmarked: the tag exists to
|
||||
// pick the dev tab out of two identical ones.
|
||||
{"", "Foodster"},
|
||||
{"prod", "Foodster"},
|
||||
{"PRODUCTION", "Foodster"},
|
||||
{" ", "Foodster"},
|
||||
{"dev", "dev · Foodster"},
|
||||
{"DEV", "dev · Foodster"},
|
||||
{"staging", "staging · Foodster"},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
setEnvTag(c.env)
|
||||
if got := pageTitle("Foodster"); got != c.want {
|
||||
t.Errorf("ENV=%q: title = %q, want %q", c.env, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateCreatesSchema(t *testing.T) {
|
||||
db, err := openDB(t.TempDir() + "/test.db")
|
||||
if err != nil {
|
||||
|
||||
+98
-29
@@ -43,16 +43,21 @@ func dayURL(base string, d, now time.Time) string {
|
||||
return base + "?pvm=" + isoDate(d)
|
||||
}
|
||||
|
||||
// dayAnchor names the element a day expands into.
|
||||
func dayAnchor(d time.Time) string {
|
||||
return "paiva-" + isoDate(d)
|
||||
// showURL turns a catalog page link into the patch endpoint behind it, so the
|
||||
// href and the Datastar call never drift apart.
|
||||
func showURL(pageURL string) string {
|
||||
return strings.Replace(pageURL, "/ruuat?", "/ruuat/nayta?", 1)
|
||||
}
|
||||
|
||||
// dayLink opens a day and lands the viewport on it. Without the fragment the
|
||||
// browser would jump to the top of a page whose selected day might be far
|
||||
// down the list.
|
||||
func dayLink(d, now time.Time) string {
|
||||
return dayURL("/", d, now) + "#" + dayAnchor(d)
|
||||
// dayPatch is the endpoint behind every link in the day list. The href beside
|
||||
// it stays a real page URL for anyone without JavaScript; Datastar calls this
|
||||
// instead and swaps the list where it stands.
|
||||
func dayPatch(d time.Time, param string) string {
|
||||
url := "/paiva?pvm=" + isoDate(d)
|
||||
if param != "" {
|
||||
url += "&" + param
|
||||
}
|
||||
return url
|
||||
}
|
||||
|
||||
// pickSeparator joins a dish onto a day URL, which already carries ?pvm= for
|
||||
@@ -64,15 +69,14 @@ func pickSeparator(v logView) string {
|
||||
return "&"
|
||||
}
|
||||
|
||||
// stepURL is any link inside the open day. It keeps the anchor, so picking a
|
||||
// dish or cancelling stays where the day is instead of throwing the viewport
|
||||
// back to the top of the list.
|
||||
// stepURL is the page URL for a link inside the open day: the fallback when
|
||||
// there is no JavaScript to intercept it.
|
||||
func stepURL(v logView, param string) string {
|
||||
url := dayURL("/", v.Date, v.Today)
|
||||
if param != "" {
|
||||
url += pickSeparator(v) + param
|
||||
}
|
||||
return url + "#" + dayAnchor(v.Date)
|
||||
return url
|
||||
}
|
||||
|
||||
// jsString renders a Go string as a JavaScript literal, for the data-signals
|
||||
@@ -104,6 +108,16 @@ func categoryLabels(d Dish) string {
|
||||
return strings.Join(names, ", ")
|
||||
}
|
||||
|
||||
// pageTitle prefixes the tab title on any instance that is not production.
|
||||
// The tab is the only place a browser shows which of two identical apps you
|
||||
// are looking at.
|
||||
func pageTitle(title string) string {
|
||||
if envTag == "" {
|
||||
return title
|
||||
}
|
||||
return envTag + " · " + title
|
||||
}
|
||||
|
||||
// countFI renders "1 pääruoka" but "16 pääruokaa": Finnish takes the partitive
|
||||
// after every number except one.
|
||||
func countFI(n int, one, many string) string {
|
||||
@@ -126,7 +140,7 @@ templ page(title, current string) {
|
||||
// header rather than butting against it.
|
||||
<meta name="theme-color" media="(prefers-color-scheme: light)" content="#FFFFFF"/>
|
||||
<meta name="theme-color" media="(prefers-color-scheme: dark)" content="#1C1E22"/>
|
||||
<title>{ title }</title>
|
||||
<title>{ pageTitle(title) }</title>
|
||||
<link rel="icon" href="/static/favicon.svg" type="image/svg+xml"/>
|
||||
<link rel="apple-touch-icon" href="/static/apple-touch-icon.png"/>
|
||||
<!-- use-credentials: the manifest is fetched behind Basic auth and
|
||||
@@ -314,13 +328,13 @@ templ logPage(v logView) {
|
||||
// above the list and drop that day out of it, so the rows below jumped up
|
||||
// under the tap. Now nothing moves — the row grows.
|
||||
templ dayList(v logView) {
|
||||
<section class="history">
|
||||
<section class="history" id="paivat">
|
||||
for i, row := range v.History.Rows {
|
||||
if i == 0 || v.History.Rows[i-1].Date.Month() != row.Date.Month() {
|
||||
<p class="monthrule">{ monthFI(row.Date) }</p>
|
||||
}
|
||||
if row.Date.Equal(v.Date) {
|
||||
<div class="open" id={ dayAnchor(row.Date) }>
|
||||
<div class="open">
|
||||
<p class="openday">
|
||||
if row.Date.Equal(v.Today) {
|
||||
Tänään
|
||||
@@ -338,7 +352,11 @@ templ dayList(v logView) {
|
||||
}
|
||||
</div>
|
||||
} else if row.Entry != nil {
|
||||
<a class="entry" href={ templ.SafeURL(dayLink(row.Date, v.Today)) }>
|
||||
<a
|
||||
class="entry"
|
||||
href={ templ.SafeURL(dayURL("/", row.Date, v.Today)) }
|
||||
data-on:click__prevent={ "@get('" + dayPatch(row.Date, "") + "')" }
|
||||
>
|
||||
<time>{ dayLabelFI(row.Date) }</time>
|
||||
<div>
|
||||
<div class="nm">
|
||||
@@ -350,7 +368,11 @@ templ dayList(v logView) {
|
||||
<span class="chev">›</span>
|
||||
</a>
|
||||
} else {
|
||||
<a class="gapline" href={ templ.SafeURL(dayLink(row.Date, v.Today)) }>
|
||||
<a
|
||||
class="gapline"
|
||||
href={ templ.SafeURL(dayURL("/", row.Date, v.Today)) }
|
||||
data-on:click__prevent={ "@get('" + dayPatch(row.Date, "") + "')" }
|
||||
>
|
||||
<time>{ dayLabelFI(row.Date) }</time>
|
||||
<span>Ei merkintää</span>
|
||||
<span class="act">Merkitse</span>
|
||||
@@ -361,6 +383,7 @@ templ dayList(v logView) {
|
||||
<a
|
||||
class="more"
|
||||
href={ templ.SafeURL(dayURL("/", v.Date, v.Today) + pickSeparator(v) + "paivat=" + strconv.Itoa(v.HistoryMore)) }
|
||||
data-on:click__prevent={ "@get('" + dayPatch(v.Date, "paivat="+strconv.Itoa(v.HistoryMore)) + "')" }
|
||||
>Näytä lisää</a>
|
||||
}
|
||||
</section>
|
||||
@@ -436,6 +459,7 @@ templ boardList(v logView) {
|
||||
<a
|
||||
class="pill plain"
|
||||
href={ templ.SafeURL(stepURL(v, "ruoka="+strconv.FormatInt(d.ID, 10))) }
|
||||
data-on:click__prevent={ "@get('" + dayPatch(v.Date, "ruoka="+strconv.FormatInt(d.ID, 10)) + "')" }
|
||||
>
|
||||
@categoryIcon(d.CategoryKey())
|
||||
{ d.Name }
|
||||
@@ -471,7 +495,11 @@ templ quickAddCard(v logView) {
|
||||
if v.New.Err != "" {
|
||||
<p class="formerr">{ v.New.Err }</p>
|
||||
}
|
||||
<form method="post" action="/lisaa">
|
||||
<form
|
||||
method="post"
|
||||
action="/lisaa"
|
||||
data-on:submit__prevent="@post('/lisaa', {contentType: 'form'})"
|
||||
>
|
||||
<input type="hidden" name="pvm" value={ isoDate(v.Date) }/>
|
||||
<label class="field">
|
||||
<span>Nimi</span>
|
||||
@@ -503,6 +531,7 @@ templ dishPill(d Dish, v logView) {
|
||||
<a
|
||||
class={ "pill", d.Size() }
|
||||
href={ templ.SafeURL(stepURL(v, "ruoka="+strconv.FormatInt(d.ID, 10))) }
|
||||
data-on:click__prevent={ "@get('" + dayPatch(v.Date, "ruoka="+strconv.FormatInt(d.ID, 10)) + "')" }
|
||||
>
|
||||
@categoryIcon(d.CategoryKey())
|
||||
{ d.Name }
|
||||
@@ -518,7 +547,11 @@ templ sidesStep(v logView) {
|
||||
@categoryIcon(v.Chosen.CategoryKey())
|
||||
{ v.Chosen.Name }
|
||||
</h3>
|
||||
<form method="post" action="/kirjaa">
|
||||
<form
|
||||
method="post"
|
||||
action="/kirjaa"
|
||||
data-on:submit__prevent="@post('/kirjaa', {contentType: 'form'})"
|
||||
>
|
||||
<input type="hidden" name="pvm" value={ isoDate(v.Date) }/>
|
||||
<input type="hidden" name="ruoka" value={ strconv.FormatInt(v.Chosen.ID, 10) }/>
|
||||
if v.Chosen.HasSides && len(v.Sides) > 0 {
|
||||
@@ -540,7 +573,11 @@ templ sidesStep(v logView) {
|
||||
}
|
||||
<button class="primary" type="submit">Tallenna</button>
|
||||
</form>
|
||||
<a class="ghost" href={ templ.SafeURL(stepURL(v, "")) }>Peruuta</a>
|
||||
<a
|
||||
class="ghost"
|
||||
href={ templ.SafeURL(stepURL(v, "")) }
|
||||
data-on:click__prevent={ "@get('" + dayPatch(v.Date, "") + "')" }
|
||||
>Peruuta</a>
|
||||
</section>
|
||||
}
|
||||
|
||||
@@ -561,21 +598,31 @@ templ loggedCard(v logView) {
|
||||
if v.Confirming {
|
||||
<p class="q">Poistetaanko merkintä?</p>
|
||||
<div class="pair">
|
||||
<form method="post" action="/poista">
|
||||
<form
|
||||
method="post"
|
||||
action="/poista"
|
||||
data-on:submit__prevent="@post('/poista', {contentType: 'form'})"
|
||||
>
|
||||
<input type="hidden" name="pvm" value={ isoDate(v.Date) }/>
|
||||
<button class="btn del" type="submit">Kyllä, poista</button>
|
||||
</form>
|
||||
<a class="btn" href={ templ.SafeURL(stepURL(v, "")) }>Peruuta</a>
|
||||
<a
|
||||
class="btn"
|
||||
href={ templ.SafeURL(stepURL(v, "")) }
|
||||
data-on:click__prevent={ "@get('" + dayPatch(v.Date, "") + "')" }
|
||||
>Peruuta</a>
|
||||
</div>
|
||||
} else {
|
||||
<div class="pair">
|
||||
<a
|
||||
class="btn"
|
||||
href={ templ.SafeURL(stepURL(v, "muuta=1")) }
|
||||
data-on:click__prevent={ "@get('" + dayPatch(v.Date, "muuta=1") + "')" }
|
||||
>Muokkaa</a>
|
||||
<a
|
||||
class="btn del"
|
||||
href={ templ.SafeURL(stepURL(v, "poista=1")) }
|
||||
data-on:click__prevent={ "@get('" + dayPatch(v.Date, "poista=1") + "')" }
|
||||
>Poista</a>
|
||||
</div>
|
||||
}
|
||||
@@ -689,25 +736,39 @@ templ emptyNote(search string) {
|
||||
// rowActions is a pencil and a bin, until the bin is tapped: then the row
|
||||
// asks. An icon is a smaller target to hit by accident than a word, and the
|
||||
// dish disappears from every picker the moment it goes.
|
||||
// Every control here is a real link or form, so the page still works without
|
||||
// JavaScript. Datastar intercepts them and patches the list in place instead,
|
||||
// which is the whole point: a delete confirmation halfway down a long list
|
||||
// must not send the browser back to the top.
|
||||
templ rowActions(v catalogView, editURL string, id int64, kind string) {
|
||||
if v.DeleteID == id && v.DeleteKind == kind {
|
||||
<div class="rowactions confirming">
|
||||
<span>Poista?</span>
|
||||
<form method="post" action="/ruuat/poista">
|
||||
<form
|
||||
method="post"
|
||||
action="/ruuat/poista"
|
||||
data-on:submit__prevent="@post('/ruuat/poista', {contentType: 'form'})"
|
||||
>
|
||||
<input type="hidden" name="id" value={ strconv.FormatInt(id, 10) }/>
|
||||
<input type="hidden" name="tyyppi" value={ kind }/>
|
||||
<button type="submit" class="del">Kyllä</button>
|
||||
</form>
|
||||
<a href="/ruuat">Peruuta</a>
|
||||
<a href="/ruuat" data-on:click__prevent="@get('/ruuat/nayta')">Peruuta</a>
|
||||
</div>
|
||||
} else {
|
||||
<div class="rowactions">
|
||||
<a href={ templ.SafeURL(editURL) } aria-label="Muokkaa" title="Muokkaa">
|
||||
<a
|
||||
href={ templ.SafeURL(editURL) }
|
||||
data-on:click__prevent={ "@get('" + showURL(editURL) + "')" }
|
||||
aria-label="Muokkaa"
|
||||
title="Muokkaa"
|
||||
>
|
||||
@iconPencil()
|
||||
</a>
|
||||
<a
|
||||
class="del"
|
||||
href={ templ.SafeURL("/ruuat?poista=" + strconv.FormatInt(id, 10) + "&tyyppi=" + kind) }
|
||||
data-on:click__prevent={ "@get('/ruuat/nayta?poista=" + strconv.FormatInt(id, 10) + "&tyyppi=" + kind + "')" }
|
||||
aria-label="Poista"
|
||||
title="Poista"
|
||||
>
|
||||
@@ -746,7 +807,11 @@ templ mainForm_(f mainForm) {
|
||||
if f.Err != "" {
|
||||
<p class="formerr">{ f.Err }</p>
|
||||
}
|
||||
<form method="post" action="/ruuat/paaruoka">
|
||||
<form
|
||||
method="post"
|
||||
action="/ruuat/paaruoka"
|
||||
data-on:submit__prevent="@post('/ruuat/paaruoka', {contentType: 'form'})"
|
||||
>
|
||||
if f.ID != 0 {
|
||||
<input type="hidden" name="id" value={ strconv.FormatInt(f.ID, 10) }/>
|
||||
}
|
||||
@@ -774,7 +839,7 @@ templ mainForm_(f mainForm) {
|
||||
<button class="primary" type="submit">Tallenna</button>
|
||||
</form>
|
||||
if f.ID != 0 {
|
||||
<a class="ghost" href="/ruuat">Peruuta</a>
|
||||
<a class="ghost" href="/ruuat" data-on:click__prevent="@get('/ruuat/nayta')">Peruuta</a>
|
||||
}
|
||||
</details>
|
||||
}
|
||||
@@ -803,7 +868,11 @@ templ sideForm_(f sideForm) {
|
||||
if f.Err != "" {
|
||||
<p class="formerr">{ f.Err }</p>
|
||||
}
|
||||
<form method="post" action="/ruuat/lisuke">
|
||||
<form
|
||||
method="post"
|
||||
action="/ruuat/lisuke"
|
||||
data-on:submit__prevent="@post('/ruuat/lisuke', {contentType: 'form'})"
|
||||
>
|
||||
if f.ID != 0 {
|
||||
<input type="hidden" name="id" value={ strconv.FormatInt(f.ID, 10) }/>
|
||||
}
|
||||
@@ -814,7 +883,7 @@ templ sideForm_(f sideForm) {
|
||||
<button class="primary" type="submit">Tallenna</button>
|
||||
</form>
|
||||
if f.ID != 0 {
|
||||
<a class="ghost" href="/ruuat">Peruuta</a>
|
||||
<a class="ghost" href="/ruuat" data-on:click__prevent="@get('/ruuat/nayta')">Peruuta</a>
|
||||
}
|
||||
</details>
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user