Skip to content

Commit

Permalink
Add UI for WaterSchedule.NotificationClientID
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinmclean committed Jul 10, 2024
1 parent 2d99fa7 commit 87ff3e1
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
2 changes: 1 addition & 1 deletion garden-app/server/garden.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func NewGardenAPI() *GardensAPI {
func (api *GardensAPI) gardenModalRenderer(ctx context.Context, g *pkg.Garden) render.Renderer {
notificationClients, err := api.storageClient.NotificationClientConfigs.GetAll(ctx, nil)
if err != nil {
return babyapi.InternalServerError(fmt.Errorf("error getting all waterschedules to create zone modal: %w", err))
return babyapi.InternalServerError(fmt.Errorf("error getting all notification clients to create garden modal: %w", err))

Check warning on line 117 in garden-app/server/garden.go

View check run for this annotation

Codecov / codecov/patch

garden-app/server/garden.go#L114-L117

Added lines #L114 - L117 were not covered by tests
}

slices.SortFunc(notificationClients, func(nc1 *notifications.Client, nc2 *notifications.Client) int {
Expand Down
9 changes: 5 additions & 4 deletions garden-app/server/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,11 @@ func templateFuncs(r *http.Request) map[string]any {
return item.String() == target.String()
})
},
"CompareNotificationClientID": func(ncID babyapi.ID, ls *pkg.LightSchedule) bool {
return ls != nil &&
ls.NotificationClientID != nil &&
ncID.String() == *ls.NotificationClientID
"CompareNotificationClientID": func(ncID babyapi.ID, parent interface {
GetNotificationClientID() string
},
) bool {
return ncID.String() == parent.GetNotificationClientID()
},

Check warning on line 164 in garden-app/server/templates.go

View check run for this annotation

Codecov / codecov/patch

garden-app/server/templates.go#L162-L164

Added lines #L162 - L164 were not covered by tests
"ZoneQuickWater": func(z *ZoneResponse) []string {
var waterDurations []string
Expand Down
19 changes: 16 additions & 3 deletions garden-app/server/templates/water_schedule_modal.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ <h3 class="uk-modal-title">{{ if .Duration }}{{ $name }}{{ else }}Create Water S
name="Interval">
</div>
<div class="uk-margin">
<input class="uk-input" value="{{ if .StartTime }}{{ .StartTime }}{{ else }}15:04:05Z{{ end }}"
placeholder="Start Time" name="StartTime">

<div class="uk-margin">
{{ if .StartTime }}
{{ template "startTimeInput" (args "Name" "StartTime" "StartTime" .StartTime) }}
Expand All @@ -49,6 +46,22 @@ <h3 class="uk-modal-title">{{ if .Duration }}{{ $name }}{{ else }}Create Water S
{{ MonthRows .ActivePeriod false }}
</select>
</div>

<div class="uk-margin">
<label class="uk-form-label" for="notification-client-select">Notification Client</label>
<select id="notification-client-select" class="uk-select" name="NotificationClientID">
{{ $noClientSelected := true }}
{{ if ne . nil }}
$noClientSelected = eq .NotificationClientID nil
{{ end }}
<option disabled {{ if $noClientSelected }}selected{{ end }}>Notification Client</option>

{{ $ws := . }}
{{ range $i, $nc := .NotificationClients }}
<option value="{{ $nc.ID }}" {{ if CompareNotificationClientID $nc.ID $ws }}selected{{ end }}>{{ $nc.Name }}</option>
{{ end }}
</select>
</div>

{{ template "modalSubmitButton" }}
{{ if .Duration }}
Expand Down
23 changes: 21 additions & 2 deletions garden-app/server/water_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import (
"errors"
"fmt"
"net/http"
"slices"
"strings"

"github.com/calvinmclean/automated-garden/garden-app/pkg"
"github.com/calvinmclean/automated-garden/garden-app/pkg/notifications"
"github.com/calvinmclean/automated-garden/garden-app/pkg/storage"
"github.com/calvinmclean/automated-garden/garden-app/worker"
"github.com/calvinmclean/babyapi"
Expand Down Expand Up @@ -80,7 +83,7 @@ func NewWaterSchedulesAPI() *WaterSchedulesAPI {
api.AddCustomRoute(http.MethodGet, "/components", babyapi.Handler(func(_ http.ResponseWriter, r *http.Request) render.Renderer {
switch r.URL.Query().Get("type") {
case "create_modal":
return waterScheduleModalTemplate.Renderer(&pkg.WaterSchedule{
return api.waterScheduleModalRenderer(r.Context(), &pkg.WaterSchedule{

Check warning on line 86 in garden-app/server/water_schedule.go

View check run for this annotation

Codecov / codecov/patch

garden-app/server/water_schedule.go#L86

Added line #L86 was not covered by tests
ID: babyapi.NewID(),
})
default:
Expand All @@ -91,7 +94,7 @@ func NewWaterSchedulesAPI() *WaterSchedulesAPI {
api.AddCustomIDRoute(http.MethodGet, "/components", api.GetRequestedResourceAndDo(func(r *http.Request, ws *pkg.WaterSchedule) (render.Renderer, *babyapi.ErrResponse) {
switch r.URL.Query().Get("type") {
case "edit_modal":
return waterScheduleModalTemplate.Renderer(ws), nil
return api.waterScheduleModalRenderer(r.Context(), ws), nil

Check warning on line 97 in garden-app/server/water_schedule.go

View check run for this annotation

Codecov / codecov/patch

garden-app/server/water_schedule.go#L97

Added line #L97 was not covered by tests
case "detail_modal":
return waterScheduleDetailModalTemplate.Renderer(ws), nil
default:
Expand All @@ -104,6 +107,22 @@ func NewWaterSchedulesAPI() *WaterSchedulesAPI {
return api
}

func (api *WaterSchedulesAPI) waterScheduleModalRenderer(ctx context.Context, ws *pkg.WaterSchedule) render.Renderer {
notificationClients, err := api.storageClient.NotificationClientConfigs.GetAll(ctx, nil)
if err != nil {
return babyapi.InternalServerError(fmt.Errorf("error getting all notification clients to create water schedule modal: %w", err))

Check warning on line 113 in garden-app/server/water_schedule.go

View check run for this annotation

Codecov / codecov/patch

garden-app/server/water_schedule.go#L110-L113

Added lines #L110 - L113 were not covered by tests
}

slices.SortFunc(notificationClients, func(nc1 *notifications.Client, nc2 *notifications.Client) int {
return strings.Compare(nc1.Name, nc2.Name)
})

Check warning on line 118 in garden-app/server/water_schedule.go

View check run for this annotation

Codecov / codecov/patch

garden-app/server/water_schedule.go#L116-L118

Added lines #L116 - L118 were not covered by tests

return waterScheduleModalTemplate.Renderer(struct {
*pkg.WaterSchedule
NotificationClients []*notifications.Client
}{ws, notificationClients})

Check warning on line 123 in garden-app/server/water_schedule.go

View check run for this annotation

Codecov / codecov/patch

garden-app/server/water_schedule.go#L120-L123

Added lines #L120 - L123 were not covered by tests
}

func (api *WaterSchedulesAPI) setup(storageClient *storage.Client, worker *worker.Worker) error {
api.storageClient = storageClient
api.worker = worker
Expand Down

0 comments on commit 87ff3e1

Please sign in to comment.