-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6dbd59a
commit c731d2b
Showing
5 changed files
with
171 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{{ define "WeatherClientModal" }} | ||
<div id="modal" class="uk-modal" style="display:block;"> | ||
<div class="uk-modal-dialog uk-modal-body uk-text-center"> | ||
<h3 class="uk-modal-title">{{ if .Type }}{{ .Type }}{{ else }}Create Weather Client{{ end }}</h3> | ||
|
||
<form _="on submit take .uk-open from #modal" hx-put="/weather_clients/{{ .ID }}" | ||
hx-headers='{"Accept": "text/html"}' hx-swap="none"> | ||
<input type="hidden" value="{{ .ID }}" name="ID"> | ||
<div class="uk-margin"> | ||
<input class="uk-input" value="{{ .Type }}" placeholder="Type" name="Type"> | ||
</div> | ||
|
||
<!-- TODO: use select/dropdown for Type and use selection to render a form with relevant inputs --> | ||
|
||
{{ template "modalSubmitButton" }} | ||
{{ if .Type }} | ||
{{ template "deleteButton" ( | ||
args "HXDelete" (print "/weather_clients/" .ID) "HXTarget" (print "#weather-client-card-" .ID) | ||
) }} | ||
{{ end }} | ||
{{ template "modalCloseButton" }} | ||
</form> | ||
</div> | ||
</div> | ||
{{ end }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{{ define "WeatherClientsPage" }} | ||
{{ template "start" }} | ||
{{ template "WeatherClients" . }} | ||
{{ template "end" }} | ||
{{ end }} | ||
|
||
{{ define "WeatherClients" }} | ||
<div hx-swap="outerHTML" hx-get="/weather_clients?refresh=true" hx-headers='{"Accept": "text/html"}' | ||
hx-trigger="{{ if NotRefresh }}load, {{ end }}newWeatherClient from:body" uk-grid> | ||
{{ range .Items }} | ||
{{ template "weatherClientCard" . }} | ||
{{ end }} | ||
</div> | ||
{{ end }} | ||
|
||
{{ define "weatherClientCard" }} | ||
<div class="uk-width-1-2@m" id="weather-client-card-{{ .ID }}"> | ||
<div class="uk-card uk-card-default" style="margin: 5%;"> | ||
<div class="uk-card-header uk-text-center"> | ||
<h3 class="uk-card-title uk-margin-remove-bottom"> | ||
{{ .ID }} | ||
</h3> | ||
{{ template "cardEditButton" (print "/weather_clients/" .ID "/components?type=edit_modal") }} | ||
</div> | ||
<div class="uk-card-body"> | ||
{{ .Type }} | ||
</div> | ||
</div> | ||
</div> | ||
{{ end }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package server | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"slices" | ||
"strings" | ||
|
||
"github.com/calvinmclean/automated-garden/garden-app/pkg/weather" | ||
"github.com/calvinmclean/babyapi" | ||
"github.com/go-chi/render" | ||
) | ||
|
||
type WeatherClientTestResponse struct { | ||
WeatherData | ||
} | ||
|
||
func (resp *WeatherClientTestResponse) Render(_ http.ResponseWriter, _ *http.Request) error { | ||
return nil | ||
} | ||
|
||
type WeatherClientResponse struct { | ||
*weather.Config | ||
|
||
Links []Link `json:"links,omitempty"` | ||
} | ||
|
||
// Render ... | ||
func (resp *WeatherClientResponse) Render(w http.ResponseWriter, r *http.Request) error { | ||
if resp != nil { | ||
resp.Links = append(resp.Links, | ||
Link{ | ||
"self", | ||
fmt.Sprintf("%s/%s", weatherClientsBasePath, resp.ID), | ||
}, | ||
) | ||
} | ||
|
||
if render.GetAcceptedContentType(r) == render.ContentTypeHTML && r.Method == http.MethodPut { | ||
w.Header().Add("HX-Trigger", "newWeatherClient") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
type AllWeatherClientsResponse struct { | ||
babyapi.ResourceList[*WeatherClientResponse] | ||
} | ||
|
||
func (aws AllWeatherClientsResponse) Render(w http.ResponseWriter, r *http.Request) error { | ||
return aws.ResourceList.Render(w, r) | ||
} | ||
|
||
func (aws AllWeatherClientsResponse) HTML(r *http.Request) string { | ||
slices.SortFunc(aws.Items, func(w *WeatherClientResponse, x *WeatherClientResponse) int { | ||
return strings.Compare(w.Type, x.Type) | ||
}) | ||
|
||
if r.URL.Query().Get("refresh") == "true" { | ||
return weatherClientsTemplate.Render(r, aws) | ||
} | ||
|
||
return weatherClientsPageTemplate.Render(r, aws) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters