From 9d02dc16a8a0fc420fad36ed4c61bfcc3ea1518c Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Sat, 10 Aug 2024 01:28:11 +0200 Subject: Do setting and theme toggling on the frontend --- main.go | 35 +---- middleware/pipe.go | 14 -- middleware/theme.go | 42 ------ templates/base.templ | 29 ++++- templates/base_templ.go | 31 ++--- templates/navbar.templ | 322 +++++++++++++++++++++++----------------------- templates/navbar_templ.go | 39 +----- 7 files changed, 203 insertions(+), 309 deletions(-) delete mode 100644 middleware/pipe.go delete mode 100644 middleware/theme.go diff --git a/main.go b/main.go index 77d301c..e0c73e6 100644 --- a/main.go +++ b/main.go @@ -38,12 +38,8 @@ func main() { mux.Handle("GET /favicon.ico", fs) mux.Handle("GET /fonts/", fs) mux.Handle("GET /style.css", fs) - mux.Handle("GET /", middleware.Pipe( - middleware.Theme, - middleware.I18n, - )(http.HandlerFunc(finalHandler))) + mux.Handle("GET /", middleware.I18n(http.HandlerFunc(finalHandler))) mux.Handle("POST /language", http.HandlerFunc(setUserLanguage)) - mux.Handle("POST /theme", http.HandlerFunc(setUserTheme)) portStr := ":" + strconv.Itoa(*port) log.Println("Listening on", portStr) @@ -101,32 +97,3 @@ func setUserLanguage(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, c.Value, http.StatusFound) } } - -func setUserTheme(w http.ResponseWriter, r *http.Request) { - c, err := r.Cookie("theme") - if errors.Is(err, http.ErrNoCookie) { - w.WriteHeader(http.StatusBadRequest) - fmt.Fprintf(w, "No ‘theme’ cookie exists") - return - } - - var theme string - - switch c.Value { - case "dark": - theme = "light" - case "light": - theme = "dark" - default: - w.WriteHeader(http.StatusBadRequest) - fmt.Fprintf(w, "Theme ‘%s’ is invalid", c.Value) - return - } - - http.SetCookie(w, &http.Cookie{ - Name: "theme", - Value: theme, - MaxAge: math.MaxInt32, - }) - http.Redirect(w, r, r.Referer(), http.StatusFound) -} diff --git a/middleware/pipe.go b/middleware/pipe.go deleted file mode 100644 index 4b5064d..0000000 --- a/middleware/pipe.go +++ /dev/null @@ -1,14 +0,0 @@ -package middleware - -import "net/http" - -type Middleware func(http.Handler) http.Handler - -func Pipe(xs ...Middleware) Middleware { - return func(next http.Handler) http.Handler { - for i := len(xs) - 1; i >= 0; i-- { - next = xs[i](next) - } - return next - } -} diff --git a/middleware/theme.go b/middleware/theme.go deleted file mode 100644 index 4f14c97..0000000 --- a/middleware/theme.go +++ /dev/null @@ -1,42 +0,0 @@ -package middleware - -import ( - "cmp" - "context" - "math" - "net/http" -) - -const defaultTheme = "dark" - -func Theme(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - var userTheme string - - /* Grab the ‘theme’ cookie to figure out what the users current - theme is and add it to the context. If the user doesn’t yet - have a theme cookie or the cookie they have contains an - invalid theme then we fallback to the default theme and - (re)set the cookie. */ - - c, err := r.Cookie("theme") - if err == nil { - switch c.Value { - case "dark", "light": - userTheme = c.Value - } - } - - theme := cmp.Or(userTheme, defaultTheme) - if userTheme == "" { - http.SetCookie(w, &http.Cookie{ - Name: "theme", - Value: theme, - MaxAge: math.MaxInt32, - }) - } - - ctx := context.WithValue(r.Context(), "theme", theme) - next.ServeHTTP(w, r.WithContext(ctx)) - }) -} diff --git a/templates/base.templ b/templates/base.templ index 88a9e77..e6c4a1d 100644 --- a/templates/base.templ +++ b/templates/base.templ @@ -5,12 +5,39 @@ import "git.thomasvoss.com/euro-cash.eu/i18n" templ Base(head, body templ.Component) { {{ p := ctx.Value("printer").(i18n.Printer) }} - + Euro Cash + if head != nil { @head } diff --git a/templates/base_templ.go b/templates/base_templ.go index 2bc2034..399b577 100644 --- a/templates/base_templ.go +++ b/templates/base_templ.go @@ -42,20 +42,7 @@ func Base(head, body templ.Component) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\" data-theme=\"") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - var templ_7745c5c3_Var3 string - templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(ctx.Value("theme").(string)) - if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `base.templ`, Line: 8, Col: 69} - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3)) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\">Euro Cash") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\">Euro Cash") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -79,12 +66,12 @@ func Base(head, body templ.Component) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - var templ_7745c5c3_Var4 string - templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(p.T("Found a mistake or want to contribute missing information?")) + var templ_7745c5c3_Var3 string + templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(p.T("Found a mistake or want to contribute missing information?")) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `base.templ`, Line: 25, Col: 73} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `base.templ`, Line: 52, Col: 73} } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4)) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3)) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -92,12 +79,12 @@ func Base(head, body templ.Component) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - var templ_7745c5c3_Var5 string - templ_7745c5c3_Var5, templ_7745c5c3_Err = templ.JoinStringErrs(p.T("Feel free to contact us!")) + var templ_7745c5c3_Var4 string + templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(p.T("Feel free to contact us!")) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `base.templ`, Line: 26, Col: 51} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `base.templ`, Line: 53, Col: 51} } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var5)) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4)) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } diff --git a/templates/navbar.templ b/templates/navbar.templ index dd34d4b..f0b030c 100644 --- a/templates/navbar.templ +++ b/templates/navbar.templ @@ -67,164 +67,170 @@ templ navbar() { diff --git a/templates/navbar_templ.go b/templates/navbar_templ.go index 3674775..96aea3a 100644 --- a/templates/navbar_templ.go +++ b/templates/navbar_templ.go @@ -146,44 +146,7 @@ func navbar() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
  • ") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - var templ_7745c5c3_Var11 = []any{noMargin()} - templ_7745c5c3_Err = templ.RenderCSSItems(ctx, templ_7745c5c3_Buffer, templ_7745c5c3_Var11...) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
  • ") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
  • ") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } -- cgit v1.2.3