added flash messages
This commit is contained in:
parent
a80dac4985
commit
82f11b254b
6 changed files with 72 additions and 13 deletions
|
@ -4,7 +4,9 @@
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
<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>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
|
@ -19,6 +21,12 @@
|
||||||
<div class="container py-5">
|
<div class="container py-5">
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-xl-8 order-1 order-xl-0">
|
<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}}
|
{{block "content" .}}{{end}}
|
||||||
</div>
|
</div>
|
||||||
<div class="col-xl-4 order-0 order-xl-1">
|
<div class="col-xl-4 order-0 order-xl-1">
|
||||||
|
|
|
@ -34,6 +34,8 @@ func (h *CommentHandler) Store() http.HandlerFunc {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
h.sessions.Put(r.Context(), "flash", "Your Comment has been added.")
|
||||||
|
|
||||||
http.Redirect(w, r, r.Referer(), http.StatusFound)
|
http.Redirect(w, r, r.Referer(), http.StatusFound)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package web
|
||||||
import (
|
import (
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"git.snrd.de/Spaenny/goddit"
|
"git.snrd.de/Spaenny/goddit"
|
||||||
"github.com/alexedwards/scs/v2"
|
"github.com/alexedwards/scs/v2"
|
||||||
|
@ -53,9 +54,13 @@ type Handler struct {
|
||||||
|
|
||||||
func (h *Handler) Home() http.HandlerFunc {
|
func (h *Handler) Home() http.HandlerFunc {
|
||||||
type data struct {
|
type data struct {
|
||||||
|
SessionData
|
||||||
|
|
||||||
Posts []goddit.Post
|
Posts []goddit.Post
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var once sync.Once
|
||||||
|
|
||||||
tmpl := template.Must(template.ParseFiles("templates/layout.html", "templates/home.html"))
|
tmpl := template.Must(template.ParseFiles("templates/layout.html", "templates/home.html"))
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
pp, err := h.store.Posts()
|
pp, err := h.store.Posts()
|
||||||
|
@ -64,6 +69,13 @@ func (h *Handler) Home() http.HandlerFunc {
|
||||||
return
|
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,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,8 @@ type PostHandler struct {
|
||||||
|
|
||||||
func (h *PostHandler) Create() http.HandlerFunc {
|
func (h *PostHandler) Create() http.HandlerFunc {
|
||||||
type data struct {
|
type data struct {
|
||||||
|
SessionData
|
||||||
|
|
||||||
CSRF template.HTML
|
CSRF template.HTML
|
||||||
Thread goddit.Thread
|
Thread goddit.Thread
|
||||||
}
|
}
|
||||||
|
@ -38,14 +40,16 @@ func (h *PostHandler) Create() http.HandlerFunc {
|
||||||
}
|
}
|
||||||
|
|
||||||
tmpl.Execute(w, data{
|
tmpl.Execute(w, data{
|
||||||
CSRF: csrf.TemplateField(r),
|
SessionData: GetSessionData(h.sessions, r.Context()),
|
||||||
Thread: t,
|
CSRF: csrf.TemplateField(r),
|
||||||
|
Thread: t,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *PostHandler) Show() http.HandlerFunc {
|
func (h *PostHandler) Show() http.HandlerFunc {
|
||||||
type data struct {
|
type data struct {
|
||||||
|
SessionData
|
||||||
CSRF template.HTML
|
CSRF template.HTML
|
||||||
Thread goddit.Thread
|
Thread goddit.Thread
|
||||||
Post goddit.Post
|
Post goddit.Post
|
||||||
|
@ -87,10 +91,11 @@ func (h *PostHandler) Show() http.HandlerFunc {
|
||||||
}
|
}
|
||||||
|
|
||||||
tmpl.Execute(w, data{
|
tmpl.Execute(w, data{
|
||||||
CSRF: csrf.TemplateField(r),
|
SessionData: GetSessionData(h.sessions, r.Context()),
|
||||||
Thread: t,
|
CSRF: csrf.TemplateField(r),
|
||||||
Post: p,
|
Thread: t,
|
||||||
Comments: cc,
|
Post: p,
|
||||||
|
Comments: cc,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -126,6 +131,8 @@ func (h *PostHandler) Store() http.HandlerFunc {
|
||||||
return
|
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)
|
http.Redirect(w, r, "/threads/"+t.ID.String()+"/"+p.ID.String(), http.StatusFound)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package web
|
package web
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
|
||||||
"github.com/alexedwards/scs/postgresstore"
|
"github.com/alexedwards/scs/postgresstore"
|
||||||
|
@ -18,3 +19,17 @@ func NewSessionsManager(dataSourceName string) (*scs.SessionManager, error) {
|
||||||
|
|
||||||
return sessions, nil
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -18,6 +18,8 @@ type ThreadHandler struct {
|
||||||
|
|
||||||
func (h *ThreadHandler) List() http.HandlerFunc {
|
func (h *ThreadHandler) List() http.HandlerFunc {
|
||||||
type data struct {
|
type data struct {
|
||||||
|
SessionData
|
||||||
|
|
||||||
Threads []goddit.Thread
|
Threads []goddit.Thread
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,24 +31,32 @@ func (h *ThreadHandler) List() http.HandlerFunc {
|
||||||
return
|
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 {
|
func (h *ThreadHandler) Create() http.HandlerFunc {
|
||||||
type data struct {
|
type data struct {
|
||||||
|
SessionData
|
||||||
|
|
||||||
CSRF template.HTML
|
CSRF template.HTML
|
||||||
}
|
}
|
||||||
tmpl := template.Must(template.ParseFiles("templates/layout.html", "templates/thread_create.html"))
|
tmpl := template.Must(template.ParseFiles("templates/layout.html", "templates/thread_create.html"))
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
tmpl.Execute(w, data{
|
tmpl.Execute(w, data{
|
||||||
CSRF: csrf.TemplateField(r),
|
SessionData: GetSessionData(h.sessions, r.Context()),
|
||||||
|
CSRF: csrf.TemplateField(r),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *ThreadHandler) Show() http.HandlerFunc {
|
func (h *ThreadHandler) Show() http.HandlerFunc {
|
||||||
type data struct {
|
type data struct {
|
||||||
|
SessionData
|
||||||
|
|
||||||
CSRF template.HTML
|
CSRF template.HTML
|
||||||
Thread goddit.Thread
|
Thread goddit.Thread
|
||||||
Posts []goddit.Post
|
Posts []goddit.Post
|
||||||
|
@ -71,9 +81,10 @@ func (h *ThreadHandler) Show() http.HandlerFunc {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
tmpl.Execute(w, data{
|
tmpl.Execute(w, data{
|
||||||
CSRF: csrf.TemplateField(r),
|
SessionData: GetSessionData(h.sessions, r.Context()),
|
||||||
Thread: t,
|
CSRF: csrf.TemplateField(r),
|
||||||
Posts: pp,
|
Thread: t,
|
||||||
|
Posts: pp,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -92,6 +103,8 @@ func (h *ThreadHandler) Store() http.HandlerFunc {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
h.sessions.Put(r.Context(), "flash", "Your new thread has been created.")
|
||||||
|
|
||||||
http.Redirect(w, r, "/threads", http.StatusFound)
|
http.Redirect(w, r, "/threads", http.StatusFound)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -111,6 +124,8 @@ func (h *ThreadHandler) Delete() http.HandlerFunc {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
h.sessions.Put(r.Context(), "flash", "The thread has been deleted.")
|
||||||
|
|
||||||
http.Redirect(w, r, "/threads", http.StatusFound)
|
http.Redirect(w, r, "/threads", http.StatusFound)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue