summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorGrailFinder <wohilas@gmail.com>2024-04-11 07:13:48 +0300
committerGrailFinder <wohilas@gmail.com>2024-04-11 07:13:48 +0300
commit9b160dcc0315aaf54a8b4588be9d2a0c98084cf0 (patch)
treef1068c6b70612d41a76b947370808cf147e45382 /internal
parent499dcd25aa8d1f878b379e2ca8f94417807c4afb (diff)
Enha: make html elements to react; repeatable separation
Diffstat (limited to 'internal')
-rw-r--r--internal/handlers/main.go40
-rw-r--r--internal/models/models.go14
2 files changed, 40 insertions, 14 deletions
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)
+ }
+}