added flash messages

This commit is contained in:
Philipp 2021-08-29 17:43:45 +02:00
parent a80dac4985
commit 82f11b254b
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG key ID: 276B613AF9DBE9C3
6 changed files with 72 additions and 13 deletions

View file

@ -4,7 +4,9 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.3/dist/umd/popper.min.js" integrity="sha384-eMNCOe7tC1doHpGoWe/6oMVemdAVTMs2xqW4mwXrXsW0L84Iytr2wi5v2QjrP/xp" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/js/bootstrap.min.js" integrity="sha384-cn7l7gDp0eyniUwwAZgrzD06kc/tftFf19TOAs2zVinnD/C7E91j9yyk5//jjpt/" crossorigin="anonymous"></script>
</head>
<body>
@ -19,6 +21,12 @@
<div class="container py-5">
<div class="row">
<div class="col-xl-8 order-1 order-xl-0">
{{with .FlashMessage}}
<div id="my-alert" class="alert alert-primary alert-dismissible fade show" role="alert">
{{.}}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
{{end}}
{{block "content" .}}{{end}}
</div>
<div class="col-xl-4 order-0 order-xl-1">

View file

@ -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)
}
}

View file

@ -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,
})
}
}

View file

@ -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)
}
}

View file

@ -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
}

View file

@ -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)
}
}