From e7cd34cd21c20a2f84ba6d71078ae23610390fd6 Mon Sep 17 00:00:00 2001 From: Calvin McLean Date: Wed, 24 Apr 2024 21:53:00 -0700 Subject: [PATCH 1/2] Do not fetch WaterHistory when listing Zones --- garden-app/server/zone_responses.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/garden-app/server/zone_responses.go b/garden-app/server/zone_responses.go index 7f4b3a1b..da7ec0c7 100644 --- a/garden-app/server/zone_responses.go +++ b/garden-app/server/zone_responses.go @@ -101,16 +101,16 @@ func (zr *ZoneResponse) Render(w http.ResponseWriter, r *http.Request) error { ) if render.GetAcceptedContentType(r) == render.ContentTypeHTML { - history, apiErr := zr.api.getWaterHistoryFromRequest(r, zr.Zone, logger) - // non-fatal error so we can still render the HTML page - if apiErr != nil { - logger.Error("error getting water history", "error", apiErr) - zr.HistoryError = apiErr.ErrorText - return nil + // only get history when rendering a ZoneDetail page + if zr.api.GetIDParam(r) != "" { + history, apiErr := zr.api.getWaterHistoryFromRequest(r, zr.Zone, logger) + if apiErr != nil { + logger.Error("error getting water history", "error", apiErr) + zr.HistoryError = apiErr.ErrorText + } + zr.History = NewZoneWaterHistoryResponse(history) } - zr.History = NewZoneWaterHistoryResponse(history) - if r.Method == http.MethodPut { w.Header().Add("HX-Trigger", "newZone") } From db836ae30be6bbe494334b259d1b807d7c5b0eb6 Mon Sep 17 00:00:00 2001 From: Calvin McLean Date: Wed, 24 Apr 2024 21:53:17 -0700 Subject: [PATCH 2/2] Add quick water dropdown to ZoneCard --- garden-app/server/templates.go | 21 +++++++++++++++++++++ garden-app/server/templates/zones.html | 21 +++++++++++++++++---- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/garden-app/server/templates.go b/garden-app/server/templates.go index 721c6e2e..c3aa3e96 100644 --- a/garden-app/server/templates.go +++ b/garden-app/server/templates.go @@ -129,6 +129,27 @@ func templateFuncs(r *http.Request) map[string]any { return item.String() == target.String() }) }, + "ZoneQuickWater": func(z *ZoneResponse) []string { + var waterDurations []string + if z.NextWater.Duration == nil || z.NextWater.Duration.Duration == 0 { + return append(waterDurations, "15m", "30m", "1h") + } + + divideDuration := func(d time.Duration, f int) string { + divided := time.Duration(int(d.Seconds())/f) * time.Second + if divided > time.Minute { + divided = (divided + 30*time.Second).Truncate(time.Minute) + } + return formatDuration(&pkg.Duration{Duration: divided}) + } + + baseDuration := z.NextWater.Duration.Duration + waterDurations = append(waterDurations, divideDuration(baseDuration, 4)) + waterDurations = append(waterDurations, divideDuration(baseDuration, 2)) + waterDurations = append(waterDurations, divideDuration(baseDuration, 1)) + + return waterDurations + }, } } diff --git a/garden-app/server/templates/zones.html b/garden-app/server/templates/zones.html index 6308a41b..c179275d 100644 --- a/garden-app/server/templates/zones.html +++ b/garden-app/server/templates/zones.html @@ -46,7 +46,7 @@

{{ define "nextWaterDetail" }} {{ if and .Duration .Time }}
- Watering for + Watering for {{ FormatDuration .Duration }} {{ FormatUpcomingDate .Time }}
@@ -61,7 +61,6 @@

{{ define "infoPopover" }}
-

{{ .ID }}

Position: {{ .Position }}

@@ -73,17 +72,31 @@

{{ define "zoneActionButton" }}
- +
+{{ end }} + +{{ define "quickWaterLinks" }} +{{ $zone := . }} +{{ range (ZoneQuickWater .) }} +
  • + + {{ . }} + +
  • +{{ end }} {{ end }} \ No newline at end of file