2024-02-17 13:17:04 +00:00
|
|
|
import {POST} from '../../modules/fetch.js';
|
2023-02-19 04:06:14 +00:00
|
|
|
import {hideElem, showElem, toggleElem} from '../../utils/dom.js';
|
2022-12-23 16:03:11 +00:00
|
|
|
|
2021-11-09 09:27:25 +00:00
|
|
|
export function initCompWebHookEditor() {
|
2024-02-17 13:17:04 +00:00
|
|
|
if (!document.querySelectorAll('.new.webhook').length) {
|
2021-10-16 17:28:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-08-17 18:24:31 +00:00
|
|
|
for (const input of document.querySelectorAll('label.events input')) {
|
2024-02-17 13:17:04 +00:00
|
|
|
input.addEventListener('change', function () {
|
|
|
|
if (this.checked) {
|
|
|
|
showElem('.events.fields');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-08-17 18:24:31 +00:00
|
|
|
for (const input of document.querySelectorAll('label.non-events input')) {
|
2024-02-17 13:17:04 +00:00
|
|
|
input.addEventListener('change', function () {
|
|
|
|
if (this.checked) {
|
|
|
|
hideElem('.events.fields');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-10-16 17:28:04 +00:00
|
|
|
|
2024-03-09 11:59:16 +00:00
|
|
|
// some webhooks (like Gitea) allow to set the request method (GET/POST), and it would toggle the "Content Type" field
|
|
|
|
const httpMethodInput = document.getElementById('http_method');
|
|
|
|
if (httpMethodInput) {
|
|
|
|
const updateContentType = function () {
|
|
|
|
const visible = httpMethodInput.value === 'POST';
|
|
|
|
toggleElem(document.getElementById('content_type').closest('.field'), visible);
|
|
|
|
};
|
|
|
|
updateContentType();
|
|
|
|
httpMethodInput.addEventListener('change', updateContentType);
|
|
|
|
}
|
2021-10-16 17:28:04 +00:00
|
|
|
|
|
|
|
// Test delivery
|
2024-02-17 13:17:04 +00:00
|
|
|
document.getElementById('test-delivery')?.addEventListener('click', async function () {
|
2024-03-21 16:31:15 +00:00
|
|
|
this.classList.add('is-loading', 'disabled');
|
2024-02-17 13:17:04 +00:00
|
|
|
await POST(this.getAttribute('data-link'));
|
|
|
|
setTimeout(() => {
|
|
|
|
window.location.href = this.getAttribute('data-redirect');
|
|
|
|
}, 5000);
|
2021-10-16 17:28:04 +00:00
|
|
|
});
|
|
|
|
}
|