Implement frontend for deleting mute rules
This commit is contained in:
parent
23a5fb55d1
commit
05ed214367
|
@ -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;
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
@ -30,3 +35,25 @@ 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;
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue