From 9b160dcc0315aaf54a8b4588be9d2a0c98084cf0 Mon Sep 17 00:00:00 2001 From: GrailFinder Date: Thu, 11 Apr 2024 07:13:48 +0300 Subject: Enha: make html elements to react; repeatable separation --- components/actions_table.html | 5 ++--- components/add_action_form.html | 4 ++-- internal/handlers/main.go | 40 +++++++++++++++++++++++++++------------- internal/models/models.go | 14 +++++++++++++- 4 files changed, 44 insertions(+), 19 deletions(-) diff --git a/components/actions_table.html b/components/actions_table.html index 8b151e5..24a4f9c 100644 --- a/components/actions_table.html +++ b/components/actions_table.html @@ -22,7 +22,7 @@ {{range $action := .Actions}} {{if and (eq $action.Type "ActionTypePlus") (or (not $action.Done) ($action.Repeatable))}} -
+ {{$action.Name}} {{$action.Magnitude}} @@ -45,10 +45,9 @@ {{range $action := .Actions}} {{if eq $action.Type "ActionTypeMinus"}} - + {{$action.Name}} - {{$action.Name}} {{$action.Magnitude}} {{$action.Repeatable}} diff --git a/components/add_action_form.html b/components/add_action_form.html index 95c869f..ac65ca0 100644 --- a/components/add_action_form.html +++ b/components/add_action_form.html @@ -7,8 +7,8 @@


-
-
+ + diff --git a/internal/handlers/main.go b/internal/handlers/main.go index efafcde..1f2276c 100644 --- a/internal/handlers/main.go +++ b/internal/handlers/main.go @@ -38,7 +38,10 @@ func NewHandlers( } // FIXME: global userscore for test -var us models.UserScore +var us = models.UserScore{ + Username: "test", + BurnTime: time.Now().Add(time.Duration(24) * time.Hour), +} func (h *Handlers) Ping(w http.ResponseWriter, r *http.Request) { h.log.Info("got ping request") @@ -53,8 +56,8 @@ func (h *Handlers) MainPage(w http.ResponseWriter, r *http.Request) { panic(err) } // tmpl.Execute(w, us) - us.Username = "test" - us.BurnTime = time.Now().Add(time.Duration(24) * time.Hour) + // us.Username = "test" + // us.BurnTime = time.Now().Add(time.Duration(24) * time.Hour) tmpl.ExecuteTemplate(w, "main", us) } @@ -73,19 +76,20 @@ func (h *Handlers) HandleForm(w http.ResponseWriter, r *http.Request) { magnitude = uint8(1) } } + repeat := false var at models.ActionType switch r.PostFormValue("act_type") { case "plus": at = models.ActionTypePlus case "minus": at = models.ActionTypeMinus + repeat = true default: h.log.Warn("uknown actiontype", "type", r.PostFormValue("act_type")) } - repeat := false - if r.PostFormValue("repeatable") == "on" { - repeat = true - } + // if r.PostFormValue("repeatable") == "on" { + // repeat = true + // } // convert map to action object act := models.Action{ Name: r.PostFormValue("name"), @@ -94,12 +98,14 @@ func (h *Handlers) HandleForm(w http.ResponseWriter, r *http.Request) { Repeatable: repeat, } // TODO: check that name + userid key is unique - us.Actions = append(us.Actions, act) - tmpl := template.Must(template.ParseGlob("components/*.html")) - // tmpl := template.Must(template.ParseFiles("components/index.html")) - // tmpl.Execute(w, us) + us.Actions = append(us.Actions, &act) // TODO: redirect to the main page instead - tmpl.ExecuteTemplate(w, "main", us) + http.Redirect(w, r, "/", 302) + + // tmpl := template.Must(template.ParseGlob("components/*.html")) + // // tmpl := template.Must(template.ParseFiles("components/index.html")) + // // tmpl.Execute(w, us) + // tmpl.ExecuteTemplate(w, "main", us) } func (h *Handlers) HandleDoneAction(w http.ResponseWriter, r *http.Request) { @@ -109,6 +115,14 @@ func (h *Handlers) HandleDoneAction(w http.ResponseWriter, r *http.Request) { h.log.Info("got postform request", "name", actionName) // change counter of user score // get action by name + for _, act := range us.Actions { + if act.Name != actionName { + continue + } + us.UpdateScore(act) + h.log.Info("show action", "act", act, "us", us) + } tmpl := template.Must(template.ParseGlob("components/*.html")) - tmpl.ExecuteTemplate(w, "main", nil) + tmpl.ExecuteTemplate(w, "main", us) + // http.Redirect(w, r, "/", 302) } diff --git a/internal/models/models.go b/internal/models/models.go index 58191a2..a2b3628 100644 --- a/internal/models/models.go +++ b/internal/models/models.go @@ -12,7 +12,7 @@ const ( type ( UserScore struct { Username string - Actions []Action + Actions []*Action BurnTime time.Time Score int8 } @@ -26,3 +26,15 @@ type ( Username string } ) + +func (us *UserScore) UpdateScore(act *Action) { + switch act.Type { + case ActionTypePlus: + us.Score += int8(act.Magnitude) + if !act.Repeatable { + act.Done = true + } + case ActionTypeMinus: + us.Score -= int8(act.Magnitude) + } +} -- cgit v1.2.3