summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--components/actions_table.html5
-rw-r--r--components/add_action_form.html4
-rw-r--r--internal/handlers/main.go40
-rw-r--r--internal/models/models.go14
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 @@
</tr>
{{range $action := .Actions}}
{{if and (eq $action.Type "ActionTypePlus") (or (not $action.Done) ($action.Repeatable))}}
- <form hx-post="/done">
+ <form hx-post="/done" hx-target="#ancestor" hx-swap="outerHTML">
<tr>
<td><input class="action_name" name="name" value={{$action.Name}} type="hidden" readonly />{{$action.Name}}</td>
<td>{{$action.Magnitude}}</td>
@@ -45,10 +45,9 @@
</tr>
{{range $action := .Actions}}
{{if eq $action.Type "ActionTypeMinus"}}
- <form hx-post="/done">
+ <form hx-post="/done" hx-target="#ancestor" hx-swap="outerHTML">
<tr>
<td><input class="action_name" name="name" value={{$action.Name}} type="hidden" readonly />{{$action.Name}}</td>
- <td>{{$action.Name}}</td>
<td>{{$action.Magnitude}}</td>
<td>{{$action.Repeatable}}</td>
<td><input type="submit" value="Done It"></input></td>
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 @@
<input type="text" name="name"><br />
<label>Magnitude:</label><br />
<input type="number" value="1" name="magnitude"><br />
- <label>Repeatable:</label><br />
- <input type="checkbox" name="repeatable"></input><br />
+ <!-- <label>Repeatable:</label><br /> -->
+ <!-- <input type="checkbox" name="repeatable"></input><br /> -->
<input type="radio" name="act_type" value="plus" checked>
<label for="plus">Plus</label>
<input type="radio" name="act_type" value="minus">
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)
+ }
+}