11import $ 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 ;
45let notificationSequenceNumber = 0 ;
56
67export 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
5133async function receiveUpdateCount ( event ) {
@@ -163,58 +145,50 @@ async function updateNotificationCountWithCallback(callback, timeout, lastCount)
163145async 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
181170async 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