Skip to content

Commit

Permalink
Added support for non-HTMX submit
Browse files Browse the repository at this point in the history
  • Loading branch information
akclace committed Oct 23, 2024
1 parent 1fe13d0 commit f4ee3a8
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
26 changes: 25 additions & 1 deletion internal/app/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (a *Action) runAction(w http.ResponseWriter, r *http.Request) {

// Save the request context in the starlark thread local
thread.SetLocal(types.TL_CONTEXT, r.Context())
//isHtmxRequest := r.Header.Get("HX-Request") == "true" && !(r.Header.Get("HX-Boosted") == "true")
isHtmxRequest := r.Header.Get("HX-Request") == "true"

r.ParseForm()
var err error
Expand Down Expand Up @@ -278,6 +278,22 @@ func (a *Action) runAction(w http.ResponseWriter, r *http.Request) {
return
}

pageInput := map[string]any{
"name": a.name,
"description": a.description,
"path": a.pagePath,
"lightTheme": a.LightTheme,
"darkTheme": a.DarkTheme,
}

if !isHtmxRequest {
err = a.actionTemplate.ExecuteTemplate(w, "header", pageInput)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}

// Render the result message
err = a.actionTemplate.ExecuteTemplate(w, "status", status)
if err != nil {
Expand Down Expand Up @@ -321,6 +337,14 @@ func (a *Action) runAction(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

if !isHtmxRequest {
err = a.actionTemplate.ExecuteTemplate(w, "footer", pageInput)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}

func (a *Action) renderResults(w http.ResponseWriter, report string, valuesMap []map[string]any, valuesStr []string) error {
Expand Down
53 changes: 53 additions & 0 deletions internal/app/tests/appaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ app = ace.app("testApp",
testutil.AssertStringContains(t, response.Body.String(), `id="param_param1"`)

request = httptest.NewRequest("POST", reqPath, nil)
request.Header.Set("HX-Request", "true")
response = httptest.NewRecorder()
a.ServeHTTP(response, request)
testutil.AssertEqualsInt(t, "code", 200, response.Code)
Expand Down Expand Up @@ -126,6 +127,7 @@ param("param3", description="param3 description", type=INT, default=10)`,
testutil.AssertStringContains(t, response.Body.String(), `id="param_param1"`)

request = httptest.NewRequest("POST", "/test", nil)
request.Header.Set("HX-Request", "true")
response = httptest.NewRecorder()
a.ServeHTTP(response, request)
testutil.AssertEqualsInt(t, "code", 200, response.Code)
Expand Down Expand Up @@ -196,6 +198,7 @@ app = ace.app("testApp",
testutil.AssertStringContains(t, response.Body.String(), `id="param_param1"`)

request = httptest.NewRequest("POST", "/test", nil)
request.Header.Set("HX-Request", "true")
response = httptest.NewRecorder()
a.ServeHTTP(response, request)
testutil.AssertEqualsInt(t, "code", 200, response.Code)
Expand Down Expand Up @@ -256,6 +259,7 @@ app = ace.app("testApp",
testutil.AssertStringContains(t, response.Body.String(), `id="param_param1"`)

request = httptest.NewRequest("POST", "/test", nil)
request.Header.Set("HX-Request", "true")
response = httptest.NewRecorder()
a.ServeHTTP(response, request)
testutil.AssertEqualsInt(t, "code", 200, response.Code)
Expand Down Expand Up @@ -309,6 +313,7 @@ app = ace.app("testApp",
testutil.AssertStringContains(t, response.Body.String(), `id="param_param1"`)

request = httptest.NewRequest("POST", "/test", nil)
request.Header.Set("HX-Request", "true")
response = httptest.NewRecorder()
a.ServeHTTP(response, request)
testutil.AssertEqualsInt(t, "code", 200, response.Code)
Expand Down Expand Up @@ -372,6 +377,7 @@ app = ace.app("testApp",
testutil.AssertStringContains(t, response.Body.String(), `id="param_param1"`)

request = httptest.NewRequest("POST", "/test", nil)
request.Header.Set("HX-Request", "true")
response = httptest.NewRecorder()
a.ServeHTTP(response, request)
testutil.AssertEqualsInt(t, "code", 200, response.Code)
Expand Down Expand Up @@ -445,6 +451,7 @@ param("param3", description="param3 description", type=INT, default=10)`,
}

request = httptest.NewRequest("POST", "/test", strings.NewReader(values.Encode()))
request.Header.Set("HX-Request", "true")
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")

response = httptest.NewRecorder()
Expand Down Expand Up @@ -526,6 +533,7 @@ app = ace.app("testApp",
testutil.AssertStringContains(t, response.Body.String(), `id="param_param1"`)

request = httptest.NewRequest("POST", "/test", nil)
request.Header.Set("HX-Request", "true")
response = httptest.NewRecorder()
a.ServeHTTP(response, request)
testutil.AssertEqualsInt(t, "code", 200, response.Code)
Expand Down Expand Up @@ -557,6 +565,7 @@ app = ace.app("testApp",
testutil.AssertStringContains(t, response.Body.String(), `id="param_param1"`)

request = httptest.NewRequest("POST", "/test", nil)
request.Header.Set("HX-Request", "true")
response = httptest.NewRecorder()
a.ServeHTTP(response, request)
testutil.AssertEqualsInt(t, "code", 200, response.Code)
Expand Down Expand Up @@ -640,6 +649,7 @@ param("param3", description="param3 description", type=INT, default=10)`,
}

request = httptest.NewRequest("POST", "/test", strings.NewReader(values.Encode()))
request.Header.Set("HX-Request", "true")
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")

response = httptest.NewRecorder()
Expand Down Expand Up @@ -676,6 +686,7 @@ param("param3", description="param3 description", type=INT, default=10)`,
}

request = httptest.NewRequest("POST", "/test", strings.NewReader(values.Encode()))
request.Header.Set("HX-Request", "true")
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")

response = httptest.NewRecorder()
Expand All @@ -690,6 +701,7 @@ param("param3", description="param3 description", type=INT, default=10)`,
}

request = httptest.NewRequest("POST", "/test", strings.NewReader(values.Encode()))
request.Header.Set("HX-Request", "true")
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")

response = httptest.NewRecorder()
Expand Down Expand Up @@ -751,3 +763,44 @@ param("param3", description="param3 description", type=INT, default=10)`,
</table>
</div>`, response.Body.String())
}

func TestNonHtmxRequest(t *testing.T) {
logger := testutil.TestLogger()
fileData := map[string]string{
"app.star": `
def handler(dry_run, args):
return ace.result(status="done", values=[{"a": 1, "b": "abc"}], report="custom")
app = ace.app("testApp",
actions=[ace.action("testAction", "/", handler)])
`,
"params.star": `param("param1", description="param1 description", type=STRING, default="myvalue")`,
"myfile.go.html": `{{block "custom" .}} customdata {{end}}`,
}
a, _, err := CreateTestApp(logger, fileData)
if err != nil {
t.Fatalf("Error %s", err)
}

request := httptest.NewRequest("GET", "/test/", nil)
response := httptest.NewRecorder()
a.ServeHTTP(response, request)

testutil.AssertEqualsInt(t, "code", 200, response.Code)
testutil.AssertStringContains(t, response.Body.String(), "<title>testAction</title>")
testutil.AssertStringContains(t, response.Body.String(), `id="param_param1"`)

request = httptest.NewRequest("POST", "/test", nil)
// no header request.Header.Set("HX-Request", "true")
response = httptest.NewRecorder()
a.ServeHTTP(response, request)
testutil.AssertEqualsInt(t, "code", 200, response.Code)
body := response.Body.String()
if !strings.Contains(body, "<!DOCTYPE html>") {
t.Errorf("Expected full html response, got: %s", body)
}
if !strings.Contains(body, "</html>") {
t.Errorf("Expected full html response, got: %s", body)
}
}

0 comments on commit f4ee3a8

Please sign in to comment.