From a236d9cea062bef64ccfe17377ae677adf89163c Mon Sep 17 00:00:00 2001 From: yequari Date: Sat, 3 Jan 2026 11:46:01 -0700 Subject: [PATCH] pagination in admin panel views --- cmd/web/handlers_admin.go | 77 ++++++--- internal/models/guestbookcomment.go | 43 +++++ internal/models/user.go | 22 +++ ui/views/admin.templ | 24 ++- ui/views/admin_templ.go | 244 ++++++++++++++++------------ ui/views/common.templ | 6 +- ui/views/common_templ.go | 6 +- 7 files changed, 279 insertions(+), 143 deletions(-) diff --git a/cmd/web/handlers_admin.go b/cmd/web/handlers_admin.go index ba47bf6..9b32fe8 100644 --- a/cmd/web/handlers_admin.go +++ b/cmd/web/handlers_admin.go @@ -39,13 +39,37 @@ func (app *application) getAdminPanelLanding(w http.ResponseWriter, r *http.Requ } func (app *application) getAdminPanelAllUsers(w http.ResponseWriter, r *http.Request) { - users, err := app.users.GetAll() + page := r.URL.Query().Get("page") + count := r.URL.Query().Get("count") + var pageNum int64 = 1 + var pageSize int64 = 5 + var err error + if page != "" { + pageNum, err = strconv.ParseInt(page, 10, 0) + if err != nil { + app.clientError(w, http.StatusBadRequest) + return + } + } + if count != "" { + pageSize, err = strconv.ParseInt(count, 10, 0) + if err != nil { + app.clientError(w, http.StatusBadRequest) + return + } + } + users, err := app.users.GetAllPage(pageNum, pageSize) + if err != nil { + app.serverError(w, r, err) + return + } + total, err := app.users.GetCount() if err != nil { app.serverError(w, r, err) return } data := app.newCommonData(r) - views.AdminPanelUsersView("All Users - Admin", data, users).Render(r.Context(), w) + views.AdminPanelUsersView("All Users - Admin", data, users, pageNum, pageSize, total).Render(r.Context(), w) } func (app *application) getAdminPanelUser(w http.ResponseWriter, r *http.Request) { @@ -209,40 +233,45 @@ func (app *application) getAdminPanelWebsites(w http.ResponseWriter, r *http.Req return } commonData := app.newCommonData(r) - views.AdminPanelAllWebsitesView("All websites", commonData, websites, pageNum, total).Render(r.Context(), w) + views.AdminPanelAllWebsitesView("All websites", commonData, websites, pageNum, pageSize, total).Render(r.Context(), w) } func (app *application) getAdminPanelWebsiteDetails(w http.ResponseWriter, r *http.Request) { slug := r.PathValue("id") - // page := r.URL.Query().Get("page") - // count := r.URL.Query().Get("count") - // var pageNum int64 = 1 - // var pageSize int64 = 5 - // var err error - // if page != "" { - // pageNum, err = strconv.ParseInt(page, 10, 0) - // if err != nil { - // app.clientError(w, http.StatusBadRequest) - // return - // } - // } - // if count != "" { - // pageSize, err = strconv.ParseInt(count, 10, 0) - // if err != nil { - // app.clientError(w, http.StatusBadRequest) - // return - // } - // } + page := r.URL.Query().Get("page") + count := r.URL.Query().Get("count") + var pageNum int64 = 1 + var pageSize int64 = 25 + var err error + if page != "" { + pageNum, err = strconv.ParseInt(page, 10, 0) + if err != nil { + app.clientError(w, http.StatusBadRequest) + return + } + } + if count != "" { + pageSize, err = strconv.ParseInt(count, 10, 0) + if err != nil { + app.clientError(w, http.StatusBadRequest) + return + } + } website, err := app.websites.Get(slugToShortId(slug)) if err != nil { app.serverError(w, r, err) return } - comments, err := app.guestbookComments.GetAll(website.Guestbook.ID) + total, err := app.guestbookComments.GetAllCount(website.Guestbook.ID) + if err != nil { + app.serverError(w, r, err) + return + } + comments, err := app.guestbookComments.GetAllPage(website.Guestbook.ID, pageNum, pageSize) if err != nil { app.serverError(w, r, err) return } commonData := app.newCommonData(r) - views.AdminPanelWebsiteDetailView(fmt.Sprintf("Admin - %s", website.Name), commonData, website, comments).Render(r.Context(), w) + views.AdminPanelWebsiteDetailView(fmt.Sprintf("Admin - %s", website.Name), commonData, website, comments, pageNum, pageSize, total).Render(r.Context(), w) } diff --git a/internal/models/guestbookcomment.go b/internal/models/guestbookcomment.go index 00ba1a4..a0431d1 100644 --- a/internal/models/guestbookcomment.go +++ b/internal/models/guestbookcomment.go @@ -38,6 +38,8 @@ type GuestbookCommentModelInterface interface { GetVisibleSerialized(guestbookId int64) ([]GuestbookCommentSerialized, error) GetDeleted(guestbookId int64) ([]GuestbookComment, error) GetAll(guestbookId int64) ([]GuestbookComment, error) + GetAllCount(guestbookId int64) (int64, error) + GetAllPage(guestbookId int64, pageNum int64, pageSize int64) ([]GuestbookComment, error) UpdateComment(comment *GuestbookComment) error } @@ -195,6 +197,47 @@ func (m *GuestbookCommentModel) GetAll(guestbookId int64) ([]GuestbookComment, e return comments, nil } +func (m *GuestbookCommentModel) GetAllCount(guestbookId int64) (int64, error) { + stmt := `SELECT COUNT(*) FROM guestbook_comments WHERE GuestbookId = ? AND Deleted IS NULL` + row := m.DB.QueryRow(stmt, guestbookId) + var result int64 + err := row.Scan(&result) + if err != nil { + return -1, err + } + if err = row.Err(); err != nil { + return -1, err + } + return result, nil +} + +func (m *GuestbookCommentModel) GetAllPage(guestbookId int64, pageNum int64, pageSize int64) ([]GuestbookComment, error) { + stmt := `SELECT Id, ShortId, GuestbookId, ParentId, AuthorName, AuthorEmail, AuthorSite, + CommentText, PageUrl, Created, IsPublished + FROM guestbook_comments + WHERE GuestbookId = ? AND Deleted IS NULL + ORDER BY Created DESC + LIMIT ? OFFSET ?` + rows, err := m.DB.Query(stmt, guestbookId, pageSize, (pageNum-1)*pageSize) + if err != nil { + return nil, err + } + var comments []GuestbookComment + for rows.Next() { + var c GuestbookComment + err = rows.Scan(&c.ID, &c.ShortId, &c.GuestbookId, &c.ParentId, &c.AuthorName, &c.AuthorEmail, &c.AuthorSite, + &c.CommentText, &c.PageUrl, &c.Created, &c.IsPublished) + if err != nil { + return nil, err + } + comments = append(comments, c) + } + if err = rows.Err(); err != nil { + return nil, err + } + return comments, nil +} + func (m *GuestbookCommentModel) UpdateComment(comment *GuestbookComment) error { stmt := `UPDATE guestbook_comments SET CommentText = ?, diff --git a/internal/models/user.go b/internal/models/user.go index a7b4e78..6643c62 100644 --- a/internal/models/user.go +++ b/internal/models/user.go @@ -61,6 +61,7 @@ type UserModelInterface interface { GetByEmail(email string) (int64, error) GetBySubject(subject string) (int64, error) GetAll() ([]User, error) + GetAllPage(pageNum, pageSize int64) ([]User, error) Authenticate(email, password string) (int64, error) Exists(id int64) (bool, error) UpdateUserSettings(userId int64, settings UserSettings) error @@ -328,6 +329,27 @@ func (m *UserModel) GetAll() ([]User, error) { return users, nil } +func (m *UserModel) GetAllPage(pageNum, pageSize int64) ([]User, error) { + stmt := `SELECT Id, ShortId, Username, Email, Created FROM users WHERE DELETED IS NULL LIMIT ? OFFSET ?` + rows, err := m.DB.Query(stmt, pageSize, (pageNum-1)*pageSize) + if err != nil { + return nil, err + } + var users []User + for rows.Next() { + var u User + err = rows.Scan(&u.ID, &u.ShortId, &u.Username, &u.Email, &u.Created) + if err != nil { + return nil, err + } + users = append(users, u) + } + if err = rows.Err(); err != nil { + return nil, err + } + return users, nil +} + func (m *UserModel) Authenticate(email, password string) (int64, error) { var id int64 var hashedPassword []byte diff --git a/ui/views/admin.templ b/ui/views/admin.templ index e94dad8..52c2b11 100644 --- a/ui/views/admin.templ +++ b/ui/views/admin.templ @@ -5,6 +5,7 @@ import ( "git.32bit.cafe/32bitcafe/guestbook/internal/forms" "git.32bit.cafe/32bitcafe/guestbook/internal/models" "slices" + "strings" "time" ) @@ -82,7 +83,7 @@ templ AdminPanelLandingView(title string, data CommonData, stats AdminStat) { } } -templ AdminPanelUsersView(title string, data CommonData, users []models.User) { +templ AdminPanelUsersView(title string, data CommonData, users []models.User, pageNum, pageSize, total int64) { @adminBase(title, data) {
@adminSidebar() @@ -102,6 +103,7 @@ templ AdminPanelUsersView(title string, data CommonData, users []models.User) { } + @pagination("/admin/users", pageNum, total, pageSize)
} @@ -203,7 +205,7 @@ templ AdminPanelUserMgmtEditForm(csrfToken string, form forms.AdminUserMgmtForm, } -templ AdminPanelAllWebsitesView(title string, data CommonData, websites []models.Website, pageNum int64, total int64) { +templ AdminPanelAllWebsitesView(title string, data CommonData, websites []models.Website, pageNum, pageCount, total int64) { @adminBase(title, data) {
@adminSidebar() @@ -228,13 +230,23 @@ templ AdminPanelAllWebsitesView(title string, data CommonData, websites []models } - @pagination("/admin/websites", pageNum, total, 5) + @pagination("/admin/websites", pageNum, total, pageCount)
} } -templ AdminPanelWebsiteDetailView(title string, data CommonData, website models.Website, comments []models.GuestbookComment) { +func truncateComment(s string, n int) string { + words := strings.Fields(s) + if len(words) < n { + return s + } + truncated := words[:n] + truncated = append(truncated, "...") + return strings.Join(truncated, " ") +} + +templ AdminPanelWebsiteDetailView(title string, data CommonData, website models.Website, comments []models.GuestbookComment, pageNum, pageAmount, total int64) { @adminBase(title, data) {
@adminSidebar() @@ -252,11 +264,13 @@ templ AdminPanelWebsiteDetailView(title string, data CommonData, website models. { c.Created.Format(time.RFC1123) } { c.AuthorEmail } { c.AuthorSite } - { c.CommentText } + { truncateComment(c.CommentText, 15) } } + {{ url := fmt.Sprintf("/admin/websites/%s", shortIdToSlug(website.ShortId)) }} + @pagination(url, pageNum, total, pageAmount)
} diff --git a/ui/views/admin_templ.go b/ui/views/admin_templ.go index a175b15..36617c9 100644 --- a/ui/views/admin_templ.go +++ b/ui/views/admin_templ.go @@ -13,6 +13,7 @@ import ( "git.32bit.cafe/32bitcafe/guestbook/internal/forms" "git.32bit.cafe/32bitcafe/guestbook/internal/models" "slices" + "strings" "time" ) @@ -50,7 +51,7 @@ func adminBase(title string, data CommonData) templ.Component { var templ_7745c5c3_Var2 string templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(title) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 21, Col: 17} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 22, Col: 17} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2)) if templ_7745c5c3_Err != nil { @@ -63,7 +64,7 @@ func adminBase(title string, data CommonData) templ.Component { var templ_7745c5c3_Var3 string templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(`{"includeIndicatorStyles":false}`) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 24, Col: 72} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 25, Col: 72} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3)) if templ_7745c5c3_Err != nil { @@ -170,7 +171,7 @@ func AdminPanelLandingView(title string, data CommonData, stats AdminStat) templ var templ_7745c5c3_Var7 string templ_7745c5c3_Var7, templ_7745c5c3_Err = templ.JoinStringErrs(title) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 63, Col: 16} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 64, Col: 16} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var7)) if templ_7745c5c3_Err != nil { @@ -183,7 +184,7 @@ func AdminPanelLandingView(title string, data CommonData, stats AdminStat) templ var templ_7745c5c3_Var8 string templ_7745c5c3_Var8, templ_7745c5c3_Err = templ.JoinStringErrs(fmt.Sprintf("%d", stats.UserCount)) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 69, Col: 45} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 70, Col: 45} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var8)) if templ_7745c5c3_Err != nil { @@ -196,7 +197,7 @@ func AdminPanelLandingView(title string, data CommonData, stats AdminStat) templ var templ_7745c5c3_Var9 string templ_7745c5c3_Var9, templ_7745c5c3_Err = templ.JoinStringErrs(fmt.Sprintf("%d", stats.WebsiteCount)) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 73, Col: 48} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 74, Col: 48} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var9)) if templ_7745c5c3_Err != nil { @@ -209,7 +210,7 @@ func AdminPanelLandingView(title string, data CommonData, stats AdminStat) templ var templ_7745c5c3_Var10 string templ_7745c5c3_Var10, templ_7745c5c3_Err = templ.JoinStringErrs(fmt.Sprintf("%d", stats.CommentCount)) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 77, Col: 48} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 78, Col: 48} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var10)) if templ_7745c5c3_Err != nil { @@ -229,7 +230,7 @@ func AdminPanelLandingView(title string, data CommonData, stats AdminStat) templ }) } -func AdminPanelUsersView(title string, data CommonData, users []models.User) templ.Component { +func AdminPanelUsersView(title string, data CommonData, users []models.User, pageNum, pageSize, total int64) templ.Component { return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil { @@ -296,7 +297,7 @@ func AdminPanelUsersView(title string, data CommonData, users []models.User) tem var templ_7745c5c3_Var14 string templ_7745c5c3_Var14, templ_7745c5c3_Err = templ.JoinStringErrs(u.Username) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 98, Col: 51} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 99, Col: 51} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var14)) if templ_7745c5c3_Err != nil { @@ -309,7 +310,7 @@ func AdminPanelUsersView(title string, data CommonData, users []models.User) tem var templ_7745c5c3_Var15 string templ_7745c5c3_Var15, templ_7745c5c3_Err = templ.JoinStringErrs(u.Created.Format(time.RFC3339)) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 99, Col: 44} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 100, Col: 44} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var15)) if templ_7745c5c3_Err != nil { @@ -322,7 +323,7 @@ func AdminPanelUsersView(title string, data CommonData, users []models.User) tem var templ_7745c5c3_Var16 string templ_7745c5c3_Var16, templ_7745c5c3_Err = templ.JoinStringErrs(u.Email) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 100, Col: 21} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 101, Col: 21} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var16)) if templ_7745c5c3_Err != nil { @@ -333,7 +334,15 @@ func AdminPanelUsersView(title string, data CommonData, users []models.User) tem return templ_7745c5c3_Err } } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 21, "") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 21, "") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = pagination("/admin/users", pageNum, total, pageSize).Render(ctx, templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 22, "") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -368,82 +377,82 @@ func AdminPanelUserMgmtDetail(csrfToken string, user models.User) templ.Componen templ_7745c5c3_Var17 = templ.NopComponent } ctx = templ.ClearChildren(ctx) - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 22, "

