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) _, err = service.Db.Conn.Exec(`UPDATE public.configuration SET value=$1 WHERE setting='THEME'`, theme)
return 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" "io/fs"
"log/slog" "log/slog"
"net/http" "net/http"
"net/url"
"os" "os"
"path" "path"
"slices" "slices"
@ -149,6 +150,7 @@ func main() { // {{{
service.Register("/configuration", false, false, pageConfiguration) service.Register("/configuration", false, false, pageConfiguration)
service.Register("/configuration/theme", false, false, actionConfigurationTheme) service.Register("/configuration/theme", false, false, actionConfigurationTheme)
service.Register("/configuration/timezone", false, false, actionConfigurationTimezone)
service.Register("/entry/{datapoint}", false, false, actionEntryDatapoint) service.Register("/entry/{datapoint}", false, false, actionEntryDatapoint)
go nodataLoop() go nodataLoop()
@ -1021,3 +1023,21 @@ func actionConfigurationTheme(w http.ResponseWriter, r *http.Request, _ *session
w.Header().Add("Location", "/configuration") w.Header().Add("Location", "/configuration")
w.WriteHeader(302) 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> </div>
<h1>Theme</h1> <h1>Theme</h1>
<form action="/configuration/theme" id="theme-set"> <form action="/configuration/theme">
<select name="theme" onchange="console.log(this.form.submit())"> <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="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> <option value="gruvbox" {{ if eq "gruvbox" .CONFIG.THEME }}selected{{ end }}>Gruvbox</option>
</select> </select>
</form> </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 }} {{ end }}