Skip to content

Commit

Permalink
Added push url for actions
Browse files Browse the repository at this point in the history
  • Loading branch information
akclace committed Nov 24, 2024
1 parent a2e9c26 commit 66ea307
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
17 changes: 16 additions & 1 deletion internal/app/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"io"
"io/fs"
"net/http"
"net/url"
"path"
"slices"
"strconv"
Expand Down Expand Up @@ -187,13 +188,16 @@ func (a *Action) runAction(w http.ResponseWriter, r *http.Request) {
args[k] = v
}

qsParams := url.Values{}

// Update args with submitted form values
for _, param := range a.params {
formValue := r.Form.Get(param.Name)
if formValue == "" {
if param.Type == starlark_type.BOOLEAN {
// Form does not submit unchecked checkboxes, set to false
args[param.Name] = starlark.Bool(false)
qsParams.Add(param.Name, "false")
}
} else {
newVal, err := apptype.ParamStringToType(param.Name, param.Type, formValue)
Expand All @@ -202,6 +206,7 @@ func (a *Action) runAction(w http.ResponseWriter, r *http.Request) {
return
}
args[param.Name] = newVal
qsParams.Add(param.Name, formValue)
}
}

Expand Down Expand Up @@ -306,6 +311,9 @@ func (a *Action) runAction(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
} else {
// Set the push URL for HTMX
w.Header().Set("HX-Push-Url", a.pagePath+"?"+qsParams.Encode())
}

// Render the result message
Expand Down Expand Up @@ -524,6 +532,7 @@ const (
)

func (a *Action) getForm(w http.ResponseWriter, r *http.Request) {
queryParams := r.URL.Query()
params := make([]ParamDef, 0, len(a.params))

options := make(map[string][]string)
Expand Down Expand Up @@ -557,6 +566,12 @@ func (a *Action) getForm(w http.ResponseWriter, r *http.Request) {
return
}

qValue := queryParams.Get(p.Name)
if qValue != "" {
// Prefer value from query params
value = qValue
}

param.Value = value // Default to string format
param.InputType = "text"
if p.Type == starlark_type.BOOLEAN {
Expand All @@ -572,7 +587,7 @@ func (a *Action) getForm(w http.ResponseWriter, r *http.Request) {
} else if options[p.Name] != nil {
param.InputType = "select"
param.Options = options[p.Name]
param.Value = param.Options[0]
param.Value = value
}

params = append(params, param)
Expand Down
10 changes: 5 additions & 5 deletions internal/app/action/form.go.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@
<select
id="param_{{ .Name }}"
class="select select-bordered w-full"
name="{{ .Name }}"
{{ if .Value }}
value="{{ .Value }}"
{{ end }}>
name="{{ .Name }}">
{{ $sel := .Value }}
{{ range .Options }}
<option value="{{ . }}">{{ . }}</option>
<option value="{{ . }}" {{ if eq $sel . }}selected{{ end }}>
{{ . }}
</option>
{{ end }}
</select>
<div id="param_{{ .Name }}_error" class="text-error mt-1"></div>
Expand Down
1 change: 1 addition & 0 deletions internal/app/tests/appaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ param("param3", description="param3 description", type=INT, default=10)`,
response = httptest.NewRecorder()
a.ServeHTTP(response, request)
testutil.AssertEqualsInt(t, "code", 200, response.Code)
testutil.AssertEqualsString(t, "push url", "/test?param1=abc&param2=true&param3=20", response.Header().Get("HX-Push-Url"))
testutil.AssertStringMatch(t, "match response", `
<div class="text-lg text-bold">
done
Expand Down

0 comments on commit 66ea307

Please sign in to comment.