Added global page errors

This commit is contained in:
Atridad Lahiji 2024-07-09 11:59:06 -06:00
parent c171b417d8
commit c3ddb393bf
No known key found for this signature in database
10 changed files with 59 additions and 17 deletions

View file

@ -10,12 +10,18 @@ import (
"github.com/labstack/echo/v4"
)
type Error struct {
Code int
Message string
}
type TemplateData struct {
Props interface{}
IsLoggedIn bool
Error Error
}
func RenderTemplate(c echo.Context, layout string, partials []string, props interface{}) error {
func RenderTemplate(c echo.Context, layout string, partials []string, props interface{}, error Error) error {
// Get the name of the current file
_, filename, _, _ := runtime.Caller(1)
page := filepath.Base(filename)
@ -41,6 +47,7 @@ func RenderTemplate(c echo.Context, layout string, partials []string, props inte
isLoggedIn := IsSignedIn(c)
templateData := TemplateData{
Props: props,
Error: error,
IsLoggedIn: isLoggedIn,
}

View file

@ -11,6 +11,9 @@ type DashboardProps struct {
}
func Dashboard(c echo.Context) error {
// Initialize the page error
pageError := lib.Error{}
currentSession, error := lib.GetSessionCookie(c.Request(), "session")
if error != nil {
lib.LogError.Printf("Error getting session: %v", error)
@ -24,5 +27,5 @@ func Dashboard(c echo.Context) error {
partials := []string{"header"}
// Render the template
return lib.RenderTemplate(c, "base", partials, props)
return lib.RenderTemplate(c, "base", partials, props, pageError)
}

View file

@ -11,6 +11,9 @@ type ExampleProps struct {
}
func Example(c echo.Context) error {
// Initialize the page error
pageError := lib.Error{}
props := ExampleProps{
ExamplePropText: "EXAMPLE TEXT HERE",
}
@ -19,5 +22,5 @@ func Example(c echo.Context) error {
partials := []string{"header", "navitems"}
// Render the template
return lib.RenderTemplate(c, "base", partials, props)
return lib.RenderTemplate(c, "base", partials, props, pageError)
}

View file

@ -10,11 +10,14 @@ type HomeProps struct {
}
func Home(c echo.Context) error {
// Initialize the page error
pageError := lib.Error{}
props := HomeProps{}
// Specify the partials used by this page
partials := []string{"header"}
// Render the template
return lib.RenderTemplate(c, "base", partials, props)
return lib.RenderTemplate(c, "base", partials, props, pageError)
}

View file

@ -10,11 +10,14 @@ type RegisterProps struct {
}
func Register(c echo.Context) error {
// Initialize the page error
pageError := lib.Error{}
props := RegisterProps{}
// Specify the partials used by this page
partials := []string{"header"}
// Render the template
return lib.RenderTemplate(c, "base", partials, props)
return lib.RenderTemplate(c, "base", partials, props, pageError)
}

View file

@ -11,19 +11,27 @@ type RoomProps struct {
}
func Room(c echo.Context) error {
_, error := lib.GetSessionCookie(c.Request(), "session")
if error != nil {
return c.Redirect(302, "/signin")
}
// Get the room ID from the URL
roomID := c.Param("id")
println("Room ID: ", roomID)
// Initialize the page error
pageError := lib.Error{}
// Get the room from the database
room, err := lib.GetRoomById(lib.GetDBPool(), roomID)
if err != nil {
return c.String(404, "Room not found!")
pageError = lib.Error{
Code: 404,
Message: "Room not found",
}
props := RoomProps{}
// Specify the partials used by this page
partials := []string{"header"}
// Render the template
return lib.RenderTemplate(c, "base", partials, props, pageError)
}
props := RoomProps{
@ -34,5 +42,5 @@ func Room(c echo.Context) error {
partials := []string{"header"}
// Render the template
return lib.RenderTemplate(c, "base", partials, props)
return lib.RenderTemplate(c, "base", partials, props, pageError)
}

View file

@ -10,11 +10,14 @@ type SignInProps struct {
}
func SignIn(c echo.Context) error {
// Initialize the page error
pageError := lib.Error{}
props := SignInProps{}
// Specify the partials used by this page
partials := []string{"header"}
// Render the template
return lib.RenderTemplate(c, "base", partials, props)
return lib.RenderTemplate(c, "base", partials, props, pageError)
}

View file

@ -12,7 +12,19 @@
<body class="block h-[100%]">
{{template "header" .}}
<main class="container flex flex-col items-center justify-center gap-3 sm:gap-6 p-4 text-center mx-auto min-h-[calc(100%-64px)]">
{{if .Error.Code}}
<h1 class="text-3xl sm:text-6xl font-bold">
<span class="bg-gradient-to-r from-primary via-accent to-secondary bg-clip-text text-transparent box-decoration-clone">
Error {{.Error.Code}}
</span>
</h1>
<h2 class="my-4 text-xl sm:text-3xl font-bold">
{{.Error.Message}}
</h2>
{{else}}
{{template "main" .}}
{{end}}
</main>
{{template "foot" .}}
</body>

View file

@ -12,11 +12,11 @@ Pollo // Room
{{define "main"}}
<div class="flex flex-col items-center justify-center gap-8">
<div class="text-2xl">{{.Room.RoomName}}</div>
<div class="text-2xl">{{.Props.Room.RoomName}}</div>
<div class="flex flex-row flex-wrap text-center justify-center items-center gap-1 text-md">
<div>ID:</div>
<div>{{.Room.ID}}</div>
<div>{{.Props.Room.ID}}</div>
</div>
</div>
{{end}}

File diff suppressed because one or more lines are too long