Adding info into the templ context to pull username and have the navbar understand it

This commit is contained in:
James Tombleson 2024-05-26 10:00:44 -07:00
parent d5fffa1d66
commit 3796ed4d20
7 changed files with 49 additions and 35 deletions

View File

@ -0,0 +1,5 @@
package domain
type contextKey string
var UserNameContext contextKey = "username"

View File

@ -13,7 +13,7 @@ import (
func (h *Handler) ArticlesList(c echo.Context) error {
_, err := ValidateJwt(c, h.config.JwtSecret, h.config.ServerAddress)
if err != nil {
return Render(c, http.StatusBadRequest, layout.Error(err))
return Render(c, http.StatusOK, layout.Error(err))
}
userToken, err := c.Cookie(domain.CookieToken)

View File

@ -46,4 +46,4 @@ func (h *Handler) UserAfterSignUp(c echo.Context) error {
return Render(c, http.StatusBadRequest, users.AfterLogin(msg, false))
}
return Render(c, http.StatusOK, users.AfterSignUp("Registration Successful!", true))
}
}

View File

@ -1,6 +1,7 @@
package handlers
import (
"context"
"errors"
"net/http"
"time"
@ -68,11 +69,14 @@ func Render(ctx echo.Context, statusCode int, t templ.Component) error {
// 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)
//}
username, err := ctx.Cookie(domain.CookieUser)
if err == nil {
request = context.WithValue(request, domain.UserNameContext, username.Value)
} else {
request = context.WithValue(request, domain.UserNameContext, "")
}
return t.Render(request, ctx.Response().Writer)
}

View File

@ -0,0 +1,6 @@
package models
type LayoutIsLoggedInViewModel struct {
IsLoggedIn bool
Username string
}

View File

@ -19,39 +19,24 @@ templ navBar() {
<div id="navbarBasicExample" class="navbar-menu">
<div class="navbar-start">
<a class="navbar-item" href="/articles">Articles</a>
<a class="navbar-item">Documentation</a>
<!--
<div class="navbar-item has-dropdown is-hoverable">
<a class="navbar-link">
More
</a>
<div class="navbar-dropdown">
<a class="navbar-item">
About
</a>
<a class="navbar-item is-selected">
Jobs
</a>
<a class="navbar-item">
Contact
</a>
<hr class="navbar-divider"/>
<a class="navbar-item">
Report an issue
</a>
</div>
</div>
-->
<a class="navbar-item">{ getUsername(ctx) }</a>
</div>
<div class="navbar-end">
<div class="navbar-item">
<div class="buttons">
<a class="button is-primary" href="/users/signup">
<strong>Sign up</strong>
</a>
<a class="button is-light" href="/users/login">
Log in
</a>
if getUsername(ctx) == "" {
<a class="button is-primary" href="/users/signup"><strong>Sign up</strong></a>
<a class="button is-light" href="/users/login">Log in</a>
} else {
<div class="navbar-item has-dropdown is-hoverable">
<a class="navbar-link">{ getUsername(ctx) }</a>
<div class="navbar-dropdown">
<a class="navbar-item">Profile</a>
<a class="navbar-item">Logout</a>
</div>
</div>
}
</div>
</div>
</div>

View File

@ -0,0 +1,14 @@
package layout
import (
"context"
"git.jamestombleson.com/jtom38/newsbot-portal/internal/domain"
)
func getUsername(ctx context.Context) string {
if theme, ok := ctx.Value(domain.UserNameContext).(string); ok {
return theme
}
return ""
}