User Info

Username

") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 24, "\">

User Info

Username

") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } var templ_7745c5c3_Var19 string templ_7745c5c3_Var19, templ_7745c5c3_Err = templ.JoinStringErrs(user.Username) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 118, Col: 23} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 120, Col: 23} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var19)) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 24, "

Email

") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 25, "

Email

") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } var templ_7745c5c3_Var20 string templ_7745c5c3_Var20, templ_7745c5c3_Err = templ.JoinStringErrs(user.Email) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 122, Col: 20} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 124, Col: 20} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var20)) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 25, "

Joined

") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 26, "

Joined

") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } var templ_7745c5c3_Var21 string templ_7745c5c3_Var21, templ_7745c5c3_Err = templ.JoinStringErrs(user.Created.Format(time.RFC3339)) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 126, Col: 43} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 128, Col: 43} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var21)) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 26, "

Groups

    ") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 27, "

Groups

    ") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } for _, g := range user.Groups { - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 27, "
  • ") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 28, "
  • ") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } var templ_7745c5c3_Var22 string templ_7745c5c3_Var22, templ_7745c5c3_Err = templ.JoinStringErrs(fmt.Sprintf("%s", getGroupName(g))) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 133, Col: 46} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 135, Col: 46} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var22)) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 28, "
  • ") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 29, "") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 29, "

