You've already forked golang-saas-starter-kit
mirror of
https://github.com/raseels-repos/golang-saas-starter-kit.git
synced 2025-08-08 22:36:41 +02:00
completed flash messages
This commit is contained in:
81
internal/platform/web/webcontext/flash_message.go
Normal file
81
internal/platform/web/webcontext/flash_message.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package webcontext
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/gob"
|
||||
"html/template"
|
||||
)
|
||||
|
||||
type FlashType string
|
||||
|
||||
var (
|
||||
FlashType_Success FlashType = "success"
|
||||
FlashType_Info FlashType = "info"
|
||||
FlashType_Warning FlashType = "warning"
|
||||
FlashType_Error FlashType = "danger"
|
||||
)
|
||||
|
||||
type FlashMsg struct {
|
||||
Type FlashType `json:"type"`
|
||||
Title string `json:"title"`
|
||||
Text string `json:"text"`
|
||||
Items []string `json:"items"`
|
||||
Details string `json:"details"`
|
||||
}
|
||||
|
||||
func (r FlashMsg) Response(ctx context.Context) map[string]interface{} {
|
||||
var items []template.HTML
|
||||
for _, i := range r.Items {
|
||||
items = append(items, template.HTML(i))
|
||||
}
|
||||
|
||||
return map[string]interface{}{
|
||||
"Type": r.Type,
|
||||
"Title": r.Title,
|
||||
"Text": template.HTML(r.Text),
|
||||
"Items": items,
|
||||
"Details": template.HTML(r.Details),
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
gob.Register(&FlashMsg{})
|
||||
}
|
||||
|
||||
// SessionAddFlash loads the session from context that is provided by the session middleware and
|
||||
// adds the message to the session. The renderer should save the session before writing the response
|
||||
// to the client or save be directly invoked.
|
||||
func SessionAddFlash(ctx context.Context, msg FlashMsg) {
|
||||
ContextSession(ctx).AddFlash(msg.Response(ctx))
|
||||
}
|
||||
|
||||
// SessionFlashSuccess add a message with type Success.
|
||||
func SessionFlashSuccess(ctx context.Context, title, text string, items ...string) {
|
||||
sessionFlashType(ctx, FlashType_Success, title, text, items...)
|
||||
}
|
||||
|
||||
// SessionFlashInfo add a message with type Info.
|
||||
func SessionFlashInfo(ctx context.Context, title, text string, items ...string) {
|
||||
sessionFlashType(ctx, FlashType_Info, title, text, items...)
|
||||
}
|
||||
|
||||
// SessionFlashWarning add a message with type Warning.
|
||||
func SessionFlashWarning(ctx context.Context, title, text string, items ...string) {
|
||||
sessionFlashType(ctx, FlashType_Warning, title, text, items...)
|
||||
}
|
||||
|
||||
// SessionFlashError add a message with type Error.
|
||||
func SessionFlashError(ctx context.Context, title, text string, items ...string) {
|
||||
sessionFlashType(ctx, FlashType_Error, title, text, items...)
|
||||
}
|
||||
|
||||
// sessionFlashType adds a flash message with the specified type.
|
||||
func sessionFlashType(ctx context.Context, flashType FlashType, title, text string, items ...string) {
|
||||
msg := FlashMsg{
|
||||
Type: flashType,
|
||||
Title: title,
|
||||
Text: text,
|
||||
Items: items,
|
||||
}
|
||||
SessionAddFlash(ctx, msg)
|
||||
}
|
@@ -12,6 +12,9 @@ type ctxKeySession int
|
||||
// KeySession is used to store/retrieve a Session from a context.Context.
|
||||
const KeySession ctxKeySession = 1
|
||||
|
||||
// KeyAccessToken is used to store the access token for the user in their session.
|
||||
const KeyAccessToken = "AccessToken"
|
||||
|
||||
// ContextWithSession appends a universal translator to a context.
|
||||
func ContextWithSession(ctx context.Context, session *sessions.Session) context.Context {
|
||||
return context.WithValue(ctx, KeySession, session)
|
||||
@@ -29,7 +32,7 @@ func ContextAccessToken(ctx context.Context) (string, bool) {
|
||||
}
|
||||
|
||||
func SessionAccessToken(session *sessions.Session) (string, bool) {
|
||||
if sv, ok := session.Values["AccessToken"].(string); ok {
|
||||
if sv, ok := session.Values[KeyAccessToken].(string); ok {
|
||||
return sv, true
|
||||
}
|
||||
|
||||
@@ -39,9 +42,9 @@ func SessionAccessToken(session *sessions.Session) (string, bool) {
|
||||
func SessionWithAccessToken(session *sessions.Session, accessToken string) *sessions.Session {
|
||||
|
||||
if accessToken != "" {
|
||||
session.Values["AccessToken"] = accessToken
|
||||
session.Values[KeyAccessToken] = accessToken
|
||||
} else {
|
||||
delete(session.Values, "AccessToken")
|
||||
delete(session.Values, KeyAccessToken)
|
||||
}
|
||||
|
||||
return session
|
||||
|
@@ -72,7 +72,6 @@ func init() {
|
||||
transNl, _ := uniTrans.GetTranslator(nl.Locale())
|
||||
transZh, _ := uniTrans.GetTranslator(zh.Locale())
|
||||
|
||||
|
||||
transEn.Add("{{name}}", "Name", false)
|
||||
transFr.Add("{{name}}", "Nom", false)
|
||||
|
||||
|
Reference in New Issue
Block a user