1
1
import $ from 'jquery' ;
2
+ import { GET } from '../modules/fetch.js' ;
2
3
3
- const { appSubUrl, csrfToken , notificationSettings, assetVersionEncoded} = window . config ;
4
+ const { appSubUrl, notificationSettings, assetVersionEncoded} = window . config ;
4
5
let notificationSequenceNumber = 0 ;
5
6
6
7
export function initNotificationsTable ( ) {
@@ -27,25 +28,6 @@ export function initNotificationsTable() {
27
28
e . target . closest ( '.notifications-item' ) . setAttribute ( 'data-remove' , 'true' ) ;
28
29
} ) ;
29
30
}
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
- } ) ;
49
31
}
50
32
51
33
async function receiveUpdateCount ( event ) {
@@ -163,58 +145,50 @@ async function updateNotificationCountWithCallback(callback, timeout, lastCount)
163
145
async function updateNotificationTable ( ) {
164
146
const notificationDiv = $ ( '#notification_div' ) ;
165
147
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' ) ;
172
157
}
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 ) ;
177
166
}
178
167
}
179
168
}
180
169
181
170
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` ) ;
189
173
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
+ }
196
177
197
- notificationCount . text ( ` ${ data . new } ` ) ;
178
+ const data = await response . json ( ) ;
198
179
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
+ }
201
186
202
- async function updateNotification ( url , status , page , q , notificationID ) {
203
- if ( status !== 'pinned' ) {
204
- $ ( `#notification_${ notificationID } ` ) . remove ( ) ;
205
- }
187
+ notificationCount . text ( `${ data . new } ` ) ;
206
188
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
+ }
220
194
}
0 commit comments