Add update of timezone setting

This commit is contained in:
Magnus Åhall 2024-06-27 10:02:11 +02:00
parent 65c0984348
commit 22f6b6a413
3 changed files with 33 additions and 1 deletions

View File

@ -54,3 +54,9 @@ func (cfg *Configuration) SetTheme(theme string) (err error) {
_, err = service.Db.Conn.Exec(`UPDATE public.configuration SET value=$1 WHERE setting='THEME'`, theme)
return
}
func (cfg *Configuration) SetTimezone(tz string) (err error) {
cfg.Settings["TIMEZONE"] = tz
_, err = service.Db.Conn.Exec(`UPDATE public.configuration SET value=$1 WHERE setting='TIMEZONE'`, tz)
return
}

20
main.go
View File

@ -19,6 +19,7 @@ import (
"io/fs"
"log/slog"
"net/http"
"net/url"
"os"
"path"
"slices"
@ -149,6 +150,7 @@ func main() { // {{{
service.Register("/configuration", false, false, pageConfiguration)
service.Register("/configuration/theme", false, false, actionConfigurationTheme)
service.Register("/configuration/timezone", false, false, actionConfigurationTimezone)
service.Register("/entry/{datapoint}", false, false, actionEntryDatapoint)
go nodataLoop()
@ -1021,3 +1023,21 @@ func actionConfigurationTheme(w http.ResponseWriter, r *http.Request, _ *session
w.Header().Add("Location", "/configuration")
w.WriteHeader(302)
} // }}}
func actionConfigurationTimezone(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
timezone := r.FormValue("timezone")
_, err := time.LoadLocation(timezone)
if err != nil {
pageError(w, "/configuration", werr.Wrap(err).Log())
return
}
err = smonConfig.SetTimezone(timezone)
if err != nil {
pageError(w, "/configuration", werr.Wrap(err).Log())
return
}
w.Header().Add("Location", "/configuration")
w.WriteHeader(302)
} // }}}

View File

@ -95,11 +95,17 @@
</div>
<h1>Theme</h1>
<form action="/configuration/theme" id="theme-set">
<form action="/configuration/theme">
<select name="theme" onchange="console.log(this.form.submit())">
<option value="default_light" {{ if eq "default_light" .CONFIG.THEME }}selected{{ end }}>Default light</option>
<option value="gruvbox" {{ if eq "gruvbox" .CONFIG.THEME }}selected{{ end }}>Gruvbox</option>
</select>
</form>
<h1>Timezone</h1>
<form action="/configuration/timezone" method="post">
<input name="timezone" type="text" value="{{ .CONFIG.TIMEZONE }}">
<button style="margin-left: 8px;">Update</button>
</form>
{{ end }}