Refactor comment creation to use request.js

This commit is contained in:
Andreas Nedbal 2022-09-03 05:03:25 +02:00
parent 2c5da2bc5c
commit 40637c5f6d
1 changed files with 15 additions and 14 deletions

View File

@ -1,4 +1,4 @@
import Rails from '@rails/ujs'; import { post } from '@rails/request.js';
import I18n from 'retrospring/i18n'; import I18n from 'retrospring/i18n';
import { showNotification, showErrorNotification } from 'utilities/notifications'; import { showNotification, showErrorNotification } from 'utilities/notifications';
@ -19,14 +19,16 @@ export function commentCreateHandler(event: KeyboardEvent): boolean {
input.disabled = true; input.disabled = true;
Rails.ajax({ post('/ajax/create_comment', {
url: '/ajax/create_comment', body: {
type: 'POST',
data: new URLSearchParams({
answer: id, answer: id,
comment: input.value comment: input.value
}).toString(), },
success: (data) => { contentType: 'application/json'
})
.then(async response => {
const data = await response.json;
if (data.success) { if (data.success) {
document.querySelector(`#ab-comments-${id}`).innerHTML = data.render; document.querySelector(`#ab-comments-${id}`).innerHTML = data.render;
document.querySelector(`#ab-comment-count-${id}`).innerHTML = data.count; document.querySelector(`#ab-comment-count-${id}`).innerHTML = data.count;
@ -39,14 +41,13 @@ export function commentCreateHandler(event: KeyboardEvent): boolean {
} }
showNotification(data.message, data.success); showNotification(data.message, data.success);
}, })
error: (data, status, xhr) => { .catch(err => {
console.log(data, status, xhr); console.log(err);
showErrorNotification(I18n.translate('frontend.error.message')); showErrorNotification(I18n.translate('frontend.error.message'));
}, })
complete: () => { .finally(() => {
input.disabled = false; input.disabled = false;
}
}); });
} }
} }