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

View file

@ -11,6 +11,9 @@ type DashboardProps struct {
} }
func Dashboard(c echo.Context) error { func Dashboard(c echo.Context) error {
// Initialize the page error
pageError := lib.Error{}
currentSession, error := lib.GetSessionCookie(c.Request(), "session") currentSession, error := lib.GetSessionCookie(c.Request(), "session")
if error != nil { if error != nil {
lib.LogError.Printf("Error getting session: %v", error) lib.LogError.Printf("Error getting session: %v", error)
@ -24,5 +27,5 @@ func Dashboard(c echo.Context) error {
partials := []string{"header"} partials := []string{"header"}
// Render the template // 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 { func Example(c echo.Context) error {
// Initialize the page error
pageError := lib.Error{}
props := ExampleProps{ props := ExampleProps{
ExamplePropText: "EXAMPLE TEXT HERE", ExamplePropText: "EXAMPLE TEXT HERE",
} }
@ -19,5 +22,5 @@ func Example(c echo.Context) error {
partials := []string{"header", "navitems"} partials := []string{"header", "navitems"}
// Render the template // 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 { func Home(c echo.Context) error {
// Initialize the page error
pageError := lib.Error{}
props := HomeProps{} props := HomeProps{}
// Specify the partials used by this page // Specify the partials used by this page
partials := []string{"header"} partials := []string{"header"}
// Render the template // 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 { func Register(c echo.Context) error {
// Initialize the page error
pageError := lib.Error{}
props := RegisterProps{} props := RegisterProps{}
// Specify the partials used by this page // Specify the partials used by this page
partials := []string{"header"} partials := []string{"header"}
// Render the template // 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 { 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 // Get the room ID from the URL
roomID := c.Param("id") roomID := c.Param("id")
println("Room ID: ", roomID)
// Initialize the page error
pageError := lib.Error{}
// Get the room from the database // Get the room from the database
room, err := lib.GetRoomById(lib.GetDBPool(), roomID) room, err := lib.GetRoomById(lib.GetDBPool(), roomID)
if err != nil { 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{ props := RoomProps{
@ -34,5 +42,5 @@ func Room(c echo.Context) error {
partials := []string{"header"} partials := []string{"header"}
// Render the template // 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 { func SignIn(c echo.Context) error {
// Initialize the page error
pageError := lib.Error{}
props := SignInProps{} props := SignInProps{}
// Specify the partials used by this page // Specify the partials used by this page
partials := []string{"header"} partials := []string{"header"}
// Render the template // 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%]"> <body class="block h-[100%]">
{{template "header" .}} {{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)]"> <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" .}} {{template "main" .}}
{{end}}
</main> </main>
{{template "foot" .}} {{template "foot" .}}
</body> </body>

View file

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

File diff suppressed because one or more lines are too long