Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove jQuery AJAX from common global functions #29528

Merged
merged 2 commits into from
Mar 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions web_src/js/features/common-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {htmlEscape} from 'escape-goat';
import {showTemporaryTooltip} from '../modules/tippy.js';
import {confirmModal} from './comp/ConfirmModal.js';
import {showErrorToast} from '../modules/toast.js';
import {request, POST} from '../modules/fetch.js';
import {request, POST, GET} from '../modules/fetch.js';
import '../htmx.js';

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

export function initFootLanguageMenu() {
function linkLanguageAction() {
async function linkLanguageAction() {
const $this = $(this);
$.get($this.data('url')).always(() => {
window.location.reload();
});
await GET($this.data('url'));
Copy link
Contributor

@wxiaoguang wxiaoguang Jun 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not right to use GET here.

Now the language switch doesn't reload the page due to error: Uncaught (in promise) TypeError: NetworkError when attempting to fetch resource.


Update: maybe it is not really a problem. See below new comment.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to wrap it in try I suppose to replicate .always. Or find a better method that does not rely on throwing at all.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm ... it seems to be a misconfiguration problem on gitea.com.

If there is no misconfiguration, there wouldn't be a problem I guess.

The strange thing is that on gitea.com, the first request succeeds to change the language, but the second redirected response make the JS error.

@techknowlogick @lunny

image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The root URL of gitea.com does not go to the gitea instance, maybe that's the problem?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for reporting and I will investigate it.

window.location.reload();
}

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

dialog.modal({
closable: false,
onApprove() {
onApprove: async () => {
if ($this.data('type') === 'form') {
$($this.data('form')).trigger('submit');
return;
}

const postData = {
_csrf: csrfToken,
};
const postData = new FormData();
for (const [key, value] of Object.entries(dataArray)) {
if (key && key.startsWith('data')) {
postData[key.slice(4)] = value;
postData.append(key.slice(4), value);
}
if (key === 'id') {
postData['id'] = value;
postData.append('id', value);
}
}

$.post($this.data('url'), postData).done((data) => {
const response = await POST($this.data('url'), {data: postData});
if (response.ok) {
const data = await response.json();
window.location.href = data.redirect;
});
}
}
}).modal('show');
}
Expand Down
Loading