Refactor mute rule handling to use request.js
This commit is contained in:
parent
d521800a59
commit
812c9617ff
|
@ -1,4 +1,5 @@
|
||||||
import Rails from '@rails/ujs';
|
import Rails from '@rails/ujs';
|
||||||
|
import { destroy, post } from '@rails/request.js';
|
||||||
|
|
||||||
function createSubmitEvent(
|
function createSubmitEvent(
|
||||||
submit: HTMLButtonElement,
|
submit: HTMLButtonElement,
|
||||||
|
@ -10,29 +11,31 @@ function createSubmitEvent(
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
submit.disabled = true;
|
submit.disabled = true;
|
||||||
|
|
||||||
Rails.ajax({
|
post('/ajax/mute', {
|
||||||
url: '/ajax/mute',
|
body: {
|
||||||
type: 'POST',
|
muted_phrase: textEntry.value
|
||||||
dataType: 'json',
|
},
|
||||||
data: new URLSearchParams({muted_phrase: textEntry.value}).toString(),
|
contentType: 'application/json'
|
||||||
success: (data) => {
|
})
|
||||||
|
.then(async response => {
|
||||||
|
const data = await response.json;
|
||||||
|
|
||||||
submit.disabled = false;
|
submit.disabled = false;
|
||||||
if (!data.success) return;
|
if (!data.success) return;
|
||||||
|
|
||||||
const newEntryFragment = template.content.cloneNode(true) as Element;
|
const newEntryFragment = template.content.cloneNode(true) as Element;
|
||||||
newEntryFragment.querySelector<HTMLInputElement>('input').value = textEntry.value;
|
newEntryFragment.querySelector<HTMLInputElement>('input').value = textEntry.value;
|
||||||
const newDeleteButton = newEntryFragment.querySelector<HTMLButtonElement>('button')
|
|
||||||
|
const newDeleteButton = newEntryFragment.querySelector<HTMLButtonElement>('button');
|
||||||
newDeleteButton.dataset.id = String(data.id);
|
newDeleteButton.dataset.id = String(data.id);
|
||||||
newDeleteButton.onclick = createDeleteEvent(
|
newDeleteButton.onclick = createDeleteEvent(
|
||||||
newEntryFragment.querySelector('.form-group'),
|
newEntryFragment.querySelector('.form-group'),
|
||||||
newDeleteButton
|
newDeleteButton
|
||||||
)
|
);
|
||||||
|
|
||||||
rulesList.appendChild(newEntryFragment)
|
rulesList.appendChild(newEntryFragment);
|
||||||
|
textEntry.value = '';
|
||||||
textEntry.value = ''
|
});
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,18 +46,16 @@ function createDeleteEvent(
|
||||||
return () => {
|
return () => {
|
||||||
deleteButton.disabled = true;
|
deleteButton.disabled = true;
|
||||||
|
|
||||||
Rails.ajax({
|
destroy(`/ajax/mute/${deleteButton.dataset.id}`)
|
||||||
url: '/ajax/mute/' + deleteButton.dataset.id,
|
.then(async response => {
|
||||||
type: 'DELETE',
|
const data = await response.json;
|
||||||
dataType: 'json',
|
|
||||||
success: (data) => {
|
|
||||||
if (data.success) {
|
if (data.success) {
|
||||||
entry.parentElement.removeChild(entry)
|
entry.parentElement.removeChild(entry)
|
||||||
} else {
|
} else {
|
||||||
deleteButton.disabled = false;
|
deleteButton.disabled = false;
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue