Skip to content

Commit

Permalink
Stats pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
aceberg committed Jan 21, 2024
1 parent 0076f52 commit b364891
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 27 deletions.
3 changes: 3 additions & 0 deletions internal/conf/getconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func Get(path string) models.Conf {
viper.SetDefault("THEME", "grass")
viper.SetDefault("COLOR", "light")
viper.SetDefault("HEATCOLOR", "#03a70c")
viper.SetDefault("PAGESTEP", 10)

viper.SetConfigFile(path)
viper.SetConfigType("yaml")
Expand All @@ -29,6 +30,7 @@ func Get(path string) models.Conf {
config.Theme, _ = viper.Get("THEME").(string)
config.Color, _ = viper.Get("COLOR").(string)
config.HeatColor, _ = viper.Get("HEATCOLOR").(string)
config.PageStep = viper.GetInt("PAGESTEP")

return config
}
Expand All @@ -44,6 +46,7 @@ func Write(config models.Conf) {
viper.Set("theme", config.Theme)
viper.Set("color", config.Color)
viper.Set("heatcolor", config.HeatColor)
viper.Set("pagestep", config.PageStep)

err := viper.WriteConfig()
check.IfError(err)
Expand Down
1 change: 1 addition & 0 deletions internal/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Conf struct {
ConfPath string
NodePath string
HeatColor string
PageStep int
}

// Exercise - one exercise
Expand Down
4 changes: 4 additions & 0 deletions internal/web/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package web
import (
"log"
"net/http"
"strconv"

"github.com/gin-gonic/gin"

Expand Down Expand Up @@ -34,6 +35,9 @@ func saveConfigHandler(c *gin.Context) {
appConfig.Theme = c.PostForm("theme")
appConfig.Color = c.PostForm("color")
appConfig.HeatColor = c.PostForm("heatcolor")
pagestep := c.PostForm("pagestep")

appConfig.PageStep, _ = strconv.Atoi(pagestep)

conf.Write(appConfig)

Expand Down
50 changes: 35 additions & 15 deletions internal/web/public/js/stats.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,56 @@
var sChart = null;
var sOffset = 0;

function addSet(date, reps, weight) {
function addSet(i, date, reps, weight) {

html_code = '<tr><td>'+date+'</td><td>'+reps+'</td><td>'+weight+'</td></tr>';
html_code = '<tr><td style="opacity: 45%;">'+i+'.</td><td>'+date+'</td><td>'+reps+'</td><td>'+weight+'</td></tr>';

document.getElementById('stats-table').insertAdjacentHTML('beforeend', html_code);
};


function setStatsPage(sets, hcolor) {
let dates = [], ws = []; reps = [];
function setStatsPage(sets, hcolor, off, step) {
let start = 0, end = 0;
let dates = [], ws = [], reps = [], exs = [];

let ex = document.getElementById("ex-value").value;
console.log("EX =", ex);
for (let i = 0; i < sets.length; i++) {
if (sets[i].Name === ex) {
exs.push(sets[i]);
}
};

let start = 0;
let end = sets.length;
sOffset = sOffset + off;
if (sOffset<0) {
sOffset = 0;
};

let arrayLength = exs.length;
let move = step + sOffset*step;

if (arrayLength > move) {
start = arrayLength - move;
end = start + step;
} else {
sOffset = sOffset - 1;
if (arrayLength > step) {
end = step;
} else {
end = arrayLength;
}
};

document.getElementById('stats-table').innerHTML = "";


for (let i = start ; i < end; i++) {
if (sets[i].Name === ex) {
addSet(sets[i].Date, sets[i].Reps, sets[i].Weight);
addSet(i+1, exs[i].Date, exs[i].Reps, exs[i].Weight);

dates.push(sets[i].Date);
reps.push(sets[i].Reps);
ws.push(sets[i].Weight);
};
dates.push(exs[i].Date);
reps.push(exs[i].Reps);
ws.push(exs[i].Weight);
};

console.log("REPS =", reps);

statsChart('stats-reps', dates, reps, hcolor, true);
weightChart('stats-weight', dates, ws, hcolor, true);
};
Expand Down
4 changes: 2 additions & 2 deletions internal/web/public/js/weight.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ function addWeight(i, date, weight, id) {
document.getElementById('weightList').insertAdjacentHTML('beforeend', html_code);
};

function setWeights(weights, wcolor, off) {
let start = 0, end = 0, step = 10;
function setWeights(weights, wcolor, off, step) {
let start = 0, end = 0;
let dates = [], ws = [];

offset = offset + off;
Expand Down
5 changes: 5 additions & 0 deletions internal/web/templates/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
<input name="heatcolor" value="{{ .Config.HeatColor }}" type="color" class="form-control form-control-color">
</td>
</tr>
<tr>
<td>Page Step</td>
<td><input name="pagestep" type="number" class="form-control" value="{{ .Config.PageStep }}"></td>
</tr>
<tr>
<td><button type="submit" class="btn btn-primary">Save</button></td>
<td></td>
Expand All @@ -62,6 +66,7 @@
<div class="card-header">About</div>
<div class="card-body">
<p>● After changing <b>Host</b> or <b>Port</b> the app must be restarted</p>
<p><b>Page Step</b> - how many items to show on one page (for <a href="/stats/">Stats</a> and <a href="/weight/">Weight</a>)</p>
<p>● Please, check out my other apps on <a href="https://github.com/aceberg" target="_blank">GitHub</a></p>
<p>● Want your own Self-Hosted app written in Go? Contact and prices <a href="https://github.com/aceberg" target="_blank">here</a></p>
</div>
Expand Down
19 changes: 12 additions & 7 deletions internal/web/templates/stats.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
<div class="container-lg">
<br>
<div class="row">
<div class="col-md">
<div class="col-md-4">
<div class="card border-primary">
<div class="card-header">Exercise stats</div>
<div class="card-body">
<div class="chart-container">
<form onchange='setStatsPage({{ .ExData.Sets }}, {{ .Config.HeatColor }});'>
<form onchange='setStatsPage({{ .ExData.Sets }}, {{ .Config.HeatColor }}, 0, {{ .Config.PageStep }});'>
<select class="form-select" id="ex-value">
{{ range $gr := .GroupMap }}
<option value="{{ $gr }}">{{ $gr }}</option>
Expand All @@ -22,18 +22,23 @@
<br>
<table class="table table-borderless">
<thead>
<th class="col-3">Date</th>
<th class="col-4">Reps</th>
<th class="col-4">Weight</th>
<th class="col-1"></th>
<th class="col-4">Date</th>
<th class="col-3">Reps</th>
<th class="col-3">Weight</th>
</thead>
<tbody id="stats-table">
</tbody>
</table>
<div class="gap-3 hstack">
<button onclick='setStatsPage({{ .ExData.Sets }}, {{ .Config.HeatColor }}, 1, {{ .Config.PageStep }});' class="m-auto btn del-set-button"><i class="bi bi-arrow-left-square"></i></button>
<button onclick='setStatsPage({{ .ExData.Sets }}, {{ .Config.HeatColor }}, -1, {{ .Config.PageStep }});' class="m-auto btn del-set-button"><i class="bi bi-arrow-right-square"></i></button>
</div>
</div>
</div>
</div>
</div>
<div class="col-md">
<div class="col-md-8">
<div class="card border-primary">
<div class="card-header">Reps</div>
<div class="card-body">
Expand All @@ -52,7 +57,7 @@
</div>

<script>
setStatsPage({{ .ExData.Sets }}, {{ .Config.HeatColor }});
setStatsPage({{ .ExData.Sets }}, {{ .Config.HeatColor }}, 0, {{ .Config.PageStep }});
</script>

{{ template "footer.html" }}
Expand Down
6 changes: 3 additions & 3 deletions internal/web/templates/weight.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
</tbody>
</table>
<div class="gap-3 hstack">
<button onclick='setWeights({{ .ExData.Weight }}, {{ .Config.HeatColor }}, 1);' class="m-auto btn del-set-button"><i class="bi bi-arrow-left-square"></i></button>
<button onclick='setWeights({{ .ExData.Weight }}, {{ .Config.HeatColor }}, -1);' class="m-auto btn del-set-button"><i class="bi bi-arrow-right-square"></i></button>
<button onclick='setWeights({{ .ExData.Weight }}, {{ .Config.HeatColor }}, 1, {{ .Config.PageStep }});' class="m-auto btn del-set-button"><i class="bi bi-arrow-left-square"></i></button>
<button onclick='setWeights({{ .ExData.Weight }}, {{ .Config.HeatColor }}, -1, {{ .Config.PageStep }});' class="m-auto btn del-set-button"><i class="bi bi-arrow-right-square"></i></button>
</div>
</div>
</div>
Expand Down Expand Up @@ -53,7 +53,7 @@

<script>
setToday();
setWeights({{ .ExData.Weight }}, {{ .Config.HeatColor }}, 0);
setWeights({{ .ExData.Weight }}, {{ .Config.HeatColor }}, 0, {{ .Config.PageStep }});
</script>

{{ template "footer.html" }}
Expand Down

0 comments on commit b364891

Please sign in to comment.