diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index a310008..fc88626 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -119,12 +119,17 @@ func (m *Repository) PostMakeReservation(w http.ResponseWriter, r *http.Request) http.Redirect(w, r, "/reservation-summary", http.StatusSeeOther) } +// ReservationSummary is the reservation summary page handler func (m *Repository) ReservationSummary(w http.ResponseWriter, r *http.Request) { reservation, ok := m.App.Session.Get(r.Context(), "reservation").(models.Reservation) if !ok { log.Println("connot get item from reservation") + m.App.Session.Put(r.Context(), "error", "Can't get reservation from session") + http.Redirect(w, r, "/", http.StatusTemporaryRedirect) } + m.App.Session.Remove(r.Context(), "reservation") + data := make(map[string]interface{}) data["reservation"] = reservation diff --git a/internal/render/render.go b/internal/render/render.go index dfd80fa..17caaea 100644 --- a/internal/render/render.go +++ b/internal/render/render.go @@ -21,6 +21,9 @@ func NewTemplates(a *config.AppConfig) { // AddDefaultData adds default template data func AddDefaultData(td *models.TemplateData, r *http.Request) *models.TemplateData { + td.Flash = app.Session.PopString(r.Context(), "flash") + td.Warning = app.Session.PopString(r.Context(), "warning") + td.Error = app.Session.PopString(r.Context(), "error") td.CSRFToken = nosurf.Token(r) return td } diff --git a/templates/base.layout.tmpl b/templates/base.layout.tmpl index f17f16f..e53bccd 100644 --- a/templates/base.layout.tmpl +++ b/templates/base.layout.tmpl @@ -116,6 +116,16 @@ }) } + {{with .Error}} + notify("{{.}}", "error") + {{end}} + {{with .Warning}} + notify("{{.}}", "warning") + {{end}} + {{with .Flash}} + notify("{{.}}", "flash") + {{end}} + // Prompt is out Javascript module for all alerts, notifications, and custom popup dialogs function Prompt() { let toast = function (c) {