Skip to content

Commit 043f55f

Browse files
Remove jQuery AJAX from the notifications (#29817)
- Removed 2 jQuery AJAX calls and replaced with our fetch wrapper - Deleted an AJAX call that wasn't attached to any element since #24989 - Tested the notification count and notification table functionality and it works as before # Demo using `fetch` instead of jQuery AJAX ![demo](https://github.com/go-gitea/gitea/assets/20454870/ff862a9a-1c88-41cc-bd01-5a0711dbd6f8) --------- Signed-off-by: Yarden Shoham <git@yardenshoham.com> Co-authored-by: silverwind <me@silverwind.io>
1 parent 6816913 commit 043f55f

File tree

1 file changed

+37
-63
lines changed

1 file changed

+37
-63
lines changed

web_src/js/features/notification.js

+37-63
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import $ from 'jquery';
2+
import {GET} from '../modules/fetch.js';
23

3-
const {appSubUrl, csrfToken, notificationSettings, assetVersionEncoded} = window.config;
4+
const {appSubUrl, notificationSettings, assetVersionEncoded} = window.config;
45
let notificationSequenceNumber = 0;
56

67
export function initNotificationsTable() {
@@ -27,25 +28,6 @@ export function initNotificationsTable() {
2728
e.target.closest('.notifications-item').setAttribute('data-remove', 'true');
2829
});
2930
}
30-
31-
$('#notification_table .button').on('click', function () {
32-
(async () => {
33-
const data = await updateNotification(
34-
$(this).data('url'),
35-
$(this).data('status'),
36-
$(this).data('page'),
37-
$(this).data('q'),
38-
$(this).data('notification-id'),
39-
);
40-
41-
if ($(data).data('sequence-number') === notificationSequenceNumber) {
42-
$('#notification_div').replaceWith(data);
43-
initNotificationsTable();
44-
}
45-
await updateNotificationCount();
46-
})();
47-
return false;
48-
});
4931
}
5032

5133
async function receiveUpdateCount(event) {
@@ -163,58 +145,50 @@ async function updateNotificationCountWithCallback(callback, timeout, lastCount)
163145
async function updateNotificationTable() {
164146
const notificationDiv = $('#notification_div');
165147
if (notificationDiv.length > 0) {
166-
const data = await $.ajax({
167-
type: 'GET',
168-
url: `${appSubUrl}/notifications${window.location.search}`,
169-
data: {
170-
'div-only': true,
171-
'sequence-number': ++notificationSequenceNumber,
148+
try {
149+
const params = new URLSearchParams(window.location.search);
150+
params.set('div-only', true);
151+
params.set('sequence-number', ++notificationSequenceNumber);
152+
const url = `${appSubUrl}/notifications?${params.toString()}`;
153+
const response = await GET(url);
154+
155+
if (!response.ok) {
156+
throw new Error('Failed to fetch notification table');
172157
}
173-
});
174-
if ($(data).data('sequence-number') === notificationSequenceNumber) {
175-
notificationDiv.replaceWith(data);
176-
initNotificationsTable();
158+
159+
const data = await response.text();
160+
if ($(data).data('sequence-number') === notificationSequenceNumber) {
161+
notificationDiv.replaceWith(data);
162+
initNotificationsTable();
163+
}
164+
} catch (error) {
165+
console.error(error);
177166
}
178167
}
179168
}
180169

181170
async function updateNotificationCount() {
182-
const data = await $.ajax({
183-
type: 'GET',
184-
url: `${appSubUrl}/notifications/new`,
185-
headers: {
186-
'X-Csrf-Token': csrfToken,
187-
},
188-
});
171+
try {
172+
const response = await GET(`${appSubUrl}/notifications/new`);
189173

190-
const notificationCount = $('.notification_count');
191-
if (data.new === 0) {
192-
notificationCount.addClass('gt-hidden');
193-
} else {
194-
notificationCount.removeClass('gt-hidden');
195-
}
174+
if (!response.ok) {
175+
throw new Error('Failed to fetch notification count');
176+
}
196177

197-
notificationCount.text(`${data.new}`);
178+
const data = await response.json();
198179

199-
return `${data.new}`;
200-
}
180+
const notificationCount = $('.notification_count');
181+
if (data.new === 0) {
182+
notificationCount.addClass('gt-hidden');
183+
} else {
184+
notificationCount.removeClass('gt-hidden');
185+
}
201186

202-
async function updateNotification(url, status, page, q, notificationID) {
203-
if (status !== 'pinned') {
204-
$(`#notification_${notificationID}`).remove();
205-
}
187+
notificationCount.text(`${data.new}`);
206188

207-
return $.ajax({
208-
type: 'POST',
209-
url,
210-
data: {
211-
_csrf: csrfToken,
212-
notification_id: notificationID,
213-
status,
214-
page,
215-
q,
216-
noredirect: true,
217-
'sequence-number': ++notificationSequenceNumber,
218-
},
219-
});
189+
return `${data.new}`;
190+
} catch (error) {
191+
console.error(error);
192+
return '0';
193+
}
220194
}

0 commit comments

Comments
 (0)