diff --git a/htmx-alpine-pt2/main.go b/htmx-alpine-pt2/main.go index e363da6..f634f25 100644 --- a/htmx-alpine-pt2/main.go +++ b/htmx-alpine-pt2/main.go @@ -18,6 +18,7 @@ type TodoList struct { ID int Name string Completed bool + Error string ui.CRUDActions } @@ -89,7 +90,7 @@ func main() { mux.HandleFunc("/todo", func(w http.ResponseWriter, r *http.Request) { err := r.ParseForm() if err != nil { - http.Error(w, "bad request", http.StatusBadRequest) + http.Error(w, fmt.Sprintf("error loading template, err: %v", err), http.StatusInternalServerError) return } @@ -99,12 +100,12 @@ func main() { if name == "" { w.Header().Set("HX-Retarget", "#create-todo-form") w.Header().Set("HX-Reswap", "outerHTML") - err := h.RenderW(w, "fragments/todo-row-form", hrender.H{ + err := h.RenderW(w, "fragments/todo-new-form", hrender.H{ "Name": name, "Error": "El nombre es obligatorio", }) if err != nil { - http.Error(w, "error loading template", http.StatusInternalServerError) + http.Error(w, fmt.Sprintf("error loading template, err: %v", err), http.StatusInternalServerError) } return } @@ -133,7 +134,7 @@ func main() { "Completed": false, }) if err != nil { - http.Error(w, "error loading template", http.StatusInternalServerError) + http.Error(w, fmt.Sprintf("error loading template, err: %v", err), http.StatusInternalServerError) } }, "POST") @@ -141,7 +142,7 @@ func main() { idStr := r.PathValue("id") id, err := strconv.Atoi(idStr) if err != nil { - http.Error(w, "invalid id", http.StatusBadRequest) + http.Error(w, fmt.Sprintf("error loading template, err: %v", err), http.StatusInternalServerError) return } @@ -155,7 +156,7 @@ func main() { } if foundItem == nil { - http.Error(w, "item not found", http.StatusNotFound) + http.Error(w, fmt.Sprintf("error loading template, err: %v", err), http.StatusInternalServerError) return } @@ -184,6 +185,20 @@ func main() { } newName := r.FormValue("name") + if newName == "" { + w.Header().Set("HX-Retarget", fmt.Sprintf("#todo-edit-form-%d", id)) + w.Header().Set("HX-Reswap", "outerHTML") + err := h.RenderW(w, "fragments/todo-edit-form", hrender.H{ + "ID": id, + "Name": newName, + "Error": "El nombre es obligatorio", + }) + if err != nil { + http.Error(w, "error loading template", http.StatusInternalServerError) + } + return + } + if newName == "some error" { log.Println("error triggered") http.Error(w, "bad request", http.StatusBadRequest) diff --git a/htmx-alpine-pt2/templates/fragments/todo-edit-form.html b/htmx-alpine-pt2/templates/fragments/todo-edit-form.html new file mode 100644 index 0000000..b7d3934 --- /dev/null +++ b/htmx-alpine-pt2/templates/fragments/todo-edit-form.html @@ -0,0 +1,31 @@ +
diff --git a/htmx-alpine-pt2/templates/fragments/todo-row-form.html b/htmx-alpine-pt2/templates/fragments/todo-new-form.html similarity index 99% rename from htmx-alpine-pt2/templates/fragments/todo-row-form.html rename to htmx-alpine-pt2/templates/fragments/todo-new-form.html index ff0e47b..b91d94a 100644 --- a/htmx-alpine-pt2/templates/fragments/todo-row-form.html +++ b/htmx-alpine-pt2/templates/fragments/todo-new-form.html @@ -7,7 +7,7 @@ hx-target-error="this" x-data="{ hasError: {{ if .Error }}true{{ else }}false{{ end }} - }" + }" @htmx:after-request=" if($event.detail.successful && $event.detail.target.id === 'todo-list-body') { $el.reset(); diff --git a/htmx-alpine-pt2/templates/fragments/todo-row.html b/htmx-alpine-pt2/templates/fragments/todo-row.html index 25f5f86..58b29e3 100644 --- a/htmx-alpine-pt2/templates/fragments/todo-row.html +++ b/htmx-alpine-pt2/templates/fragments/todo-row.html @@ -9,6 +9,7 @@ cancelEdit() { this.isEditing = false; this.form.name = '{{ .Name }}'; + this.$dispatch('edit-cancelled'); } }" > @@ -49,24 +50,7 @@ - + \ No newline at end of file diff --git a/htmx-alpine-pt2/templates/fragments/todo-widget.html b/htmx-alpine-pt2/templates/fragments/todo-widget.html index 2a3ab75..1aa5ff9 100644 --- a/htmx-alpine-pt2/templates/fragments/todo-widget.html +++ b/htmx-alpine-pt2/templates/fragments/todo-widget.html @@ -13,8 +13,8 @@ -