Added easier setter funcs for flash messages.

This commit is contained in:
mikestefanello 2021-12-03 16:44:04 -05:00
parent fc1d3cdc33
commit d40244b3d1
4 changed files with 30 additions and 14 deletions

View file

@ -20,7 +20,7 @@ func (a *Contact) Get(c echo.Context) error {
}
func (a *Contact) Post(c echo.Context) error {
msg.Set(c, msg.Success, "Thank you for contacting us!")
msg.Set(c, msg.Info, "We will respond to you shortly.")
msg.Success(c, "Thank you for contacting us!")
msg.Info(c, "We will respond to you shortly.")
return a.Redirect(c, "home")
}

View file

@ -20,6 +20,6 @@ func (l *Login) Get(c echo.Context) error {
}
func (l *Login) Post(c echo.Context) error {
msg.Set(c, msg.Danger, "Invalid credentials. Please try again.")
msg.Danger(c, "Invalid credentials. Please try again.")
return l.Get(c)
}

View file

@ -20,6 +20,6 @@ func (r *Register) Get(c echo.Context) error {
}
func (r *Register) Post(c echo.Context) error {
msg.Set(c, msg.Danger, "Registration is currently disabled.")
msg.Danger(c, "Registration is currently disabled.")
return r.Get(c)
}

View file

@ -9,23 +9,34 @@ import (
type Type string
const (
Success Type = "success"
Info Type = "info"
Warning Type = "warning"
Danger Type = "danger"
TypeSuccess Type = "success"
TypeInfo Type = "info"
TypeWarning Type = "warning"
TypeDanger Type = "danger"
)
// TODO: Error handling
// TODO: Error handling and cleanup
func getSession(c echo.Context) *sessions.Session {
sess, _ := session.Get("msg", c)
return sess
func Success(c echo.Context, message string) {
Set(c, TypeSuccess, message)
}
func Info(c echo.Context, message string) {
Set(c, TypeInfo, message)
}
func Warning(c echo.Context, message string) {
Set(c, TypeWarning, message)
}
func Danger(c echo.Context, message string) {
Set(c, TypeDanger, message)
}
// Set adds a new message into the cookie storage.
func Set(c echo.Context, typ Type, value string) {
func Set(c echo.Context, typ Type, message string) {
sess := getSession(c)
sess.AddFlash(value, string(typ))
sess.AddFlash(message, string(typ))
_ = sess.Save(c.Request(), c.Response())
}
@ -47,3 +58,8 @@ func Get(c echo.Context, typ Type) []string {
}
return nil
}
func getSession(c echo.Context) *sessions.Session {
sess, _ := session.Get("msg", c)
return sess
}