Skip to content

Commit ada4dd1

Browse files
committed
Update action links on form submit
1 parent 9966605 commit ada4dd1

File tree

3 files changed

+113
-19
lines changed

3 files changed

+113
-19
lines changed

internal/app/action/action.go

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ func (a *Action) execAction(w http.ResponseWriter, r *http.Request, isSuggest, i
384384
}
385385

386386
if isSuggest {
387-
a.handleSuggestResponse(w, ret)
387+
a.handleSuggestResponse(w, qsParams.Encode(), ret)
388388
return
389389
}
390390

@@ -463,6 +463,16 @@ func (a *Action) execAction(w http.ResponseWriter, r *http.Request, isSuggest, i
463463
return
464464
}
465465

466+
if len(a.Links) > 1 {
467+
linksWithQS := a.getLinksWithQS(qsParams.Encode())
468+
input := map[string]any{"links": linksWithQS}
469+
err = a.actionTemplate.ExecuteTemplate(w, "dropdown", input)
470+
if err != nil {
471+
http.Error(w, err.Error(), http.StatusInternalServerError)
472+
return
473+
}
474+
}
475+
466476
// Render the param error messages, using HTMX OOB
467477
errorMsgs := map[string]string{}
468478
errorKeys := []string{}
@@ -775,16 +785,7 @@ func (a *Action) getForm(w http.ResponseWriter, r *http.Request) {
775785
params = append(params, param)
776786
}
777787

778-
linksWithQS := make([]ActionLink, 0, len(a.Links))
779-
for _, link := range a.Links {
780-
if link.Path != a.pagePath { // Don't add self link
781-
if r.URL.RawQuery != "" {
782-
link.Path = link.Path + "?" + r.URL.RawQuery
783-
}
784-
linksWithQS = append(linksWithQS, link)
785-
}
786-
}
787-
788+
linksWithQS := a.getLinksWithQS(r.URL.RawQuery)
788789
input := map[string]any{
789790
"dev": a.isDev,
790791
"name": a.name,
@@ -806,7 +807,20 @@ func (a *Action) getForm(w http.ResponseWriter, r *http.Request) {
806807
}
807808
}
808809

809-
func (a *Action) handleSuggestResponse(w http.ResponseWriter, retVal starlark.Value) {
810+
func (a *Action) getLinksWithQS(qs string) []ActionLink {
811+
linksWithQS := make([]ActionLink, 0, len(a.Links))
812+
for _, link := range a.Links {
813+
if link.Path != a.pagePath { // Don't add self link
814+
if qs != "" {
815+
link.Path = link.Path + "?" + qs
816+
}
817+
linksWithQS = append(linksWithQS, link)
818+
}
819+
}
820+
return linksWithQS
821+
}
822+
823+
func (a *Action) handleSuggestResponse(w http.ResponseWriter, paramQS string, retVal starlark.Value) {
810824
ret, err := starlark_type.UnmarshalStarlark(retVal)
811825
if err != nil {
812826
http.Error(w, fmt.Sprintf("error unmarshalling suggest response: %s", err), http.StatusInternalServerError)
@@ -824,6 +838,16 @@ func (a *Action) handleSuggestResponse(w http.ResponseWriter, retVal starlark.Va
824838
return
825839
}
826840

841+
if len(a.Links) > 1 {
842+
linksWithQS := a.getLinksWithQS(paramQS)
843+
input := map[string]any{"links": linksWithQS}
844+
err = a.actionTemplate.ExecuteTemplate(w, "dropdown", input)
845+
if err != nil {
846+
http.Error(w, err.Error(), http.StatusInternalServerError)
847+
return
848+
}
849+
}
850+
827851
if retIsString {
828852
// No suggestions available
829853
return

internal/app/action/form.go.html

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,19 @@
5555
<path d="M4 18l16 0" />
5656
</svg>
5757
</div>
58-
<ul
59-
tabindex="0"
60-
class="dropdown-content menu bg-base-100 rounded-box z-[1] w-52 p-2 shadow">
61-
{{ range .links }}
62-
<li><a href="{{ .Path }}">{{ .Name }}</a></li>
63-
{{ end }}
64-
</ul>
58+
59+
{{ block "dropdown" . }}
60+
<ul
61+
id="dropdown-menu"
62+
hx-swap-oob="true"
63+
hx-swap="outerHTML"
64+
tabindex="0"
65+
class="dropdown-content menu bg-base-100 rounded-box z-[1] w-52 p-2 shadow">
66+
{{ range .links }}
67+
<li><a href="{{ .Path }}">{{ .Name }}</a></li>
68+
{{ end }}
69+
</ul>
70+
{{ end }}
6571
</div>
6672
{{ end }}
6773
</div>

internal/app/tests/appaction_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,70 @@ app = ace.app("testApp",
871871
t.Errorf("actions switcher should not have current action, got %s", body)
872872
}
873873
testutil.AssertStringContains(t, body, `<li><a href="/test/test1?param1=abc">test1Action</a></li>`)
874+
875+
values := url.Values{
876+
"param1": {"p1val"},
877+
}
878+
request = httptest.NewRequest("POST", "/test/test1", strings.NewReader(values.Encode()))
879+
request.Header.Set("HX-Request", "true")
880+
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
881+
882+
response = httptest.NewRecorder()
883+
a.ServeHTTP(response, request)
884+
testutil.AssertEqualsInt(t, "code", 200, response.Code)
885+
testutil.AssertStringMatch(t, "response", `
886+
<div class="text-lg text-bold">
887+
done
888+
</div>
889+
890+
<ul
891+
id="dropdown-menu"
892+
hx-swap-oob="true"
893+
hx-swap="outerHTML"
894+
tabindex="0"
895+
class="dropdown-content menu bg-base-100 rounded-box z-[1] w-52 p-2 shadow">
896+
897+
<li><a href="/test/test2?param1=p1val">test2Action</a></li>
898+
899+
</ul>
900+
901+
<div
902+
id="param_param1_error"
903+
hx-swap-oob="true"
904+
hx-swap="outerHTML"
905+
class="text-error mt-1">
906+
907+
</div>
908+
909+
<div id="action_result" hx-swap-oob="true" hx-swap="outerHTML">
910+
<div class="divider text-lg text-secondary">Report</div>
911+
<div class="overflow-x-auto">
912+
<table
913+
class="table table-auto min-w-full table-zebra text-sm md:text-xl font-mono">
914+
<thead>
915+
<tr class="text-primary">
916+
917+
<th>a</th>
918+
919+
<th>b</th>
920+
921+
</tr>
922+
</thead>
923+
<tbody>
924+
925+
<tr>
926+
927+
<td>1</td>
928+
929+
<td>abc</td>
930+
931+
</tr>
932+
933+
</tbody>
934+
</table>
935+
</div>
936+
</div>
937+
`, response.Body.String())
874938
}
875939

876940
func TestDisplayTypes(t *testing.T) {

0 commit comments

Comments
 (0)