diff --git a/.gitignore b/.gitignore index 124cb1f..7c67e3e 100644 --- a/.gitignore +++ b/.gitignore @@ -25,4 +25,5 @@ hibiscus-txt # Testing data data/ -config/log.txt \ No newline at end of file +config/log.txt +config/dev-config.txt \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 7221196..edc7c7a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,16 @@ # Changelog This file keeps track of changes in more human-readable fashion -# 5 May 2024 +## 6 May 2024 +* Grace period is now non-inclusive (so `4h` means the switch will happen right at `4:00`, not `4:01`) +* Added API method to check if grace period is active +* Made changes to date display on frontend + * The date is now updated every minute, instead of every hour. + * Now using the API to dynamically update the grace indicator + * I kinda dislike this change, since it complicates the structure a *bit*. + But I think it's fine. + +## 5 May 2024 * Added this changelog * Added grace period (as per suggestions) * Set in config like `grace_period=3h26m` (via Go's `time.ParseDuration`) diff --git a/README.md b/README.md index 3afbbc2..cd4a154 100644 --- a/README.md +++ b/README.md @@ -96,4 +96,6 @@ GET /export - get .zip archive of entire data directory GET /readme - get file contents for readme.txt in data dir's root POST /readme - save request body into readme.txt + +GET /grace - "true" if grace period is active, otherwise "false" ``` \ No newline at end of file diff --git a/api.go b/api.go index aee91d4..072833f 100644 --- a/api.go +++ b/api.go @@ -106,3 +106,13 @@ func PostNoteApi(w http.ResponseWriter, r *http.Request) { } PostFile("notes/"+noteString, w, r) } + +// GraceActiveApi returns "true" if grace period is active, and "false" otherwise +func GraceActiveApi(w http.ResponseWriter, r *http.Request) { + value := "false" + if GraceActive() { + value = "true" + } + HandleWrite(w.Write([]byte(value))) + w.WriteHeader(http.StatusOK) +} diff --git a/files.go b/files.go index 8e45870..f2868d8 100644 --- a/files.go +++ b/files.go @@ -77,16 +77,20 @@ func ListFiles(directory string) ([]string, error) { // GraceActive returns whether the grace period (Cfg.GraceTime) is active func GraceActive() bool { - return (60*time.Now().In(Cfg.Timezone).Hour() + time.Now().In(Cfg.Timezone).Minute()) <= int(Cfg.GraceTime.Minutes()) + t := time.Now().In(Cfg.Timezone) + active := (60*t.Hour() + t.Minute()) < int(Cfg.GraceTime.Minutes()) + if active { + slog.Debug("grace period active", + "time", 60*t.Hour()+t.Minute(), + "grace", Cfg.GraceTime.Minutes()) + } + return active } // TodayDate returns today's formatted date. It accounts for Config.GraceTime func TodayDate() string { dateFormatted := time.Now().In(Cfg.Timezone).Format(time.DateOnly) if GraceActive() { - slog.Debug("grace period active", - "time", 60*time.Now().In(Cfg.Timezone).Hour()+time.Now().In(Cfg.Timezone).Minute(), - "grace", Cfg.GraceTime.Minutes()) dateFormatted = time.Now().In(Cfg.Timezone).AddDate(0, 0, -1).Format(time.DateOnly) } return dateFormatted diff --git a/pages/base.html b/pages/base.html index 2f799d0..69e98da 100644 --- a/pages/base.html +++ b/pages/base.html @@ -25,8 +25,7 @@ {{template "footer" .}} diff --git a/public/date.js b/public/date.js index 931b270..de59770 100644 --- a/public/date.js +++ b/public/date.js @@ -8,29 +8,27 @@ function formatDate(date) { return dateFormat.format(date) } -// Set today's date -function updateDate() { - let timeField = document.getElementById("today-date") - if (graceActive) { - let graceField = document.getElementById("grace") - graceField.hidden = false - } - timeField.innerText = formatDate(Date.now()) - +async function graceActive() { + const response = await fetch("/api/grace"); + const active = await response.text(); + return active === "true" } -// Start interval to update today's date every hour at 00:00 -function callEveryHour() { - setInterval(updateDate, 1000 * 60 * 60); +// Set today's date and grace status +function updateTime() { + document.getElementById("today-date").innerText = formatDate(Date.now()); + graceActive().then(v => document.getElementById("grace").hidden = !v) +} + +// Start interval to update time at start of every minute +function callEveryMinute() { + setInterval(updateTime, 1000 * 60); } // Begin above interval -function beginDateUpdater() { - let nextDate = new Date(); - nextDate.setHours(nextDate.getHours() + 1); - nextDate.setMinutes(0); - nextDate.setSeconds(0); - - const difference = nextDate - new Date(); - setTimeout(callEveryHour, difference); +function beginTimeUpdater() { + const difference = (60 - new Date().getSeconds()) * 1000; + setTimeout(callEveryMinute, difference); + setTimeout(updateTime, difference); + updateTime(); } \ No newline at end of file diff --git a/routes.go b/routes.go index 48404d2..db6a875 100644 --- a/routes.go +++ b/routes.go @@ -25,7 +25,7 @@ type Entry struct { type formatEntries func([]string) []Entry -var templateFuncs = map[string]interface{}{"translatableText": TranslatableText, "graceActive": GraceActive} +var templateFuncs = map[string]interface{}{"translatableText": TranslatableText} var editTemplate = template.Must(template.New("").Funcs(templateFuncs).ParseFiles("./pages/base.html", "./pages/edit.html")) var viewTemplate = template.Must(template.New("").Funcs(templateFuncs).ParseFiles("./pages/base.html", "./pages/entry.html")) var listTemplate = template.Must(template.New("").Funcs(templateFuncs).ParseFiles("./pages/base.html", "./pages/list.html")) diff --git a/serve.go b/serve.go index e2a158f..7e38dd9 100644 --- a/serve.go +++ b/serve.go @@ -40,6 +40,7 @@ func Serve() { apiRouter.Get("/today", GetTodayApi) apiRouter.Post("/today", PostTodayApi) apiRouter.Get("/export", GetExport) + apiRouter.Get("/grace", GraceActiveApi) r.Mount("/api", apiRouter) // Static files