@@ -8,8 +8,9 @@ import {initViewedCheckboxListenerFor, countAndUpdateViewedFiles, initExpandAndC
8
8
import { initImageDiff } from './imagediff.js' ;
9
9
import { showErrorToast } from '../modules/toast.js' ;
10
10
import { submitEventSubmitter } from '../utils/dom.js' ;
11
+ import { POST , GET } from '../modules/fetch.js' ;
11
12
12
- const { csrfToken , pageData, i18n} = window . config ;
13
+ const { pageData, i18n} = window . config ;
13
14
14
15
function initRepoDiffReviewButton ( ) {
15
16
const $reviewBox = $ ( '#review-box' ) ;
@@ -63,8 +64,9 @@ function initRepoDiffConversationForm() {
63
64
if ( isSubmittedByButton && submitter . name ) {
64
65
formData . append ( submitter . name , submitter . value ) ;
65
66
}
66
- const formDataString = String ( new URLSearchParams ( formData ) ) ;
67
- const $newConversationHolder = $ ( await $ . post ( $form . attr ( 'action' ) , formDataString ) ) ;
67
+
68
+ const response = await POST ( $form . attr ( 'action' ) , { data : formData } ) ;
69
+ const $newConversationHolder = $ ( await response . text ( ) ) ;
68
70
const { path, side, idx} = $newConversationHolder . data ( ) ;
69
71
70
72
$form . closest ( '.conversation-holder' ) . replaceWith ( $newConversationHolder ) ;
@@ -75,7 +77,8 @@ function initRepoDiffConversationForm() {
75
77
}
76
78
$newConversationHolder . find ( '.dropdown' ) . dropdown ( ) ;
77
79
initCompReactionSelector ( $newConversationHolder ) ;
78
- } catch { // here the caught error might be a jQuery AJAX error (thrown by await $.post), which is not good to use for error message handling
80
+ } catch ( error ) {
81
+ console . error ( 'Error:' , error ) ;
79
82
showErrorToast ( i18n . network_error ) ;
80
83
} finally {
81
84
$form . removeClass ( 'is-loading' ) ;
@@ -89,15 +92,20 @@ function initRepoDiffConversationForm() {
89
92
const action = $ ( this ) . data ( 'action' ) ;
90
93
const url = $ ( this ) . data ( 'update-url' ) ;
91
94
92
- const data = await $ . post ( url , { _csrf : csrfToken , origin, action, comment_id} ) ;
93
-
94
- if ( $ ( this ) . closest ( '.conversation-holder' ) . length ) {
95
- const conversation = $ ( data ) ;
96
- $ ( this ) . closest ( '.conversation-holder' ) . replaceWith ( conversation ) ;
97
- conversation . find ( '.dropdown' ) . dropdown ( ) ;
98
- initCompReactionSelector ( conversation ) ;
99
- } else {
100
- window . location . reload ( ) ;
95
+ try {
96
+ const response = await POST ( url , { data : new URLSearchParams ( { origin, action, comment_id} ) } ) ;
97
+ const data = await response . text ( ) ;
98
+
99
+ if ( $ ( this ) . closest ( '.conversation-holder' ) . length ) {
100
+ const conversation = $ ( data ) ;
101
+ $ ( this ) . closest ( '.conversation-holder' ) . replaceWith ( conversation ) ;
102
+ conversation . find ( '.dropdown' ) . dropdown ( ) ;
103
+ initCompReactionSelector ( conversation ) ;
104
+ } else {
105
+ window . location . reload ( ) ;
106
+ }
107
+ } catch ( error ) {
108
+ console . error ( 'Error:' , error ) ;
101
109
}
102
110
} ) ;
103
111
}
@@ -132,18 +140,18 @@ function onShowMoreFiles() {
132
140
initImageDiff ( ) ;
133
141
}
134
142
135
- export function loadMoreFiles ( url ) {
143
+ export async function loadMoreFiles ( url ) {
136
144
const $target = $ ( 'a#diff-show-more-files' ) ;
137
145
if ( $target . hasClass ( 'disabled' ) || pageData . diffFileInfo . isLoadingNewData ) {
138
146
return ;
139
147
}
140
148
141
149
pageData . diffFileInfo . isLoadingNewData = true ;
142
150
$target . addClass ( 'disabled' ) ;
143
- $ . ajax ( {
144
- type : 'GET' ,
145
- url,
146
- } ) . done ( ( resp ) => {
151
+
152
+ try {
153
+ const response = await GET ( url ) ;
154
+ const resp = await response . text ( ) ;
147
155
const $resp = $ ( resp ) ;
148
156
// the response is a full HTML page, we need to extract the relevant contents:
149
157
// 1. append the newly loaded file list items to the existing list
@@ -152,10 +160,13 @@ export function loadMoreFiles(url) {
152
160
$ ( 'body' ) . append ( $resp . find ( 'script#diff-data-script' ) ) ;
153
161
154
162
onShowMoreFiles ( ) ;
155
- } ) . always ( ( ) => {
163
+ } catch ( error ) {
164
+ console . error ( 'Error:' , error ) ;
165
+ showErrorToast ( 'An error occurred while loading more files.' ) ;
166
+ } finally {
156
167
$target . removeClass ( 'disabled' ) ;
157
168
pageData . diffFileInfo . isLoadingNewData = false ;
158
- } ) ;
169
+ }
159
170
}
160
171
161
172
function initRepoDiffShowMore ( ) {
@@ -167,7 +178,7 @@ function initRepoDiffShowMore() {
167
178
loadMoreFiles ( linkLoadMore ) ;
168
179
} ) ;
169
180
170
- $ ( document ) . on ( 'click' , 'a.diff-load-button' , ( e ) => {
181
+ $ ( document ) . on ( 'click' , 'a.diff-load-button' , async ( e ) => {
171
182
e . preventDefault ( ) ;
172
183
const $target = $ ( e . target ) ;
173
184
@@ -178,19 +189,21 @@ function initRepoDiffShowMore() {
178
189
$target . addClass ( 'disabled' ) ;
179
190
180
191
const url = $target . data ( 'href' ) ;
181
- $ . ajax ( {
182
- type : 'GET' ,
183
- url,
184
- } ) . done ( ( resp ) => {
192
+
193
+ try {
194
+ const response = await GET ( url ) ;
195
+ const resp = await response . text ( ) ;
196
+
185
197
if ( ! resp ) {
186
- $target . removeClass ( 'disabled' ) ;
187
198
return ;
188
199
}
189
200
$target . parent ( ) . replaceWith ( $ ( resp ) . find ( '#diff-file-boxes .diff-file-body .file-body' ) . children ( ) ) ;
190
201
onShowMoreFiles ( ) ;
191
- } ) . fail ( ( ) => {
202
+ } catch ( error ) {
203
+ console . error ( 'Error:' , error ) ;
204
+ } finally {
192
205
$target . removeClass ( 'disabled' ) ;
193
- } ) ;
206
+ }
194
207
} ) ;
195
208
}
196
209
0 commit comments