Implement frontend for deleting mute rules

This commit is contained in:
Karina Kwiatek 2021-12-23 01:29:48 +01:00
parent 23a5fb55d1
commit 05ed214367
2 changed files with 33 additions and 2 deletions

View File

@ -1,10 +1,14 @@
import {createSubmitEvent} from "retrospring/features/settings/mute";
import {createDeleteEvent, createSubmitEvent} from "retrospring/features/settings/mute";
export default (): void => {
const submit: HTMLButtonElement = document.getElementById('new-rule-submit') as HTMLButtonElement;
if (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;

View File

@ -21,7 +21,12 @@ export function createSubmitEvent(
const newEntryFragment = template.content.cloneNode(true) as Element;
newEntryFragment.querySelector<HTMLInputElement>('input').value = textEntry.value;
newEntryFragment.querySelector<HTMLButtonElement>('button').dataset.id = String(data.id);
const newDeleteButton = newEntryFragment.querySelector<HTMLButtonElement>('button')
newDeleteButton.dataset.id = String(data.id);
newDeleteButton.onclick = createDeleteEvent(
newEntryFragment.querySelector('.form-group'),
newDeleteButton
)
rulesList.appendChild(newEntryFragment)
@ -29,4 +34,26 @@ export function createSubmitEvent(
}
});
};
}
export function createDeleteEvent(
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;
}
}
})
}
}