Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve notification pager #9821

Merged
merged 3 commits into from
Jan 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Improve notification pager
  • Loading branch information
lunny committed Jan 17, 2020
commit e77ffbf37ad9d4aea369f69ef9e245b00de34e76
22 changes: 14 additions & 8 deletions routers/user/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ func Notifications(c *context.Context) {
status = models.NotificationStatusUnread
}

total, err := models.GetNotificationCount(c.User, status)
if err != nil {
c.ServerError("ErrGetNotificationCount", err)
return
}

// redirect to last page if request page is more than total pages
pager := context.NewPagination(int(total), perPage, page, 5)
if pager.Paginater.Current() < page {
c.Redirect(fmt.Sprintf("/notifications?q=%s&page=%d", c.Query("q"), pager.Paginater.Current()))
return
}

statuses := []models.NotificationStatus{status, models.NotificationStatusPinned}
notifications, err := models.NotificationsForUser(c.User, statuses, page, perPage)
if err != nil {
Expand All @@ -87,12 +100,6 @@ func Notifications(c *context.Context) {
return
}

total, err := models.GetNotificationCount(c.User, status)
if err != nil {
c.ServerError("ErrGetNotificationCount", err)
return
}

title := c.Tr("notifications")
if status == models.NotificationStatusUnread && total > 0 {
title = fmt.Sprintf("(%d) %s", total, title)
Expand All @@ -102,7 +109,6 @@ func Notifications(c *context.Context) {
c.Data["Status"] = status
c.Data["Notifications"] = notifications

pager := context.NewPagination(int(total), perPage, page, 5)
pager.SetDefaultParams(c)
c.Data["Page"] = pager

Expand Down Expand Up @@ -134,7 +140,7 @@ func NotificationStatusPost(c *context.Context) {
return
}

url := fmt.Sprintf("%s/notifications", setting.AppSubURL)
url := fmt.Sprintf("%s/notifications?page=%s", setting.AppSubURL, c.Query("page"))
c.Redirect(url, 303)
}

Expand Down
2 changes: 2 additions & 0 deletions templates/user/notification/notification.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
{{$.CsrfTokenHtml}}
<input type="hidden" name="notification_id" value="{{$notification.ID}}" />
<input type="hidden" name="status" value="read" />
<input type="hidden" name="page" value="{{$.Page.Paginater.Current}}" />
<button class="ui mini button" title='{{$.i18n.Tr "notification.mark_as_read"}}'>
<i class="octicon octicon-check"></i>
</button>
Expand All @@ -97,6 +98,7 @@
{{$.CsrfTokenHtml}}
<input type="hidden" name="notification_id" value="{{$notification.ID}}" />
<input type="hidden" name="status" value="unread" />
<input type="hidden" name="page" value="{{$.Page.Paginater.Current}}" />
<button class="ui mini button" title='{{$.i18n.Tr "notification.mark_as_unread"}}'>
<i class="octicon octicon-bell"></i>
</button>
Expand Down