+ {{with .FlashMessage}}
+
+ {{.}}
+
+
+ {{end}}
{{block "content" .}}{{end}}
diff --git a/web/comment_handler.go b/web/comment_handler.go
index de2199c..8ce42bb 100644
--- a/web/comment_handler.go
+++ b/web/comment_handler.go
@@ -34,6 +34,8 @@ func (h *CommentHandler) Store() http.HandlerFunc {
return
}
+ h.sessions.Put(r.Context(), "flash", "Your Comment has been added.")
+
http.Redirect(w, r, r.Referer(), http.StatusFound)
}
}
diff --git a/web/handler.go b/web/handler.go
index 5209266..8491102 100644
--- a/web/handler.go
+++ b/web/handler.go
@@ -3,6 +3,7 @@ package web
import (
"html/template"
"net/http"
+ "sync"
"git.snrd.de/Spaenny/goddit"
"github.com/alexedwards/scs/v2"
@@ -53,9 +54,13 @@ type Handler struct {
func (h *Handler) Home() http.HandlerFunc {
type data struct {
+ SessionData
+
Posts []goddit.Post
}
+ var once sync.Once
+
tmpl := template.Must(template.ParseFiles("templates/layout.html", "templates/home.html"))
return func(w http.ResponseWriter, r *http.Request) {
pp, err := h.store.Posts()
@@ -64,6 +69,13 @@ func (h *Handler) Home() http.HandlerFunc {
return
}
- tmpl.Execute(w, data{Posts: pp})
+ once.Do(func() {
+ h.sessions.Put(r.Context(), "flash", "hello")
+ })
+
+ tmpl.Execute(w, data{
+ SessionData: GetSessionData(h.sessions, r.Context()),
+ Posts: pp,
+ })
}
}
diff --git a/web/post_handler.go b/web/post_handler.go
index 870eb2f..5f7ac5b 100644
--- a/web/post_handler.go
+++ b/web/post_handler.go
@@ -18,6 +18,8 @@ type PostHandler struct {
func (h *PostHandler) Create() http.HandlerFunc {
type data struct {
+ SessionData
+
CSRF template.HTML
Thread goddit.Thread
}
@@ -38,14 +40,16 @@ func (h *PostHandler) Create() http.HandlerFunc {
}
tmpl.Execute(w, data{
- CSRF: csrf.TemplateField(r),
- Thread: t,
+ SessionData: GetSessionData(h.sessions, r.Context()),
+ CSRF: csrf.TemplateField(r),
+ Thread: t,
})
}
}
func (h *PostHandler) Show() http.HandlerFunc {
type data struct {
+ SessionData
CSRF template.HTML
Thread goddit.Thread
Post goddit.Post
@@ -87,10 +91,11 @@ func (h *PostHandler) Show() http.HandlerFunc {
}
tmpl.Execute(w, data{
- CSRF: csrf.TemplateField(r),
- Thread: t,
- Post: p,
- Comments: cc,
+ SessionData: GetSessionData(h.sessions, r.Context()),
+ CSRF: csrf.TemplateField(r),
+ Thread: t,
+ Post: p,
+ Comments: cc,
})
}
}
@@ -126,6 +131,8 @@ func (h *PostHandler) Store() http.HandlerFunc {
return
}
+ h.sessions.Put(r.Context(), "flash", "Your Post has been created.")
+
http.Redirect(w, r, "/threads/"+t.ID.String()+"/"+p.ID.String(), http.StatusFound)
}
}
diff --git a/web/sessions.go b/web/sessions.go
index 3add466..d39c216 100644
--- a/web/sessions.go
+++ b/web/sessions.go
@@ -1,6 +1,7 @@
package web
import (
+ "context"
"database/sql"
"github.com/alexedwards/scs/postgresstore"
@@ -18,3 +19,17 @@ func NewSessionsManager(dataSourceName string) (*scs.SessionManager, error) {
return sessions, nil
}
+
+type SessionData struct {
+ FlashMessage string
+ // UserID uuid.UUID
+}
+
+func GetSessionData(session *scs.SessionManager, ctx context.Context) SessionData {
+ var data SessionData
+
+ data.FlashMessage = session.PopString(ctx, "flash")
+ // data.UserID, _ = session.Get(ctx "user_id").(uuid.UUID)
+
+ return data
+}
diff --git a/web/thread_handler.go b/web/thread_handler.go
index fd54c60..5c3efa8 100644
--- a/web/thread_handler.go
+++ b/web/thread_handler.go
@@ -18,6 +18,8 @@ type ThreadHandler struct {
func (h *ThreadHandler) List() http.HandlerFunc {
type data struct {
+ SessionData
+
Threads []goddit.Thread
}
@@ -29,24 +31,32 @@ func (h *ThreadHandler) List() http.HandlerFunc {
return
}
- tmpl.Execute(w, data{Threads: tt})
+ tmpl.Execute(w, data{
+ SessionData: GetSessionData(h.sessions, r.Context()),
+ Threads: tt,
+ })
}
}
func (h *ThreadHandler) Create() http.HandlerFunc {
type data struct {
+ SessionData
+
CSRF template.HTML
}
tmpl := template.Must(template.ParseFiles("templates/layout.html", "templates/thread_create.html"))
return func(w http.ResponseWriter, r *http.Request) {
tmpl.Execute(w, data{
- CSRF: csrf.TemplateField(r),
+ SessionData: GetSessionData(h.sessions, r.Context()),
+ CSRF: csrf.TemplateField(r),
})
}
}
func (h *ThreadHandler) Show() http.HandlerFunc {
type data struct {
+ SessionData
+
CSRF template.HTML
Thread goddit.Thread
Posts []goddit.Post
@@ -71,9 +81,10 @@ func (h *ThreadHandler) Show() http.HandlerFunc {
return
}
tmpl.Execute(w, data{
- CSRF: csrf.TemplateField(r),
- Thread: t,
- Posts: pp,
+ SessionData: GetSessionData(h.sessions, r.Context()),
+ CSRF: csrf.TemplateField(r),
+ Thread: t,
+ Posts: pp,
})
}
}
@@ -92,6 +103,8 @@ func (h *ThreadHandler) Store() http.HandlerFunc {
return
}
+ h.sessions.Put(r.Context(), "flash", "Your new thread has been created.")
+
http.Redirect(w, r, "/threads", http.StatusFound)
}
}
@@ -111,6 +124,8 @@ func (h *ThreadHandler) Delete() http.HandlerFunc {
return
}
+ h.sessions.Put(r.Context(), "flash", "The thread has been deleted.")
+
http.Redirect(w, r, "/threads", http.StatusFound)
}
}