features/client-side-settings #3

Merged
jtom38 merged 12 commits from features/client-side-settings into main 2024-04-14 19:40:30 -07:00
4 changed files with 21 additions and 18 deletions
Showing only changes of commit a110f2de73 - Show all commits

View File

@ -1,6 +1,7 @@
package handlers
import (
"context"
"errors"
"templ-test/client"
"templ-test/domain"
@ -43,7 +44,16 @@ func (h *Handlers) Register(group echo.Group) {
func Render(ctx echo.Context, statusCode int, t templ.Component) error {
ctx.Response().Writer.WriteHeader(statusCode)
ctx.Response().Header().Set(echo.HeaderContentType, echo.MIMETextHTML)
return t.Render(ctx.Request().Context(), ctx.Response().Writer)
// take the request context and make it a var
request := ctx.Request().Context()
//Check to see if we the echo context has the cookie we are looking for, if so, create a new context based on what we had and add the value
darkMode, err := ctx.Cookie(domain.CookieSettingsDarkMode)
if err == nil {
request = context.WithValue(request, domain.CookieSettingsDarkMode, darkMode.Value)
}
return t.Render(request, ctx.Response().Writer)
}
type jwtToken struct {
@ -101,5 +111,10 @@ func GetCookieValues(ctx echo.Context) models.AllCookies {
m.RefreshToken = refresh.Value
}
darkMode, err := ctx.Cookie(domain.CookieSettingsDarkMode)
if err == nil {
m.DarkMode = darkMode.Value
}
return m
}

View File

@ -1,20 +1,8 @@
package layout
templ WithLayout(pageName string) {
<html>
@getHtmlHead()
<body>
@bootstrapNavBar()
@getBodyHeader(pageName)
<div class="container-fluid">
{ children... }
</div>
</body>
</html>
}
templ Testing(pageName string) {
<html>
<!doctype html>
<html lang="en" data-bs-theme={ useLightOrDarkTheme(ctx) }>
@getHtmlHead()
<body>
@bootstrapNavBar()

View File

@ -1,7 +1,7 @@
package layout
templ bootstrapNavBar() {
<nav class="navbar navbar-expand-lg bg-body-tertiary" data-bs-theme={ useLightOrDarkTheme(ctx)}>
<nav class="navbar navbar-expand-lg bg-body-tertiary">
<div class="container-fluid">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">

View File

@ -6,8 +6,8 @@ import (
)
func useLightOrDarkTheme(ctx context.Context) string {
cookie := ctx.Value(domain.CookieSettingsDarkMode)
if cookie == "on" {
value := ctx.Value(domain.CookieSettingsDarkMode)
if value == "on" {
return "dark"
} else {
return "light"