Skip to content

Commit

Permalink
impl: flash messages
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDevMinerTV committed Oct 4, 2023
1 parent 3e2275e commit fffd6df
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 1 deletion.
3 changes: 2 additions & 1 deletion internal/web/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (a *App) loginHandler(c *fiber.Ctx) error {
"session": sess,
"title": "Login",
"headscripts": "",
"flashes": []string{"Invalid username or password"},
"flashes": []Flash{NewFlash(FlashTypeError, "Invalid username or password")},
}, "layouts/base")
}

Expand All @@ -53,5 +53,6 @@ func (a *App) loginHandler(c *fiber.Ctx) error {
"session": sess,
"title": "Login",
"headscripts": "",
"flashes": []Flash{},
}, "layouts/base")
}
2 changes: 2 additions & 0 deletions internal/web/computers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func (a *App) computersHandler(c *fiber.Ctx) error {
"title": "All computers",
"activePage": "/computers",
"headscripts": "",
"flashes": []Flash{},
"computers": computers,
}, "layouts/logged-in")
}
Expand Down Expand Up @@ -54,6 +55,7 @@ func (a *App) computerHandler(c *fiber.Ctx) error {
"title": computer.CN(),
"activePage": "/computers",
"headscripts": "",
"flashes": []Flash{},
"computer": computer,
}, "layouts/logged-in")
}
2 changes: 2 additions & 0 deletions internal/web/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func (a *App) groupsHandler(c *fiber.Ctx) error {
"title": "All groups",
"activePage": "/groups",
"headscripts": "",
"flashes": []Flash{},
"groups": groups,
}, "layouts/logged-in")
}
Expand Down Expand Up @@ -54,6 +55,7 @@ func (a *App) groupHandler(c *fiber.Ctx) error {
"title": group.CN(),
"activePage": "/groups",
"headscripts": "",
"flashes": []Flash{},
"group": group,
}, "layouts/logged-in")
}
8 changes: 8 additions & 0 deletions internal/web/layouts/logged-in.html
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@
</nav>

<div class="mx-auto w-full max-w-4xl flex-1 p-4">
{{ if len .flashes }}
<div class="mb-4">
{{ range .flashes }}
<div class="{{ .BorderColor }} rounded-md border p-4 py-3">{{ .Message }}</div>
{{ end }}
</div>
{{ end }}

{{ embed }}
</div>
</body>
Expand Down
3 changes: 3 additions & 0 deletions internal/web/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func handle500(c *fiber.Ctx, err error) error {
return c.Render("views/500", fiber.Map{
"title": "error",
"headscripts": "",
"flashes": []Flash{},
"error": err.Error(),
}, "layouts/base")
}
Expand All @@ -117,6 +118,7 @@ func (a *App) indexHandler(c *fiber.Ctx) error {
"title": "List",
"activePage": "/",
"headscripts": "",
"flashes": []Flash{},
"user": user,
}, "layouts/logged-in")
}
Expand All @@ -125,5 +127,6 @@ func (a *App) fourOhFourHandler(c *fiber.Ctx) error {
return c.Render("views/404", fiber.Map{
"title": "404",
"headscripts": "",
"flashes": []Flash{},
}, "layouts/base")
}
49 changes: 49 additions & 0 deletions internal/web/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,52 @@ func tplNavbarActive(activeTab, tab string) string {

return navbarActiveClasses
}

type FlashType string

const (
FlashTypeSuccess FlashType = "success"
FlashTypeError FlashType = "error"
FlashTypeInfo FlashType = "info"
)

type Flash struct {
Message string
Type FlashType
}

func NewFlash(type_ FlashType, message string) Flash {
return Flash{
Message: message,
Type: type_,
}
}

func (f Flash) IsSuccess() bool {
return f.Type == FlashTypeSuccess
}

func (f Flash) IsError() bool {
return f.Type == FlashTypeError
}

func (f Flash) IsInfo() bool {
return f.Type == FlashTypeInfo
}

const flashSuccessClasses = "border-green-500"
const flashErrorClasses = "border-red-500"
const flashInfoClasses = "border-blue-500"

func (f Flash) BorderColor() string {
switch f.Type {
case FlashTypeSuccess:
return flashSuccessClasses
case FlashTypeError:
return flashErrorClasses
case FlashTypeInfo:
return flashInfoClasses
default:
panic("unknown flash type")
}
}
2 changes: 2 additions & 0 deletions internal/web/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func (a *App) usersHandler(c *fiber.Ctx) error {
"title": "All users",
"activePage": "/users",
"headscripts": "",
"flashes": []Flash{},
"users": users,
}, "layouts/logged-in")
}
Expand Down Expand Up @@ -54,6 +55,7 @@ func (a *App) userHandler(c *fiber.Ctx) error {
"title": user.CN(),
"activePage": "/users",
"headscripts": "",
"flashes": []Flash{},
"user": user,
}, "layouts/logged-in")
}
9 changes: 9 additions & 0 deletions internal/web/views/login.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
<form class="space-y-4 rounded-md border border-gray-500 p-8" action="/login" method="get">
{{ if len .flashes }}
<div class="mb-4">
{{ range .flashes }}
<div class="{{ .BorderColor }} rounded-md border p-4 py-3">{{ .Message }}</div>
{{ end }}
</div>
{{ end }}


<div>
<input
type="text"
Expand Down

0 comments on commit fffd6df

Please sign in to comment.