Saving success messages betteR

This commit is contained in:
Simon Martens
2026-01-23 20:00:55 +01:00
parent 7ef2611537
commit 0beb5a2c79
20 changed files with 1209 additions and 811 deletions

View File

@@ -81,7 +81,7 @@ func (p *AlmanachContentsEditPage) GET(engine *templating.Engine, app core.App)
data["pagination_values"] = paginationValuesSorted()
data["agent_relations"] = dbmodels.AGENT_RELATIONS
if msg := e.Request.URL.Query().Get("saved_message"); msg != "" {
if msg := popFlashSuccess(e); msg != "" {
data["success"] = msg
}
data["edit_content_id"] = strings.TrimSpace(e.Request.URL.Query().Get("edit_content"))
@@ -165,7 +165,7 @@ func (p *AlmanachContentsEditPage) GETItemEdit(engine *templating.Engine, app co
data["content_index"] = contentIndex
data["content_total"] = contentTotal
if msg := e.Request.URL.Query().Get("saved_message"); msg != "" {
if msg := popFlashSuccess(e); msg != "" {
data["success"] = msg
}
@@ -588,7 +588,8 @@ func (p *AlmanachContentsEditPage) POSTSave(engine *templating.Engine, app core.
go updateContentsFTS5(app, entry, touched)
}
savedMessage := url.QueryEscape("Änderungen gespeichert.")
saveAction := strings.TrimSpace(e.Request.FormValue("save_action"))
savedMessage := "Änderungen gespeichert."
if contentID != "" {
effectiveContentID := contentID
if mappedID, ok := tempToCreated[effectiveContentID]; ok {
@@ -596,12 +597,18 @@ func (p *AlmanachContentsEditPage) POSTSave(engine *templating.Engine, app core.
}
if effectiveContentID != "" {
if resolved, err := dbmodels.Contents_IDs(app, []any{effectiveContentID}); err == nil && len(resolved) > 0 {
redirect := fmt.Sprintf("/almanach/%s/contents/%d/edit?saved_message=%s", id, resolved[0].MusenalmID(), savedMessage)
if saveAction == "view" {
redirect := fmt.Sprintf("/beitrag/%d", resolved[0].MusenalmID())
return e.Redirect(http.StatusSeeOther, redirect)
}
setFlashSuccess(e, savedMessage)
redirect := fmt.Sprintf("/almanach/%s/contents/%d/edit", id, resolved[0].MusenalmID())
return e.Redirect(http.StatusSeeOther, redirect)
}
}
}
redirect := fmt.Sprintf("/almanach/%s/contents/edit?saved_message=%s", id, savedMessage)
setFlashSuccess(e, savedMessage)
redirect := fmt.Sprintf("/almanach/%s/contents/edit", id)
return e.Redirect(http.StatusSeeOther, redirect)
}
}
@@ -641,7 +648,8 @@ func (p *AlmanachContentsEditPage) POSTUpdateExtent(engine *templating.Engine, a
}
}(app, entry)
redirect := fmt.Sprintf("/almanach/%s/contents/edit?saved_message=%s", id, url.QueryEscape("Struktur/Umfang gespeichert."))
setFlashSuccess(e, "Struktur/Umfang gespeichert.")
redirect := fmt.Sprintf("/almanach/%s/contents/edit", id)
return e.Redirect(http.StatusSeeOther, redirect)
}
}

View File

