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

When updating notification table with AJAX only use latest data #16445

Merged
merged 2 commits into from
Jul 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions routers/web/user/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func Notifications(c *context.Context) {
return
}
if c.QueryBool("div-only") {
c.Data["SequenceNumber"] = c.Query("sequence-number")
c.HTML(http.StatusOK, tplNotificationDiv)
return
}
Expand Down Expand Up @@ -175,6 +176,7 @@ func NotificationStatusPost(c *context.Context) {
return
}
c.Data["Link"] = setting.AppURL + "notifications"
c.Data["SequenceNumber"] = c.Req.PostFormValue("sequence-number")

c.HTML(http.StatusOK, tplNotificationDiv)
}
Expand Down
2 changes: 1 addition & 1 deletion templates/user/notification/notification_div.tmpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="page-content user notification" id="notification_div" data-params="{{.Page.GetParams}}">
<div class="page-content user notification" id="notification_div" data-params="{{.Page.GetParams}}" data-sequence-number="{{.SequenceNumber}}">
<div class="ui container">
<h1 class="ui dividing header">{{.i18n.Tr "notification.notifications"}}</h1>
<div class="ui top attached tabular menu">
Expand Down
16 changes: 12 additions & 4 deletions web_src/js/features/notification.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const {AppSubUrl, csrf, NotificationSettings} = window.config;

let notificationSequenceNumber = 0;

export function initNotificationsTable() {
$('#notification_table .button').on('click', async function () {
const data = await updateNotification(
Expand All @@ -10,8 +12,10 @@ export function initNotificationsTable() {
$(this).data('notification-id'),
);

$('#notification_div').replaceWith(data);
initNotificationsTable();
if ($(data).data('sequence-number') === notificationSequenceNumber) {
$('#notification_div').replaceWith(data);
initNotificationsTable();
}
await updateNotificationCount();

return false;
Expand Down Expand Up @@ -139,10 +143,13 @@ async function updateNotificationTable() {
url: `${AppSubUrl}/notifications?${notificationDiv.data('params')}`,
data: {
'div-only': true,
'sequence-number': ++notificationSequenceNumber,
}
});
notificationDiv.replaceWith(data);
initNotificationsTable();
if ($(data).data('sequence-number') === notificationSequenceNumber) {
notificationDiv.replaceWith(data);
initNotificationsTable();
}
}
}

Expand Down Expand Up @@ -182,6 +189,7 @@ async function updateNotification(url, status, page, q, notificationID) {
page,
q,
noredirect: true,
'sequence-number': ++notificationSequenceNumber,
},
});
}