Actions

") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 30, "

Actions

") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -451,80 +460,80 @@ func AdminPanelUserMgmtDetail(csrfToken string, user models.User) templ.Componen putBanUrl := fmt.Sprintf("/admin/users/%s/ban", shortIdToSlug(user.ShortId)) putUnbanUrl := fmt.Sprintf("/admin/users/%s/unban", shortIdToSlug(user.ShortId)) deleteUrl := fmt.Sprintf("/admin/users/%s", shortIdToSlug(user.ShortId)) - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 30, " ") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 32, "\" hx-target=\"#user-info\">Edit ") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } if user.ID != 1 { if user.Banned.IsZero() { - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 32, "") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 34, "\" hx-confirm=\"Are you sure you want to ban this user?\" hx-target=\"#user-info\" class=\"danger\">Ban") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } } else { - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 34, "") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 36, "\" hx-confirm=\"Are you sure you want to unban this user?\" hx-target=\"#user-info\">Unban") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 36, " ") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 38, "\" hx-confirm=\"Are you sure you want to delete this user? This is irreversible\" class=\"danger\">Delete") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 38, "
") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 39, "") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -565,7 +574,7 @@ func AdminPanelUserMgmtView(title string, data CommonData, user models.User) tem }() } ctx = templ.InitializeContext(ctx) - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 39, "
") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 40, "
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -577,7 +586,7 @@ func AdminPanelUserMgmtView(title string, data CommonData, user models.User) tem if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 40, "
") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 41, "
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -612,112 +621,112 @@ func AdminPanelUserMgmtEditForm(csrfToken string, form forms.AdminUserMgmtForm, templ_7745c5c3_Var29 = templ.NopComponent } ctx = templ.ClearChildren(ctx) - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 41, "

