Files
Atay-Makhzan/web_src/js/features/comp/WebHookEditor.ts
T

48 lines
1.6 KiB
TypeScript
Raw Normal View History

import {POST} from '../../modules/fetch.ts';
import {hideElem, showElem, toggleElem} from '../../utils/dom.ts';
2022-12-23 17:03:11 +01:00
export function initCompWebHookEditor() {
if (!document.querySelectorAll('.new.webhook').length) {
2021-10-17 01:28:04 +08:00
return;
}
2025-01-15 21:26:17 +01:00
for (const input of document.querySelectorAll<HTMLInputElement>('.events.checkbox input')) {
input.addEventListener('change', function () {
if (this.checked) {
showElem('.events.fields');
}
});
}
2025-01-15 21:26:17 +01:00
for (const input of document.querySelectorAll<HTMLInputElement>('.non-events.checkbox input')) {
input.addEventListener('change', function () {
if (this.checked) {
hideElem('.events.fields');
}
});
}
2021-10-17 01:28:04 +08:00
// some webhooks (like Gitea) allow to set the request method (GET/POST), and it would toggle the "Content Type" field
const httpMethodInput = document.querySelector<HTMLInputElement>('#http_method');
if (httpMethodInput) {
const updateContentType = function () {
const visible = httpMethodInput.value === 'POST';
2025-12-03 03:13:16 +01:00
toggleElem(document.querySelector('#content_type')!.closest('.field')!, visible);
};
updateContentType();
httpMethodInput.addEventListener('change', updateContentType);
}
2021-10-17 01:28:04 +08:00
// Test delivery
2025-01-15 21:26:17 +01:00
document.querySelector<HTMLButtonElement>('#test-delivery')?.addEventListener('click', async function () {
this.classList.add('is-loading', 'disabled');
2025-12-03 03:13:16 +01:00
await POST(this.getAttribute('data-link')!);
setTimeout(() => {
2025-12-03 03:13:16 +01:00
const redirectUrl = this.getAttribute('data-redirect');
if (redirectUrl) {
window.location.href = redirectUrl;
}
}, 5000);
2021-10-17 01:28:04 +08:00
});
}