diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..b1258dc --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,15 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Launch file", + "type": "go", + "request": "launch", + "mode": "debug", + "program": "cmd/main.go" + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 1d914e3..276f6bf 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,13 @@ { + "editor.formatOnSave": true, + "[templ]": { + "editor.defaultFormatter": "a-h.templ" + }, "files.exclude": { "**/*_templ.go": true, "**/*_templ.txt": true + }, + "emmet.includeLanguages": { + "templ": "html" } } \ No newline at end of file diff --git a/domain/consts.go b/domain/consts.go new file mode 100644 index 0000000..9e67900 --- /dev/null +++ b/domain/consts.go @@ -0,0 +1,9 @@ +package domain + +const ( + CookieToken = "token" + CookieRefreshToken = "refresh" + CookieUser = "user" + + CookieSettingsDarkMode = "darkmode" +) diff --git a/handlers/auth.go b/handlers/auth.go index 5b152a2..29692c8 100644 --- a/handlers/auth.go +++ b/handlers/auth.go @@ -3,6 +3,7 @@ package handlers import ( "log" "net/http" + "templ-test/domain" "templ-test/views/auth" "templ-test/views/home" @@ -25,22 +26,22 @@ func (h *Handlers) AuthLoginPost(c echo.Context) error { } cookie := new(http.Cookie) - cookie.Name = CookieToken + cookie.Name = domain.CookieToken cookie.Value = resp.Token c.SetCookie(cookie) cookie = new(http.Cookie) - cookie.Name = CookieRefreshToken + cookie.Name = domain.CookieRefreshToken cookie.Value = resp.RefreshToken c.SetCookie(cookie) cookie = new(http.Cookie) - cookie.Name = CookieUser + cookie.Name = domain.CookieUser cookie.Value = user c.SetCookie(cookie) // render - return Render(c, http.StatusOK, home.Home()) + return Render(c, http.StatusOK, auth.LoginPost()) } func (h *Handlers) AuthShowCookies(c echo.Context) error { diff --git a/handlers/handler.go b/handlers/handler.go index 4cbfa70..315852b 100644 --- a/handlers/handler.go +++ b/handlers/handler.go @@ -1,8 +1,10 @@ package handlers import ( + "context" "errors" "templ-test/client" + "templ-test/domain" "templ-test/models" "templ-test/services" "time" @@ -13,12 +15,6 @@ import ( "github.com/labstack/echo/v4" ) -const ( - CookieToken = "token" - CookieRefreshToken = "refresh" - CookieUser = "user" -) - type Handlers struct { api client.ApiClient cfg services.EnvConfig @@ -48,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 { @@ -76,7 +81,7 @@ func ValidateJwt(ctx echo.Context, sharedSecret, issuer string) (jwtToken, error if !token.Valid { return jwtToken{}, errors.New("invalid jwt token") } - + claims := token.Claims.(*jwtToken) if !claims.Exp.After(time.Now()) { return jwtToken{}, errors.New("the jwt token has expired") @@ -91,20 +96,25 @@ func ValidateJwt(ctx echo.Context, sharedSecret, issuer string) (jwtToken, error func GetCookieValues(ctx echo.Context) models.AllCookies { m := models.AllCookies{} - token, err := ctx.Cookie(CookieToken) + token, err := ctx.Cookie(domain.CookieToken) if err == nil { m.Token = token.Value } - user, err := ctx.Cookie(CookieUser) + user, err := ctx.Cookie(domain.CookieUser) if err == nil { m.Username = user.Value } - refresh, err := ctx.Cookie(CookieRefreshToken) + refresh, err := ctx.Cookie(domain.CookieRefreshToken) if err == nil { m.RefreshToken = refresh.Value } + darkMode, err := ctx.Cookie(domain.CookieSettingsDarkMode) + if err == nil { + m.DarkMode = darkMode.Value + } + return m } diff --git a/handlers/home.go b/handlers/home.go index d35c8df..b358034 100644 --- a/handlers/home.go +++ b/handlers/home.go @@ -2,6 +2,8 @@ package handlers import ( "net/http" + "templ-test/domain" + "templ-test/models" "templ-test/views/home" "github.com/labstack/echo/v4" @@ -12,15 +14,25 @@ func (h *Handlers) HomeHandler(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 { // 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 { -// return Render(c, http.StatusOK, views.List()) -//} diff --git a/models/auth.go b/models/auth.go index af18023..050559c 100644 --- a/models/auth.go +++ b/models/auth.go @@ -4,6 +4,7 @@ type AllCookies struct { Username string Token string RefreshToken string + DarkMode string } type ShowCookie struct { diff --git a/models/home.go b/models/home.go new file mode 100644 index 0000000..ac1d1d3 --- /dev/null +++ b/models/home.go @@ -0,0 +1,5 @@ +package models + +type SettingsViewModel struct { + DarkMode string +} diff --git a/views/auth/cookie.templ b/views/auth/cookie.templ index a9e8ada..64e92dc 100644 --- a/views/auth/cookie.templ +++ b/views/auth/cookie.templ @@ -4,10 +4,13 @@ import "templ-test/models" import "templ-test/views/layout" templ ShowCookie(m models.AllCookies) { - @layout.Testing("Cookie Explorer") { + @layout.WithLayout("Cookie Explorer") {

These are stored as cookies

+

JWT Values

Username: { m.Username }

JWT Token: { m.Token }

RefreshToken: { m.RefreshToken }

+

User Settings

+

