Skip to content

Commit aff0c56

Browse files
committed
Remove jQuery AJAX from the diff functions
- Removed all jQuery AJAX calls and replaced with our fetch wrapper - Tested the review conversation comment, resolve, unresolve, show more files, and load diff functionality and it works as before Signed-off-by: Yarden Shoham <[email protected]>
1 parent 36de5b2 commit aff0c56

File tree

1 file changed

+45
-26
lines changed

1 file changed

+45
-26
lines changed

web_src/js/features/repo-diff.js

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import {initViewedCheckboxListenerFor, countAndUpdateViewedFiles, initExpandAndC
88
import {initImageDiff} from './imagediff.js';
99
import {showErrorToast} from '../modules/toast.js';
1010
import {submitEventSubmitter} from '../utils/dom.js';
11+
import {POST, GET} from '../modules/fetch.js';
1112

12-
const {csrfToken, pageData, i18n} = window.config;
13+
const {pageData, i18n} = window.config;
1314

1415
function initRepoDiffReviewButton() {
1516
const $reviewBox = $('#review-box');
@@ -63,8 +64,9 @@ function initRepoDiffConversationForm() {
6364
if (isSubmittedByButton && submitter.name) {
6465
formData.append(submitter.name, submitter.value);
6566
}
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());
6870
const {path, side, idx} = $newConversationHolder.data();
6971

7072
$form.closest('.conversation-holder').replaceWith($newConversationHolder);
@@ -75,7 +77,8 @@ function initRepoDiffConversationForm() {
7577
}
7678
$newConversationHolder.find('.dropdown').dropdown();
7779
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);
7982
showErrorToast(i18n.network_error);
8083
} finally {
8184
$form.removeClass('is-loading');
@@ -89,15 +92,25 @@ function initRepoDiffConversationForm() {
8992
const action = $(this).data('action');
9093
const url = $(this).data('update-url');
9194

92-
const data = await $.post(url, {_csrf: csrfToken, origin, action, comment_id});
95+
const params = new URLSearchParams();
96+
params.append('origin', origin);
97+
params.append('action', action);
98+
params.append('comment_id', comment_id);
9399

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();
100+
try {
101+
const response = await POST(url, {data: params});
102+
const data = await response.text();
103+
104+
if ($(this).closest('.conversation-holder').length) {
105+
const conversation = $(data);
106+
$(this).closest('.conversation-holder').replaceWith(conversation);
107+
conversation.find('.dropdown').dropdown();
108+
initCompReactionSelector(conversation);
109+
} else {
110+
window.location.reload();
111+
}
112+
} catch (error) {
113+
console.error('Error:', error);
101114
}
102115
});
103116
}
@@ -132,18 +145,18 @@ function onShowMoreFiles() {
132145
initImageDiff();
133146
}
134147

135-
export function loadMoreFiles(url) {
148+
export async function loadMoreFiles(url) {
136149
const $target = $('a#diff-show-more-files');
137150
if ($target.hasClass('disabled') || pageData.diffFileInfo.isLoadingNewData) {
138151
return;
139152
}
140153

141154
pageData.diffFileInfo.isLoadingNewData = true;
142155
$target.addClass('disabled');
143-
$.ajax({
144-
type: 'GET',
145-
url,
146-
}).done((resp) => {
156+
157+
try {
158+
const response = await GET(url);
159+
const resp = await response.text();
147160
const $resp = $(resp);
148161
// the response is a full HTML page, we need to extract the relevant contents:
149162
// 1. append the newly loaded file list items to the existing list
@@ -152,10 +165,13 @@ export function loadMoreFiles(url) {
152165
$('body').append($resp.find('script#diff-data-script'));
153166

154167
onShowMoreFiles();
155-
}).always(() => {
168+
} catch (error) {
169+
console.error('Error:', error);
170+
showErrorToast('An error occurred while loading more files.');
171+
} finally {
156172
$target.removeClass('disabled');
157173
pageData.diffFileInfo.isLoadingNewData = false;
158-
});
174+
}
159175
}
160176

161177
function initRepoDiffShowMore() {
@@ -167,7 +183,7 @@ function initRepoDiffShowMore() {
167183
loadMoreFiles(linkLoadMore);
168184
});
169185

170-
$(document).on('click', 'a.diff-load-button', (e) => {
186+
$(document).on('click', 'a.diff-load-button', async (e) => {
171187
e.preventDefault();
172188
const $target = $(e.target);
173189

@@ -178,19 +194,22 @@ function initRepoDiffShowMore() {
178194
$target.addClass('disabled');
179195

180196
const url = $target.data('href');
181-
$.ajax({
182-
type: 'GET',
183-
url,
184-
}).done((resp) => {
197+
198+
try {
199+
const response = await GET(url);
200+
const resp = await response.text();
201+
185202
if (!resp) {
186203
$target.removeClass('disabled');
187204
return;
188205
}
189206
$target.parent().replaceWith($(resp).find('#diff-file-boxes .diff-file-body .file-body').children());
190207
onShowMoreFiles();
191-
}).fail(() => {
208+
} catch (error) {
209+
console.error('Error:', error);
210+
} finally {
192211
$target.removeClass('disabled');
193-
});
212+
}
194213
});
195214
}
196215

0 commit comments

Comments
 (0)