Skip to content

Commit 8a0a83a

Browse files
authored
Remove jQuery AJAX from common global functions (#29528)
- Removed all jQuery AJAX calls and replaced with our fetch wrapper - Tested the locale change functionality and it works as before - Tested the delete button functionality and it works as before # Demo using `fetch` instead of jQuery AJAX ![action](https://github.com/go-gitea/gitea/assets/20454870/8a024f75-c2a5-4bff-898d-ca751d2489f1) Signed-off-by: Yarden Shoham <[email protected]>
1 parent 9de5e39 commit 8a0a83a

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

web_src/js/features/common-global.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {htmlEscape} from 'escape-goat';
1111
import {showTemporaryTooltip} from '../modules/tippy.js';
1212
import {confirmModal} from './comp/ConfirmModal.js';
1313
import {showErrorToast} from '../modules/toast.js';
14-
import {request, POST} from '../modules/fetch.js';
14+
import {request, POST, GET} from '../modules/fetch.js';
1515
import '../htmx.js';
1616

1717
const {appUrl, appSubUrl, csrfToken, i18n} = window.config;
@@ -37,11 +37,10 @@ export function initHeadNavbarContentToggle() {
3737
}
3838

3939
export function initFootLanguageMenu() {
40-
function linkLanguageAction() {
40+
async function linkLanguageAction() {
4141
const $this = $(this);
42-
$.get($this.data('url')).always(() => {
43-
window.location.reload();
44-
});
42+
await GET($this.data('url'));
43+
window.location.reload();
4544
}
4645

4746
$('.language-menu a[lang]').on('click', linkLanguageAction);
@@ -309,27 +308,26 @@ export function initGlobalLinkActions() {
309308

310309
dialog.modal({
311310
closable: false,
312-
onApprove() {
311+
onApprove: async () => {
313312
if ($this.data('type') === 'form') {
314313
$($this.data('form')).trigger('submit');
315314
return;
316315
}
317-
318-
const postData = {
319-
_csrf: csrfToken,
320-
};
316+
const postData = new FormData();
321317
for (const [key, value] of Object.entries(dataArray)) {
322318
if (key && key.startsWith('data')) {
323-
postData[key.slice(4)] = value;
319+
postData.append(key.slice(4), value);
324320
}
325321
if (key === 'id') {
326-
postData['id'] = value;
322+
postData.append('id', value);
327323
}
328324
}
329325

330-
$.post($this.data('url'), postData).done((data) => {
326+
const response = await POST($this.data('url'), {data: postData});
327+
if (response.ok) {
328+
const data = await response.json();
331329
window.location.href = data.redirect;
332-
});
330+
}
333331
}
334332
}).modal('show');
335333
}

0 commit comments

Comments
 (0)