From 4ea04ef5b382b147d789240ef78dc24bd142df95 Mon Sep 17 00:00:00 2001 From: GrailFinder Date: Sat, 23 Mar 2024 16:39:52 +0300 Subject: Enha: demonstraton of layouts --- components/actions_table.html | 42 ++++++++++++++++++++++++++++++++++++++++++ components/index.html | 33 ++++++++++++++++++--------------- internal/handlers/main.go | 20 +++++++++++++------- internal/models/models.go | 2 -- 4 files changed, 73 insertions(+), 24 deletions(-) create mode 100644 components/actions_table.html diff --git a/components/actions_table.html b/components/actions_table.html new file mode 100644 index 0000000..0e9ecc1 --- /dev/null +++ b/components/actions_table.html @@ -0,0 +1,42 @@ +{{define "UserScore"}} + + + + + + + + + + + +
IDBurnTimeScore
{{.ID}}{{.BurnTime}}{{.Score}}
+ + + + + {{range $action := .Actions}} + {{if eq $action.Type "ActionTypePlus"}} + + + + + + {{end}} + {{end}} +
ActionTypePlus
{{$action.Name}}{{$action.Magnitude}}{{$action.Repeatable}}
+ + + + + {{range $action := .Actions}} + {{if eq $action.Type "ActionTypeMinus"}} + + + + + + {{end}} + {{end}} +
ActionTypeMinus
{{$action.Name}}{{$action.Magnitude}}{{$action.Repeatable}}
+{{end}} diff --git a/components/index.html b/components/index.html index 7c4cb82..d32154a 100644 --- a/components/index.html +++ b/components/index.html @@ -1,3 +1,4 @@ +{{define "main"}} @@ -12,20 +13,21 @@
HELLO {{.ID}}
- You have {{len .Actions}} actions. -
- Your current score: {{.Score}} -
- {{ range .Actions }} - Action Name: {{.Name}} -
- Action Magnitude: {{.Magnitude}} -
- Action Type: {{.Type}} -
- Repeatable: {{.Repeatable}} -
- {{end}} + + + + + + + + + + + + + + + {{ template "UserScore" . }}
@@ -34,7 +36,7 @@


-
+


@@ -47,3 +49,4 @@
+{{end}} diff --git a/internal/handlers/main.go b/internal/handlers/main.go index 1934250..4abc8e1 100644 --- a/internal/handlers/main.go +++ b/internal/handlers/main.go @@ -43,9 +43,14 @@ func (h *Handlers) Ping(w http.ResponseWriter, r *http.Request) { func (h *Handlers) MainPage(w http.ResponseWriter, r *http.Request) { h.log.Info("got mainpage request") - tmpl := template.Must(template. - ParseFiles("components/index.html")) - tmpl.Execute(w, us) + // tmpl := template.Must(template.ParseFiles("components/index.html")) + tmpl, err := template.ParseGlob("components/*.html") + if err != nil { + panic(err) + } + // tmpl.Execute(w, us) + us.ID = "test" + tmpl.ExecuteTemplate(w, "main", us) } func (h *Handlers) HandleForm(w http.ResponseWriter, r *http.Request) { @@ -53,7 +58,7 @@ func (h *Handlers) HandleForm(w http.ResponseWriter, r *http.Request) { h.log.Info("got postform request", "payload", r.PostForm) magnitude := uint8(1) mS := r.PostFormValue("magnitude") - if mS != "1" || mS != "" { + if mS != "1" || mS != "" || mS != " " { u64, err := strconv.ParseUint(mS, 10, 64) magnitude = uint8(u64) if err != nil { @@ -83,7 +88,8 @@ func (h *Handlers) HandleForm(w http.ResponseWriter, r *http.Request) { Repeatable: repeat, } us.Actions = append(us.Actions, act) - tmpl := template.Must(template. - ParseFiles("components/index.html")) - tmpl.Execute(w, us) + tmpl := template.Must(template.ParseGlob("components/*.html")) + // tmpl := template.Must(template.ParseFiles("components/index.html")) + // tmpl.Execute(w, us) + tmpl.ExecuteTemplate(w, "main", us) } diff --git a/internal/models/models.go b/internal/models/models.go index ee6ae5f..42990a3 100644 --- a/internal/models/models.go +++ b/internal/models/models.go @@ -16,8 +16,6 @@ type ( BurnTime time.Time Score int8 } - - // plus 1 Action struct { Name string Magnitude uint8 -- cgit v1.2.3