Add in mark as reaad button to list view, use ajax to preform the action without page refresh.
This commit is contained in:
@@ -7,6 +7,7 @@ package accountMessageHandler
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"database/sql"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
@@ -137,3 +138,36 @@ func (h *AccountMessageHandlers) ReadGet(c *gin.Context) {
|
||||
}
|
||||
c.Data(http.StatusOK, "text/html; charset=utf-8", buf.Bytes())
|
||||
}
|
||||
|
||||
func (h *AccountMessageHandlers) MarkReadPost(c *gin.Context) {
|
||||
app := c.MustGet("app").(*bootstrap.App)
|
||||
sm := app.SessionManager
|
||||
userID := mustUserID(c)
|
||||
|
||||
idStr := c.PostForm("id")
|
||||
id, err := strconv.ParseInt(idStr, 10, 64)
|
||||
if err != nil || id <= 0 {
|
||||
sm.Put(c.Request.Context(), "flash", "Invalid message id.")
|
||||
c.Redirect(http.StatusSeeOther, c.Request.Referer()) // back to where they came from
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.Svc.MarkRead(userID, id); err != nil {
|
||||
logging.Info("❌ MarkRead error: %v", err)
|
||||
if err == sql.ErrNoRows {
|
||||
sm.Put(c.Request.Context(), "flash", "Message not found or not permitted.")
|
||||
} else {
|
||||
sm.Put(c.Request.Context(), "flash", "Could not mark message as read.")
|
||||
}
|
||||
c.Redirect(http.StatusSeeOther, "/account/messages")
|
||||
return
|
||||
}
|
||||
|
||||
sm.Put(c.Request.Context(), "flash", "Message marked as read.")
|
||||
// Redirect back to referer when possible so UX is smooth.
|
||||
if ref := c.Request.Referer(); ref != "" {
|
||||
c.Redirect(http.StatusSeeOther, ref)
|
||||
} else {
|
||||
c.Redirect(http.StatusSeeOther, "/account/messages")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user