DarkMode: { m.DarkMode }

} } diff --git a/views/auth/login.templ b/views/auth/login.templ index 6dc0886..b617169 100644 --- a/views/auth/login.templ +++ b/views/auth/login.templ @@ -3,7 +3,7 @@ package auth import "templ-test/views/layout" templ AuthLogin() { - @layout.WithLayout("Login", true) { + @layout.WithLayout("Login") {
diff --git a/views/auth/loginPost.templ b/views/auth/loginPost.templ new file mode 100644 index 0000000..d1c7556 --- /dev/null +++ b/views/auth/loginPost.templ @@ -0,0 +1,7 @@ +package auth + +import "templ-test/views/components/bootstrap" + +templ LoginPost() { + @bootstrap.BootstrapAlert("Login successful!", bootstrap.VariantSuccess) +} diff --git a/views/components/bootstrap/bs_card.templ b/views/components/bootstrap/alert.templ similarity index 75% rename from views/components/bootstrap/bs_card.templ rename to views/components/bootstrap/alert.templ index 6f4073b..c2be6c8 100644 --- a/views/components/bootstrap/bs_card.templ +++ b/views/components/bootstrap/alert.templ @@ -17,7 +17,3 @@ templ BootstrapAlert(message, variant string) { { message }
} - -templ BootstrapButton(message, variant string) { - -} diff --git a/views/components/bootstrap/button.templ b/views/components/bootstrap/button.templ new file mode 100644 index 0000000..fc85b61 --- /dev/null +++ b/views/components/bootstrap/button.templ @@ -0,0 +1,10 @@ +package bootstrap + +const ( + ButtonTypeSubmit = "submit" + ButtonTypeDefault = "button" +) + +templ BootstrapButton(message, variant, buttonType string) { + +} diff --git a/views/components/bootstrap/label.templ b/views/components/bootstrap/label.templ new file mode 100644 index 0000000..9786fe9 --- /dev/null +++ b/views/components/bootstrap/label.templ @@ -0,0 +1,5 @@ +package bootstrap + +templ Label(cssClass, name, cssForId, message string) { + +} \ No newline at end of file diff --git a/views/components/bootstrap/switch.templ b/views/components/bootstrap/switch.templ new file mode 100644 index 0000000..e0635f3 --- /dev/null +++ b/views/components/bootstrap/switch.templ @@ -0,0 +1,13 @@ +package bootstrap + +const ( + SwitchTypeCheckbox = "checkbox" +) + +templ Switch(name, switchType, cssId string, enabled bool) { + if enabled == true { + + } else { + + } +} diff --git a/views/home/error.templ b/views/home/error.templ index 7ca9230..f92a3b9 100644 --- a/views/home/error.templ +++ b/views/home/error.templ @@ -3,7 +3,7 @@ package home import "templ-test/views/layout" templ Error(message error) { - @layout.Testing("Error") { + @layout.WithLayout("Error") {

Oops... :(

{ message.Error() }

} diff --git a/views/home/home.templ b/views/home/home.templ index 0a1add7..e6e9f74 100644 --- a/views/home/home.templ +++ b/views/home/home.templ @@ -4,14 +4,14 @@ import "templ-test/views/components/bootstrap" import "templ-test/views/layout" templ Home() { - @layout.WithLayout("Home", true) { + @layout.WithLayout("Home") {

this should be above the alert

@bootstrap.BootstrapAlert("Testing!", bootstrap.VariantDark)

you should now see this under the Alert

- @bootstrap.BootstrapButton("I am in danger", bootstrap.VariantDanger) - @bootstrap.BootstrapButton("I am the darkness", bootstrap.VariantDark) + @bootstrap.BootstrapButton("I am in danger", bootstrap.VariantDanger, bootstrap.ButtonTypeDefault) + @bootstrap.BootstrapButton("I am the darkness", bootstrap.VariantDark, bootstrap.ButtonTypeDefault) } } diff --git a/views/home/settings.templ b/views/home/settings.templ index 30da693..2c50972 100644 --- a/views/home/settings.templ +++ b/views/home/settings.templ @@ -1,9 +1,33 @@ package home import "templ-test/views/layout" +import "templ-test/views/components/bootstrap" +import "templ-test/models" +import "templ-test/domain" -templ UserSettings() { - @layout.Testing("Settings") { -

This is not ready yet

+const ( + test = "t" +) + +templ UserSettings(model models.SettingsViewModel) { + @layout.WithLayout("Settings") { + +
+ 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") +
+ @bootstrap.BootstrapButton("Submit", bootstrap.VariantInfo, bootstrap.ButtonTypeSubmit) +
} } + +func useDarkMode(value string) bool { + if value == "on" { + return true + } + return false +} \ No newline at end of file diff --git a/views/home/settingsUpdated.templ b/views/home/settingsUpdated.templ new file mode 100644 index 0000000..69b1eb3 --- /dev/null +++ b/views/home/settingsUpdated.templ @@ -0,0 +1,5 @@ +package home + +templ SettingsUpdated() { +

You are now free to move about the cabin. Thank you for saving with us!

+} diff --git a/views/layout/body.templ b/views/layout/body.templ index a626b65..e1af390 100644 --- a/views/layout/body.templ +++ b/views/layout/body.templ @@ -1,20 +1,8 @@ package layout -templ WithLayout(pageName string, useDarkMode bool) { - - @getHtmlHead() - - @bootstrapNavBar() - @getBodyHeader(pageName) -
- { children... } -
- - -} - -templ Testing(pageName string) { - +templ WithLayout(pageName string) { + + @getHtmlHead() @bootstrapNavBar() diff --git a/views/layout/navbar.templ b/views/layout/navbar.templ index 9101e8b..131d6ca 100644 --- a/views/layout/navbar.templ +++ b/views/layout/navbar.templ @@ -1,7 +1,7 @@ package layout templ bootstrapNavBar() { -