Skip to content

Commit

Permalink
Prevent page navigation on form submission
Browse files Browse the repository at this point in the history
  • Loading branch information
Ujstor committed Feb 2, 2025
1 parent 7f17dcd commit 1396b0f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
4 changes: 2 additions & 2 deletions blueprint-ui/cmd/web/components/form.templ
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ var options = OptionsStruct{
}

templ Form() {
<form class="space-y-2" hx-boost="true" action="/update_structure" method="post">
<form class="space-y-2" hx-boost="true" action="/update_structure" method="submit" hx-target="#results" >
<div class="mt-4">
<label
for="projectName"
Expand All @@ -97,7 +97,7 @@ templ Form() {
id="projectName"
class="block w-full rounded-md border-0 px-2 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"
placeholder="my_project"
hx-trigger="keyup changed delay:500ms"
hx-trigger="input changed delay:500ms"
hx-post="/update_structure"
hx-target="#results"
required
Expand Down
7 changes: 3 additions & 4 deletions blueprint-ui/cmd/web/update_structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ func contains(slice []string, value string) bool {
}

func UpdateStructureHandler(w http.ResponseWriter, r *http.Request) {
// Set HTMX response headers
w.Header().Set("HX-Trigger", "updateComplete")
err := r.ParseForm()
if err != nil {
http.Error(w, "Bad Request", http.StatusBadRequest)
Expand All @@ -23,14 +25,11 @@ func UpdateStructureHandler(w http.ResponseWriter, r *http.Request) {

advancedOptions, ok := r.Form["advancedOptions"]
if !ok {
// Handle the case where no checkbox was checked
advancedOptions = []string{}
}

advancedFrontend, ok := r.Form["advancedFrontend"]

if !ok {
// Handle the case where no checkbox was checked
advancedFrontend = []string{}
}

Expand All @@ -43,8 +42,8 @@ func UpdateStructureHandler(w http.ResponseWriter, r *http.Request) {
AdvancedFrontend: advancedFrontend,
AdvancedOptions: advancedOptions,
}
commandStr := components.GetCommandString(options)

commandStr := components.GetCommandString(options)
err = components.FolderStructure(options, commandStr).Render(r.Context(), w)
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
Expand Down

0 comments on commit 1396b0f

Please sign in to comment.