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