2024-07-07 09:32:30 -06:00
|
|
|
import {request} from '../modules/fetch.ts';
|
|
|
|
import {showErrorToast} from '../modules/toast.ts';
|
2024-11-26 08:36:55 -07:00
|
|
|
import {addDelegatedEventListener, submitEventSubmitter} from '../utils/dom.ts';
|
2024-07-07 09:32:30 -06:00
|
|
|
import {confirmModal} from './comp/ConfirmModal.ts';
|
2024-11-26 08:36:55 -07:00
|
|
|
import type {RequestOpts} from '../types.ts';
|
2024-06-21 01:40:33 -06:00
|
|
|
|
|
|
|
const {appSubUrl, i18n} = window.config;
|
|
|
|
|
|
|
|
// fetchActionDoRedirect does real redirection to bypass the browser's limitations of "location"
|
|
|
|
// more details are in the backend's fetch-redirect handler
|
2024-11-26 08:36:55 -07:00
|
|
|
function fetchActionDoRedirect(redirect: string) {
|
2024-06-21 01:40:33 -06:00
|
|
|
const form = document.createElement('form');
|
|
|
|
const input = document.createElement('input');
|
|
|
|
form.method = 'post';
|
|
|
|
form.action = `${appSubUrl}/-/fetch-redirect`;
|
|
|
|
input.type = 'hidden';
|
|
|
|
input.name = 'redirect';
|
|
|
|
input.value = redirect;
|
|
|
|
form.append(input);
|
|
|
|
document.body.append(form);
|
|
|
|
form.submit();
|
|
|
|
}
|
|
|
|
|
2024-11-26 08:36:55 -07:00
|
|
|
async function fetchActionDoRequest(actionElem: HTMLElement, url: string, opt: RequestOpts) {
|
2024-06-21 01:40:33 -06:00
|
|
|
try {
|
|
|
|
const resp = await request(url, opt);
|
|
|
|
if (resp.status === 200) {
|
|
|
|
let {redirect} = await resp.json();
|
|
|
|
redirect = redirect || actionElem.getAttribute('data-redirect');
|
|
|
|
actionElem.classList.remove('dirty'); // remove the areYouSure check before reloading
|
|
|
|
if (redirect) {
|
|
|
|
fetchActionDoRedirect(redirect);
|
|
|
|
} else {
|
|
|
|
window.location.reload();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
} else if (resp.status >= 400 && resp.status < 500) {
|
|
|
|
const data = await resp.json();
|
|
|
|
// the code was quite messy, sometimes the backend uses "err", sometimes it uses "error", and even "user_error"
|
|
|
|
// but at the moment, as a new approach, we only use "errorMessage" here, backend can use JSONError() to respond.
|
|
|
|
if (data.errorMessage) {
|
|
|
|
showErrorToast(data.errorMessage, {useHtmlBody: data.renderFormat === 'html'});
|
|
|
|
} else {
|
|
|
|
showErrorToast(`server error: ${resp.status}`);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
showErrorToast(`server error: ${resp.status}`);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
if (e.name !== 'AbortError') {
|
|
|
|
console.error('error when doRequest', e);
|
|
|
|
showErrorToast(`${i18n.network_error} ${e}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
actionElem.classList.remove('is-loading', 'loading-icon-2px');
|
|
|
|
}
|
|
|
|
|
2024-11-26 08:36:55 -07:00
|
|
|
async function formFetchAction(formEl: HTMLFormElement, e: SubmitEvent) {
|
2024-06-21 01:40:33 -06:00
|
|
|
e.preventDefault();
|
|
|
|
if (formEl.classList.contains('is-loading')) return;
|
|
|
|
|
|
|
|
formEl.classList.add('is-loading');
|
|
|
|
if (formEl.clientHeight < 50) {
|
|
|
|
formEl.classList.add('loading-icon-2px');
|
|
|
|
}
|
|
|
|
|
|
|
|
const formMethod = formEl.getAttribute('method') || 'get';
|
|
|
|
const formActionUrl = formEl.getAttribute('action');
|
|
|
|
const formData = new FormData(formEl);
|
|
|
|
const formSubmitter = submitEventSubmitter(e);
|
|
|
|
const [submitterName, submitterValue] = [formSubmitter?.getAttribute('name'), formSubmitter?.getAttribute('value')];
|
|
|
|
if (submitterName) {
|
|
|
|
formData.append(submitterName, submitterValue || '');
|
|
|
|
}
|
|
|
|
|
|
|
|
let reqUrl = formActionUrl;
|
2024-11-26 08:36:55 -07:00
|
|
|
const reqOpt = {method: formMethod.toUpperCase(), body: null};
|
2024-06-21 01:40:33 -06:00
|
|
|
if (formMethod.toLowerCase() === 'get') {
|
|
|
|
const params = new URLSearchParams();
|
|
|
|
for (const [key, value] of formData) {
|
|
|
|
params.append(key, value.toString());
|
|
|
|
}
|
|
|
|
const pos = reqUrl.indexOf('?');
|
|
|
|
if (pos !== -1) {
|
|
|
|
reqUrl = reqUrl.slice(0, pos);
|
|
|
|
}
|
|
|
|
reqUrl += `?${params.toString()}`;
|
|
|
|
} else {
|
|
|
|
reqOpt.body = formData;
|
|
|
|
}
|
|
|
|
|
|
|
|
await fetchActionDoRequest(formEl, reqUrl, reqOpt);
|
|
|
|
}
|
|
|
|
|
2024-11-26 08:36:55 -07:00
|
|
|
async function linkAction(el: HTMLElement, e: Event) {
|
2024-06-21 01:40:33 -06:00
|
|
|
// A "link-action" can post AJAX request to its "data-url"
|
|
|
|
// Then the browser is redirected to: the "redirect" in response, or "data-redirect" attribute, or current URL by reloading.
|
|
|
|
// If the "link-action" has "data-modal-confirm" attribute, a confirm modal dialog will be shown before taking action.
|
|
|
|
e.preventDefault();
|
|
|
|
const url = el.getAttribute('data-url');
|
|
|
|
const doRequest = async () => {
|
2024-11-26 08:36:55 -07:00
|
|
|
if ('disabled' in el) el.disabled = true; // el could be A or BUTTON, but A doesn't have disabled attribute
|
2024-06-21 01:40:33 -06:00
|
|
|
await fetchActionDoRequest(el, url, {method: 'POST'});
|
2024-11-26 08:36:55 -07:00
|
|
|
if ('disabled' in el) el.disabled = false;
|
2024-06-21 01:40:33 -06:00
|
|
|
};
|
|
|
|
|
2024-11-26 08:36:55 -07:00
|
|
|
const modalConfirmContent = el.getAttribute('data-modal-confirm') ||
|
|
|
|
el.getAttribute('data-modal-confirm-content') || '';
|
2024-06-21 01:40:33 -06:00
|
|
|
if (!modalConfirmContent) {
|
|
|
|
await doRequest();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const isRisky = el.classList.contains('red') || el.classList.contains('negative');
|
2024-11-26 08:36:55 -07:00
|
|
|
if (await confirmModal({
|
|
|
|
header: el.getAttribute('data-modal-confirm-header') || '',
|
|
|
|
content: modalConfirmContent,
|
|
|
|
confirmButtonColor: isRisky ? 'red' : 'primary',
|
|
|
|
})) {
|
2024-06-21 01:40:33 -06:00
|
|
|
await doRequest();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function initGlobalFetchAction() {
|
2024-11-26 20:54:50 -07:00
|
|
|
addDelegatedEventListener(document, 'submit', '.form-fetch-action', formFetchAction);
|
2024-11-26 08:36:55 -07:00
|
|
|
addDelegatedEventListener(document, 'click', '.link-action', linkAction);
|
2024-06-21 01:40:33 -06:00
|
|
|
}
|