Refactor question answering to use request.js

This commit is contained in:
Andreas Nedbal 2022-09-03 16:18:31 +02:00
parent eb0271210b
commit 2f05708a65
1 changed files with 12 additions and 11 deletions

View File

@ -1,4 +1,4 @@
import Rails from '@rails/ujs'; import { post } from '@rails/request.js';
import { showNotification, showErrorNotification } from 'utilities/notifications'; import { showNotification, showErrorNotification } from 'utilities/notifications';
@ -22,11 +22,13 @@ export function questionAnswerHandler(event: Event): void {
inbox: String(false) inbox: String(false)
}; };
Rails.ajax({ post('/ajax/answer', {
url: '/ajax/answer', body: data,
type: 'POST', contentType: 'application/json'
data: new URLSearchParams(data).toString(), })
success: (data) => { .then(async response => {
const data = await response.json;
if (!data.success) { if (!data.success) {
showErrorNotification(data.message); showErrorNotification(data.message);
button.disabled = false; button.disabled = false;
@ -37,13 +39,12 @@ export function questionAnswerHandler(event: Event): void {
showNotification(data.message); showNotification(data.message);
document.querySelector('div#answers').insertAdjacentHTML('afterbegin', data.render); document.querySelector('div#answers').insertAdjacentHTML('afterbegin', data.render);
document.querySelector('div#q-answer-box').remove(); document.querySelector('div#q-answer-box').remove();
}, })
error: (data, status, xhr) => { .catch(err => {
console.log(data, status, xhr); console.log(err);
button.disabled = false; button.disabled = false;
document.querySelector<HTMLInputElement>('textarea#q-answer-text').readOnly = false; document.querySelector<HTMLInputElement>('textarea#q-answer-text').readOnly = false;
} });
});
} }
export function questionAnswerInputHandler(event: KeyboardEvent): void { export function questionAnswerInputHandler(event: KeyboardEvent): void {