User Info

Username

User Info

Username
Email
Email
Joined

") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 45, "\" required>

Joined

") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } var templ_7745c5c3_Var33 string templ_7745c5c3_Var33, templ_7745c5c3_Err = templ.JoinStringErrs(user.Created.Format(time.RFC3339)) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 182, Col: 43} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 184, Col: 43} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var33)) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 45, "

Groups

") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 46, "

Groups

") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } isAdmin := slices.Contains(user.Groups, models.AdminGroup) - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 46, "

Actions

") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 50, ">

Actions

") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } putFormUrl := fmt.Sprintf("/admin/users/%s/edit", shortIdToSlug(user.ShortId)) getDetailUrl := fmt.Sprintf("/admin/users/%s/detail", shortIdToSlug(user.ShortId)) - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 50, "
") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 53, "\" hx-target=\"#user-info\">Cancel") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -725,7 +734,7 @@ func AdminPanelUserMgmtEditForm(csrfToken string, form forms.AdminUserMgmtForm, }) } -func AdminPanelAllWebsitesView(title string, data CommonData, websites []models.Website, pageNum int64, total int64) templ.Component { +func AdminPanelAllWebsitesView(title string, data CommonData, websites []models.Website, pageNum, pageCount, total int64) templ.Component { return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil { @@ -758,7 +767,7 @@ func AdminPanelAllWebsitesView(title string, data CommonData, websites []models. }() } ctx = templ.InitializeContext(ctx) - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 53, "
") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 54, "
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -766,17 +775,17 @@ func AdminPanelAllWebsitesView(title string, data CommonData, websites []models. if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 54, "
") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 55, "
Site NameOwnerURLCreatedGuestbook
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } for _, w := range websites { - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 55, "") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 56, "") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } detailUrl := fmt.Sprintf("/admin/websites/%s", shortIdToSlug(w.ShortId)) - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 56, "") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 63, "") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } gbUrl := fmt.Sprintf("/websites/%s/guestbook", shortIdToSlug(w.ShortId)) - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 63, "") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 65, "\">View") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 65, "
Site NameOwnerURLCreatedGuestbook
") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 58, "\">") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } var templ_7745c5c3_Var39 string templ_7745c5c3_Var39, templ_7745c5c3_Err = templ.JoinStringErrs(w.Name) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 221, Col: 57} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 223, Col: 57} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var39)) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 58, "") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 59, "") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } var templ_7745c5c3_Var40 string templ_7745c5c3_Var40, templ_7745c5c3_Err = templ.JoinStringErrs(w.AuthorName) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 222, Col: 26} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 224, Col: 26} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var40)) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 59, "") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 61, "\">") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } var templ_7745c5c3_Var42 string templ_7745c5c3_Var42, templ_7745c5c3_Err = templ.JoinStringErrs(w.Url.String()) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 223, Col: 70} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 225, Col: 70} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var42)) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 61, "") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 62, "") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } var templ_7745c5c3_Var43 string templ_7745c5c3_Var43, templ_7745c5c3_Err = templ.JoinStringErrs(w.Created.Format(time.RFC1123)) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 224, Col: 44} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 226, Col: 44} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var43)) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 62, "View
") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 66, "") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = pagination("/admin/websites", pageNum, total, 5).Render(ctx, templ_7745c5c3_Buffer) + templ_7745c5c3_Err = pagination("/admin/websites", pageNum, total, pageCount).Render(ctx, templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 66, "
") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 67, "
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -887,7 +896,17 @@ func AdminPanelAllWebsitesView(title string, data CommonData, websites []models. }) } -func AdminPanelWebsiteDetailView(title string, data CommonData, website models.Website, comments []models.GuestbookComment) templ.Component { +func truncateComment(s string, n int) string { + words := strings.Fields(s) + if len(words) < n { + return s + } + truncated := words[:n] + truncated = append(truncated, "...") + return strings.Join(truncated, " ") +} + +func AdminPanelWebsiteDetailView(title string, data CommonData, website models.Website, comments []models.GuestbookComment, pageNum, pageAmount, total int64) templ.Component { return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil { @@ -920,7 +939,7 @@ func AdminPanelWebsiteDetailView(title string, data CommonData, website models.W }() } ctx = templ.InitializeContext(ctx) - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 67, "
") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 68, "
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -928,47 +947,34 @@ func AdminPanelWebsiteDetailView(title string, data CommonData, website models.W if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 68, "
") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 69, "
AuthorCreatedEmailHomepageComment
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } for _, c := range comments { - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 69, "") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 75, "") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 75, "
AuthorCreatedEmailHomepageComment
") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 70, "
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } var templ_7745c5c3_Var47 string templ_7745c5c3_Var47, templ_7745c5c3_Err = templ.JoinStringErrs(c.AuthorName) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 251, Col: 26} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 263, Col: 26} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var47)) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 70, "") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - var templ_7745c5c3_Var48 string - templ_7745c5c3_Var48, templ_7745c5c3_Err = templ.JoinStringErrs(c.Created.Format(time.RFC1123)) - if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 252, Col: 44} - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var48)) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 71, "") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - var templ_7745c5c3_Var49 string - templ_7745c5c3_Var49, templ_7745c5c3_Err = templ.JoinStringErrs(c.AuthorEmail) + var templ_7745c5c3_Var48 string + templ_7745c5c3_Var48, templ_7745c5c3_Err = templ.JoinStringErrs(c.Created.Format(time.RFC1123)) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 253, Col: 27} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 264, Col: 44} } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var49)) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var48)) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -976,12 +982,12 @@ func AdminPanelWebsiteDetailView(title string, data CommonData, website models.W if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - var templ_7745c5c3_Var50 string - templ_7745c5c3_Var50, templ_7745c5c3_Err = templ.JoinStringErrs(c.AuthorSite) + var templ_7745c5c3_Var49 string + templ_7745c5c3_Var49, templ_7745c5c3_Err = templ.JoinStringErrs(c.AuthorEmail) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 254, Col: 26} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 265, Col: 27} } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var50)) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var49)) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -989,21 +995,43 @@ func AdminPanelWebsiteDetailView(title string, data CommonData, website models.W if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - var templ_7745c5c3_Var51 string - templ_7745c5c3_Var51, templ_7745c5c3_Err = templ.JoinStringErrs(c.CommentText) + var templ_7745c5c3_Var50 string + templ_7745c5c3_Var50, templ_7745c5c3_Err = templ.JoinStringErrs(c.AuthorSite) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 255, Col: 27} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 266, Col: 26} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var50)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 74, "") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var51 string + templ_7745c5c3_Var51, templ_7745c5c3_Err = templ.JoinStringErrs(truncateComment(c.CommentText, 15)) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/admin.templ`, Line: 267, Col: 48} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var51)) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 74, "
") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 76, "") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + url := fmt.Sprintf("/admin/websites/%s", shortIdToSlug(website.ShortId)) + templ_7745c5c3_Err = pagination(url, pageNum, total, pageAmount).Render(ctx, templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 77, "
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } diff --git a/ui/views/common.templ b/ui/views/common.templ index 3fd38c5..c0463fb 100644 --- a/ui/views/common.templ +++ b/ui/views/common.templ @@ -124,7 +124,7 @@ templ pagination(baseUrl string, currentPage int64, recordsAmount int64, records