feat(webui): bootstrap account settings management with light and dark mode
This commit is contained in:
parent
ab043d8617
commit
98c7d6f578
13 changed files with 136 additions and 14 deletions
53
pkg/webui/settings.go
Normal file
53
pkg/webui/settings.go
Normal file
|
@ -0,0 +1,53 @@
|
|||
package webui
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
|
||||
"git.adyxax.org/adyxax/tfstated/pkg/database"
|
||||
"git.adyxax.org/adyxax/tfstated/pkg/model"
|
||||
)
|
||||
|
||||
type SettingsPage struct {
|
||||
Page *Page
|
||||
Settings *model.Settings
|
||||
}
|
||||
|
||||
var settingsTemplates = template.Must(template.ParseFS(htmlFS, "html/base.html", "html/settings.html"))
|
||||
|
||||
func handleSettingsGET(db *database.DB) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
settings := r.Context().Value(model.SettingsContextKey{}).(*model.Settings)
|
||||
render(w, settingsTemplates, http.StatusOK, SettingsPage{
|
||||
Page: makePage(r, &Page{Title: "Settings", Section: "settings"}),
|
||||
Settings: settings,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func handleSettingsPOST(db *database.DB) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if err := r.ParseForm(); err != nil {
|
||||
errorResponse(w, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
darkMode := r.FormValue("dark-mode")
|
||||
settings := model.Settings{
|
||||
LightMode: darkMode != "1",
|
||||
}
|
||||
account := r.Context().Value(model.AccountContextKey{}).(*model.Account)
|
||||
err := db.SaveAccountSettings(account, &settings)
|
||||
if err != nil {
|
||||
errorResponse(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
render(w, settingsTemplates, http.StatusOK, SettingsPage{
|
||||
Page: &Page{
|
||||
LightMode: settings.LightMode,
|
||||
Title: "Settings",
|
||||
Section: "settings",
|
||||
},
|
||||
Settings: &settings,
|
||||
})
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue