Refactor answer destroy to use request.js

This commit is contained in:
Andreas Nedbal 2022-09-03 05:05:06 +02:00
parent 5ea28e1830
commit 508c7e844b
1 changed files with 14 additions and 13 deletions

View File

@ -1,4 +1,4 @@
import Rails from '@rails/ujs';
import { post } from '@rails/request.js';
import { showErrorNotification, showNotification } from 'utilities/notifications';
import swal from 'sweetalert';
@ -24,23 +24,24 @@ export function answerboxDestroyHandler(event: Event): void {
return;
}
Rails.ajax({
url: '/ajax/destroy_answer',
type: 'POST',
data: new URLSearchParams({
post('/ajax/destroy_answer', {
body: {
answer: answerId
}).toString(),
success: (data) => {
},
contentType: 'application/json'
})
.then(async response => {
const data = await response.json;
if (data.success) {
document.querySelector(`.answerbox[data-id="${answerId}"]`).remove();
}
showNotification(data.message, data.success);
},
error: (data, status, xhr) => {
console.log(data, status, xhr);
})
.catch(err => {
console.log(err);
showErrorNotification(I18n.translate('frontend.error.message'));
}
});
});
}