Skip to content

Commit bfb0a5a

Browse files
Remove jQuery AJAX from the comment edit box (#29812)
- Removed all jQuery AJAX calls and replaced with our fetch wrapper - Tested the file addition and removal functionality and it works as before # Demo using `fetch` instead of jQuery AJAX ![demo](https://github.com/go-gitea/gitea/assets/20454870/846ed6d5-3798-43ca-920c-d619e9c3d745) --------- Signed-off-by: Yarden Shoham <git@yardenshoham.com> Co-authored-by: silverwind <me@silverwind.io>
1 parent 6692894 commit bfb0a5a

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

web_src/js/features/repo-legacy.js

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {initRepoPullRequestCommitStatus} from './repo-issue-pr-status.js';
2424
import {hideElem, showElem} from '../utils/dom.js';
2525
import {getComboMarkdownEditor, initComboMarkdownEditor} from './comp/ComboMarkdownEditor.js';
2626
import {attachRefIssueContextPopup} from './contextpopup.js';
27-
import {POST} from '../modules/fetch.js';
27+
import {POST, GET} from '../modules/fetch.js';
2828

2929
const {csrfToken} = window.config;
3030

@@ -83,7 +83,7 @@ export function initRepoCommentForm() {
8383
await POST(form.attr('action'), {data: params});
8484
window.location.reload();
8585
} catch (error) {
86-
console.error('Error:', error);
86+
console.error(error);
8787
}
8888
} else if (editMode === '') {
8989
$selectBranch.find('.ui .branch-name').text(selectedValue);
@@ -355,23 +355,26 @@ async function onEditContent(event) {
355355
const input = $(`<input id="${data.uuid}" name="files" type="hidden">`).val(data.uuid);
356356
$dropzone.find('.files').append(input);
357357
});
358-
this.on('removedfile', (file) => {
358+
this.on('removedfile', async (file) => {
359359
if (disableRemovedfileEvent) return;
360360
$(`#${file.uuid}`).remove();
361361
if ($dropzone.attr('data-remove-url') && !fileUuidDict[file.uuid].submitted) {
362-
$.post($dropzone.attr('data-remove-url'), {
363-
file: file.uuid,
364-
_csrf: csrfToken,
365-
});
362+
try {
363+
await POST($dropzone.attr('data-remove-url'), {data: new URLSearchParams({file: file.uuid})});
364+
} catch (error) {
365+
console.error(error);
366+
}
366367
}
367368
});
368369
this.on('submit', () => {
369370
$.each(fileUuidDict, (fileUuid) => {
370371
fileUuidDict[fileUuid].submitted = true;
371372
});
372373
});
373-
this.on('reload', () => {
374-
$.getJSON($editContentZone.attr('data-attachment-url'), (data) => {
374+
this.on('reload', async () => {
375+
try {
376+
const response = await GET($editContentZone.attr('data-attachment-url'));
377+
const data = await response.json();
375378
// do not trigger the "removedfile" event, otherwise the attachments would be deleted from server
376379
disableRemovedfileEvent = true;
377380
dz.removeAllFiles(true);
@@ -390,7 +393,9 @@ async function onEditContent(event) {
390393
const input = $(`<input id="${attachment.uuid}" name="files" type="hidden">`).val(attachment.uuid);
391394
$dropzone.find('.files').append(input);
392395
}
393-
});
396+
} catch (error) {
397+
console.error(error);
398+
}
394399
});
395400
},
396401
});
@@ -406,22 +411,25 @@ async function onEditContent(event) {
406411
}
407412
};
408413

409-
const saveAndRefresh = (dz) => {
414+
const saveAndRefresh = async (dz) => {
410415
showElem($renderContent);
411416
hideElem($editContentZone);
412-
$.post($editContentZone.attr('data-update-url'), {
413-
_csrf: csrfToken,
414-
content: comboMarkdownEditor.value(),
415-
context: $editContentZone.attr('data-context'),
416-
files: dz.files.map((file) => file.uuid),
417-
}, (data) => {
417+
418+
try {
419+
const params = new URLSearchParams({
420+
content: comboMarkdownEditor.value(),
421+
context: $editContentZone.attr('data-context'),
422+
});
423+
for (const file of dz.files) params.append('files[]', file.uuid);
424+
425+
const response = await POST($editContentZone.attr('data-update-url'), {data: params});
426+
const data = await response.json();
418427
if (!data.content) {
419428
$renderContent.html($('#no-content').html());
420429
$rawContent.text('');
421430
} else {
422431
$renderContent.html(data.content);
423432
$rawContent.text(comboMarkdownEditor.value());
424-
425433
const refIssues = $renderContent.find('p .ref-issue');
426434
attachRefIssueContextPopup(refIssues);
427435
}
@@ -442,7 +450,9 @@ async function onEditContent(event) {
442450
}
443451
initMarkupContent();
444452
initCommentContent();
445-
});
453+
} catch (error) {
454+
console.error(error);
455+
}
446456
};
447457

448458
if (!$editContentZone.html()) {

0 commit comments

Comments
 (0)