Skip to content

Commit

Permalink
Merge pull request #157 from calvinmclean/feature/quick-water
Browse files Browse the repository at this point in the history
Add quick water UI
  • Loading branch information
calvinmclean committed Apr 25, 2024
2 parents 1dc1295 + db836ae commit 32a93aa
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
21 changes: 21 additions & 0 deletions garden-app/server/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
}

Expand Down
21 changes: 17 additions & 4 deletions garden-app/server/templates/zones.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ <h3 class="uk-card-title uk-margin-remove-bottom uk-text-center">
{{ define "nextWaterDetail" }}
{{ if and .Duration .Time }}
<div>
</span> Watering for
Watering for
<span class="uk-text-primary">{{ FormatDuration .Duration }}</span>
{{ FormatUpcomingDate .Time }}
</div>
Expand All @@ -61,7 +61,6 @@ <h3 class="uk-card-title uk-margin-remove-bottom uk-text-center">

{{ define "infoPopover" }}
<div class="uk-inline">
<span class="uk-margin-small-right" uk-icon="info"></span>
<div uk-dropdown>
<p><b>{{ .ID }}</b></p>
<p>Position: {{ .Position }}</p>
Expand All @@ -73,17 +72,31 @@ <h3 class="uk-card-title uk-margin-remove-bottom uk-text-center">
{{ define "zoneActionButton" }}
<div id="action-modal-here"></div>
<div class="uk-inline">
<button class="uk-button uk-button-default" type="button">Actions</button>
<button class="uk-button uk-button-default" type="button">Quick Water</button>
<div uk-dropdown>
<ul class="uk-nav uk-dropdown-nav">
<li>
<a hx-get="/gardens/{{ .GardenID }}/zones/{{ .ID }}/components?type=action_modal"
hx-headers='{"Accept": "text/html"}' hx-target="#action-modal-here"
_="on htmx:afterOnLoad wait 10ms then add .uk-open to #modal">
<span uk-icon="cloud-download"></span> Water
Custom
</a>
</li>

{{ template "quickWaterLinks" . }}
</ul>
</div>
</div>
{{ end }}

{{ define "quickWaterLinks" }}
{{ $zone := . }}
{{ range (ZoneQuickWater .) }}
<li>
<a hx-post="/gardens/{{ $zone.GardenID }}/zones/{{ $zone.ID }}/action" hx-include="this" hx-swap="none" {{
template "actionFeedback" "primary" }}>
<input type="hidden" name="water.duration" value="{{ . }}"> {{ . }}
</a>
</li>
{{ end }}
{{ end }}
16 changes: 8 additions & 8 deletions garden-app/server/zone_responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down

0 comments on commit 32a93aa

Please sign in to comment.