@@ -67,7 +67,7 @@ func (p *AlmanachEditPage) GET(engine *templating.Engine, app core.App) HandleFu
data["agent_relations"] = dbmodels.AGENT_RELATIONS
data["series_relations"] = dbmodels.SERIES_RELATIONS
if msg := e.Request.URL.Query().Get("saved_message"); msg != "" {
if msg := popFlashSuccess(e); msg != "" {
data["success"] = msg
}
@@ -225,6 +225,7 @@ func (p *AlmanachEditPage) POSTSave(engine *templating.Engine, app core.App) Han
updatedInfo["user"] = user.Name
}
setFlashSuccess(e, "Änderungen gespeichert.")
return e.JSON(http.StatusOK, map[string]any{
"success": true,
"message": "Änderungen gespeichert.",

47
controllers/flash.go Normal file
View File

@@ -0,0 +1,47 @@
package controllers
import (
"net/http"
"net/url"
"github.com/pocketbase/pocketbase/core"
)
const flashSuccessCookieName = "flash_success"
func setFlashSuccess(e *core.RequestEvent, message string) {
if e == nil || message == "" {
return
}
e.SetCookie(&http.Cookie{
Name: flashSuccessCookieName,
Value: url.QueryEscape(message),
Path: "/",
MaxAge: 60,
HttpOnly: true,
SameSite: http.SameSiteLaxMode,
})
}
func popFlashSuccess(e *core.RequestEvent) string {
if e == nil || e.Request == nil {
return ""
}
cookie, err := e.Request.Cookie(flashSuccessCookieName)
if err != nil || cookie == nil || cookie.Value == "" {
return ""
}
e.SetCookie(&http.Cookie{
Name: flashSuccessCookieName,
Value: "",
Path: "/",
MaxAge: -1,
HttpOnly: true,
SameSite: http.SameSiteLaxMode,
})
value, err := url.QueryUnescape(cookie.Value)
if err != nil {
return ""
}
return value
}

View File

@@ -3,7 +3,6 @@ package controllers
import (
"fmt"
"net/http"
"net/url"
"slices"
"strings"
@@ -110,7 +109,7 @@ func (p *OrtEditPage) GET(engine *templating.Engine, app core.App) HandleFunc {
req := templating.NewRequest(e)
data["csrf_token"] = req.Session().Token
if msg := e.Request.URL.Query().Get("saved_message"); msg != "" {
if msg := popFlashSuccess(e); msg != "" {
data["success"] = msg
}
@@ -142,6 +141,7 @@ func (p *OrtEditPage) renderError(engine *templating.Engine, app core.App, e *co
type ortEditForm struct {
CSRFToken string `form:"csrf_token"`
LastEdited string `form:"last_edited"`
SaveAction string `form:"save_action"`
Name string `form:"name"`
Pseudonyms string `form:"pseudonyms"`
Annotation string `form:"annotation"`
@@ -237,7 +237,16 @@ func (p *OrtEditPage) POST(engine *templating.Engine, app core.App) HandleFunc {
}
}(app, place.Id, nameChanged)
redirect := fmt.Sprintf("/ort/%s/edit?saved_message=%s", id, url.QueryEscape("Änderungen gespeichert."))
if strings.TrimSpace(formdata.SaveAction) == "view" {
redirect := fmt.Sprintf("/reihen/?place=%s", id)
return e.Redirect(http.StatusSeeOther, redirect)
}
if strings.TrimSpace(formdata.SaveAction) == "view" {
redirect := fmt.Sprintf("/ort/%s", id)
return e.Redirect(http.StatusSeeOther, redirect)
}
setFlashSuccess(e, "Änderungen gespeichert.")
redirect := fmt.Sprintf("/ort/%s/edit", id)
return e.Redirect(http.StatusSeeOther, redirect)
}
}

View File

@@ -3,7 +3,6 @@ package controllers
import (
"fmt"
"net/http"
"net/url"
"slices"
"strings"
@@ -143,7 +142,8 @@ func (p *OrtNewPage) POST(engine *templating.Engine, app core.App) HandleFunc {
}
}(app, createdPlace.Id)
redirect := fmt.Sprintf("/ort/%s/edit?saved_message=%s", createdPlace.Id, url.QueryEscape("Änderungen gespeichert."))
setFlashSuccess(e, "Änderungen gespeichert.")
redirect := fmt.Sprintf("/ort/%s/edit", createdPlace.Id)
return e.Redirect(http.StatusSeeOther, redirect)
}
}

View File

@@ -122,7 +122,7 @@ func (p *PersonEditPage) GET(engine *templating.Engine, app core.App) HandleFunc
req := templating.NewRequest(e)
data["csrf_token"] = req.Session().Token
if msg := e.Request.URL.Query().Get("saved_message"); msg != "" {
if msg := popFlashSuccess(e); msg != "" {
data["success"] = msg
}
@@ -240,6 +240,7 @@ func agentContentsDetails(app core.App, agentID string) ([]*dbmodels.Content, ma
type personEditForm struct {
CSRFToken string `form:"csrf_token"`
LastEdited string `form:"last_edited"`
SaveAction string `form:"save_action"`
Name string `form:"name"`
Pseudonyms string `form:"pseudonyms"`
BiographicalData string `form:"biographical_data"`
@@ -348,7 +349,12 @@ func (p *PersonEditPage) POST(engine *templating.Engine, app core.App) HandleFun
}
}(app, agent.Id, nameChanged)
redirect := fmt.Sprintf("/person/%s", id)
if strings.TrimSpace(formdata.SaveAction) == "view" {
redirect := fmt.Sprintf("/person/%s", id)
return e.Redirect(http.StatusSeeOther, redirect)
}
setFlashSuccess(e, "Änderungen gespeichert.")
redirect := fmt.Sprintf("/person/%s/edit", id)
return e.Redirect(http.StatusSeeOther, redirect)
}
}

View File

@@ -131,7 +131,7 @@ func (p *ReiheEditPage) GET(engine *templating.Engine, app core.App) HandleFunc
req := templating.NewRequest(e)
data["csrf_token"] = req.Session().Token
if msg := e.Request.URL.Query().Get("saved_message"); msg != "" {
if msg := popFlashSuccess(e); msg != "" {
data["success"] = msg
}
@@ -353,6 +353,7 @@ func preferredSeriesEntries(app core.App, seriesID string) ([]*dbmodels.Entry, e
type reiheEditForm struct {
CSRFToken string `form:"csrf_token"`
LastEdited string `form:"last_edited"`
SaveAction string `form:"save_action"`
Title string `form:"title"`
Pseudonyms string `form:"pseudonyms"`
Annotation string `form:"annotation"`
@@ -448,7 +449,12 @@ func (p *ReiheEditPage) POST(engine *templating.Engine, app core.App) HandleFunc
}
}(app, series.Id, titleChanged)
redirect := fmt.Sprintf("/reihe/%s/", id)
if strings.TrimSpace(formdata.SaveAction) == "view" {
redirect := fmt.Sprintf("/reihe/%s/", id)
return e.Redirect(http.StatusSeeOther, redirect)
}
setFlashSuccess(e, "Änderungen gespeichert.")
redirect := fmt.Sprintf("/reihe/%s/edit", id)
return e.Redirect(http.StatusSeeOther, redirect)
}
}