2021-12-22 16:20:47 -08:00
|
|
|
import Rails from '@rails/ujs';
|
|
|
|
|
2022-01-05 17:55:46 -08:00
|
|
|
function createSubmitEvent(
|
2021-12-22 16:20:47 -08:00
|
|
|
submit: HTMLButtonElement,
|
|
|
|
rulesList: HTMLDivElement,
|
|
|
|
textEntry: HTMLButtonElement,
|
|
|
|
template: HTMLTemplateElement
|
|
|
|
): (event: Event) => void {
|
|
|
|
return (event) => {
|
|
|
|
event.preventDefault();
|
|
|
|
submit.disabled = true;
|
|
|
|
|
|
|
|
Rails.ajax({
|
|
|
|
url: '/ajax/mute',
|
|
|
|
type: 'POST',
|
|
|
|
dataType: 'json',
|
|
|
|
data: new URLSearchParams({muted_phrase: textEntry.value}).toString(),
|
|
|
|
success: (data) => {
|
|
|
|
submit.disabled = false;
|
|
|
|
if (!data.success) return;
|
|
|
|
|
|
|
|
const newEntryFragment = template.content.cloneNode(true) as Element;
|
|
|
|
newEntryFragment.querySelector<HTMLInputElement>('input').value = textEntry.value;
|
2021-12-22 16:29:48 -08:00
|
|
|
const newDeleteButton = newEntryFragment.querySelector<HTMLButtonElement>('button')
|
|
|
|
newDeleteButton.dataset.id = String(data.id);
|
|
|
|
newDeleteButton.onclick = createDeleteEvent(
|
|
|
|
newEntryFragment.querySelector('.form-group'),
|
|
|
|
newDeleteButton
|
|
|
|
)
|
2021-12-22 16:20:47 -08:00
|
|
|
|
|
|
|
rulesList.appendChild(newEntryFragment)
|
|
|
|
|
|
|
|
textEntry.value = ''
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2021-12-22 16:29:48 -08:00
|
|
|
}
|
|
|
|
|
2022-01-05 17:55:46 -08:00
|
|
|
function createDeleteEvent(
|
2021-12-22 16:29:48 -08:00
|
|
|
entry: HTMLDivElement,
|
|
|
|
deleteButton: HTMLButtonElement
|
|
|
|
): () => void {
|
|
|
|
return () => {
|
|
|
|
deleteButton.disabled = true;
|
|
|
|
|
|
|
|
Rails.ajax({
|
|
|
|
url: '/ajax/mute/' + deleteButton.dataset.id,
|
|
|
|
type: 'DELETE',
|
|
|
|
dataType: 'json',
|
|
|
|
success: (data) => {
|
|
|
|
if (data.success) {
|
|
|
|
entry.parentElement.removeChild(entry)
|
|
|
|
} else {
|
|
|
|
deleteButton.disabled = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-01-05 17:55:46 -08:00
|
|
|
}
|
|
|
|
|
2022-01-05 18:04:01 -08:00
|
|
|
export function muteDocumentHandler(): void {
|
2022-01-05 17:55:46 -08:00
|
|
|
const submit: HTMLButtonElement = document.getElementById('new-rule-submit') as HTMLButtonElement;
|
|
|
|
if (!submit || submit.classList.contains('js-initialized')) return;
|
|
|
|
|
|
|
|
const rulesList = document.querySelector<HTMLDivElement>('.js-rules-list');
|
|
|
|
rulesList.querySelectorAll<HTMLDivElement>('.form-group:not(.js-initalized)').forEach(entry => {
|
|
|
|
const button = entry.querySelector('button')
|
|
|
|
button.onclick = createDeleteEvent(entry, button)
|
|
|
|
});
|
|
|
|
const textEntry: HTMLButtonElement = document.getElementById('new-rule-text') as HTMLButtonElement;
|
|
|
|
const template: HTMLTemplateElement = document.getElementById('rule-template') as HTMLTemplateElement;
|
|
|
|
|
|
|
|
submit.form.onsubmit = createSubmitEvent(submit, rulesList, textEntry, template)
|
|
|
|
|
|
|
|
submit.classList.add('js-initialized');
|
2021-12-22 16:20:47 -08:00
|
|
|
}
|