Skip to content

Commit

Permalink
fixup! templates: render home page using templ
Browse files Browse the repository at this point in the history
Signed-off-by: Sumner Evans <me@sumnerevans.com>
  • Loading branch information
sumnerevans committed May 25, 2024
1 parent f7e22b2 commit 3cd7535
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
36 changes: 26 additions & 10 deletions internal/application.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package internal

import (
"context"
"fmt"
"html/template"
"net/http"
Expand Down Expand Up @@ -93,28 +94,39 @@ type renderInfo struct {
RedirectIfLoggedIn bool
}

type ContextKey string

const (
ContextKeyLoggedInUser ContextKey = "com.mineshspc.logged_in_user"
)

type Info struct {
config config.Configuration
config config.Configuration
pageName string
}

func (*Info) Title() string {
return "Home"
func (i *Info) Title() string {
return i.pageName
}

func (i *Info) HostedByHTML() template.HTML {
return i.config.HostedByHTML
}

func (*Info) PageName() partials.PageName {
return partials.PageNameHome
func (i *Info) PageName() partials.PageName {
return partials.PageName(strings.ToLower(i.pageName))
}

func (i *Info) RegistrationEnabled() bool {
return i.config.RegistrationEnabled
}

func (*Info) Username() string {
return "username"
func (*Info) Username(ctx context.Context) string {
if loggedInUser, ok := ctx.Value(ContextKeyLoggedInUser).(*database.Teacher); ok {
return loggedInUser.Name
} else {
return ""
}
}

func (a *Application) Start() {
Expand All @@ -127,10 +139,14 @@ func (a *Application) Start() {

noArgs := func(r *http.Request) map[string]any { return nil }

base := Info{config: a.Config}

router.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) {
templates.Base(&base, templates.Home(&base)).Render(r.Context(), w)
info := Info{config: a.Config, pageName: "Home"}
ctx := r.Context()
user, err := a.GetLoggedInTeacher(r)
if err == nil {
ctx = context.WithValue(ctx, ContextKeyLoggedInUser, user)
}
templates.Base(&info, templates.Home(&info)).Render(ctx, w)
})

// Static pages
Expand Down
6 changes: 3 additions & 3 deletions internal/templates/partials/navbar.templ
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const (
type NavbarInfo interface {
PageName() PageName
RegistrationEnabled() bool
Username() string
Username(ctx context.Context) string
// TODO make more things configurable?
}

Expand Down Expand Up @@ -106,8 +106,8 @@ templ Navbar(info NavbarInfo) {
</li>
</ul>
<div class="logged-in-user nav-link small text-secondary me-4">
if len(info.Username()) > 0 {
Welcome <a href="/register/teacher/teams">{ info.Username() }</a>
if len(info.Username(ctx)) > 0 {
Welcome <a href="/register/teacher/teams">{ info.Username(ctx) }</a>
<span class="mx-2">|</span>
<a href="/register/teacher/logout">Logout</a>
} else {
Expand Down

0 comments on commit 3cd7535

Please sign in to comment.