working on getting the clientside settings in place

This commit is contained in:
James Tombleson 2024-04-14 18:39:04 -07:00
parent f9c7032e92
commit 6cfc53901e
3 changed files with 54 additions and 5 deletions

View File

@ -2,6 +2,8 @@ package handlers
import ( import (
"net/http" "net/http"
"templ-test/domain"
"templ-test/models"
"templ-test/views/home" "templ-test/views/home"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
@ -12,13 +14,27 @@ func (h *Handlers) HomeHandler(c echo.Context) error {
} }
func (h *Handlers) Settings(c echo.Context) error { func (h *Handlers) Settings(c echo.Context) error {
return Render(c, http.StatusOK, home.UserSettings()) m := models.SettingsViewModel{}
darkMode, err := c.Cookie(domain.CookieSettingsDarkMode)
if err == nil {
m.DarkMode = darkMode.Value
}
return Render(c, http.StatusOK, home.UserSettings(m))
} }
func (h *Handlers) SettingsPost(c echo.Context) error { func (h *Handlers) SettingsPost(c echo.Context) error {
// take in the updated values from he user and write the cookies... tbd // take in the updated values from he user and write the cookies... tbd
useDarkMode := c.FormValue(domain.CookieSettingsDarkMode)
if useDarkMode != "" {
darkModeCookie := new(http.Cookie)
darkModeCookie.Name = domain.CookieSettingsDarkMode
darkModeCookie.Value = useDarkMode
return Render(c, http.StatusOK, home.UserSettings()) c.SetCookie(darkModeCookie)
}
return Render(c, http.StatusOK, home.SettingsUpdated())
} }
//func (h *Handlers) ListHandler(c echo.Context) error { //func (h *Handlers) ListHandler(c echo.Context) error {

View File

@ -1,9 +1,33 @@
package home package home
import "templ-test/views/layout" import "templ-test/views/layout"
import "templ-test/views/components/bootstrap"
import "templ-test/models"
import "templ-test/domain"
templ UserSettings() { const (
@layout.Testing("Settings") { test = "t"
<h2>This is not ready yet</h2> )
templ UserSettings(model models.SettingsViewModel) {
@layout.WithLayout("Settings") {
<form hx-post="/settings">
<div class="mb-3">
if useDarkMode(model.DarkMode) {
@bootstrap.Switch("darkmode", bootstrap.SwitchTypeCheckbox, test, true)
} else {
@bootstrap.Switch("darkmode", bootstrap.SwitchTypeCheckbox, "flexSwitchCheckDefault", true)
}
@bootstrap.Label("form-check-label", domain.CookieSettingsDarkMode, "flexSwitchCheckDefault", "Use Dark Mode")
</div>
@bootstrap.BootstrapButton("Submit", bootstrap.VariantInfo, bootstrap.ButtonTypeSubmit)
</form>
} }
} }
func useDarkMode(value string) bool {
if value == "on" {
return true
}
return false
}

View File

@ -0,0 +1,9 @@
package home
import "templ-test/views/layout"
templ SettingsUpdated() {
@layout.WithLayout("Settings Updated") {
<p>You are now free to move about the cabin. Thank you for saving with us!</p>
}
}