From 925b7b7a6a6de1f40262d0fc96be7920b26b0021 Mon Sep 17 00:00:00 2001 From: GrailFinder Date: Tue, 2 Apr 2024 12:29:25 +0300 Subject: Feat: add htmx to show|hide form --- components/add_action_form.html | 7 +++++++ components/index.html | 3 ++- internal/handlers/elements.go | 22 ++++++++++++++++++++++ internal/models/models.go | 1 + internal/server/router.go | 4 ++++ 5 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 internal/handlers/elements.go diff --git a/components/add_action_form.html b/components/add_action_form.html index 70403dc..95c869f 100644 --- a/components/add_action_form.html +++ b/components/add_action_form.html @@ -1,5 +1,6 @@ {{define "actionform"}}
+

Add action


@@ -16,3 +17,9 @@
{{end}} + +{{define "showformbtn"}} +
+ +
+{{end}} diff --git a/components/index.html b/components/index.html index 6848f1a..b3da4ea 100644 --- a/components/index.html +++ b/components/index.html @@ -6,6 +6,7 @@ +
@@ -14,7 +15,7 @@ {{ template "UserScore" . }}
- {{ template "actionform" }} + {{ template "showformbtn" }}
diff --git a/internal/handlers/elements.go b/internal/handlers/elements.go new file mode 100644 index 0000000..c0da7f0 --- /dev/null +++ b/internal/handlers/elements.go @@ -0,0 +1,22 @@ +package handlers + +import ( + "html/template" + "net/http" +) + +func (h *Handlers) ServeShowForm(w http.ResponseWriter, r *http.Request) { + tmpl, err := template.ParseGlob("components/*.html") + if err != nil { + panic(err) + } + tmpl.ExecuteTemplate(w, "actionform", nil) +} + +func (h *Handlers) ServeHideForm(w http.ResponseWriter, r *http.Request) { + tmpl, err := template.ParseGlob("components/*.html") + if err != nil { + panic(err) + } + tmpl.ExecuteTemplate(w, "showformbtn", nil) +} diff --git a/internal/models/models.go b/internal/models/models.go index 42990a3..5d6dfa6 100644 --- a/internal/models/models.go +++ b/internal/models/models.go @@ -21,5 +21,6 @@ type ( Magnitude uint8 Repeatable bool Type ActionType + Done bool } ) diff --git a/internal/server/router.go b/internal/server/router.go index 9b03371..726f8b8 100644 --- a/internal/server/router.go +++ b/internal/server/router.go @@ -23,6 +23,10 @@ func (srv *server) ListenToRequests() { mux.HandleFunc("GET /", h.MainPage) mux.HandleFunc("POST /", h.HandleForm) + // ====== elements ====== + mux.HandleFunc("GET /showform", h.ServeShowForm) + mux.HandleFunc("GET /hideform", h.ServeHideForm) + fmt.Printf("Listening on %v\n", server.Addr) server.ListenAndServe() } -- cgit v1.2.3