Refactor question asking on profiles to use request.js

This commit is contained in:
Andreas Nedbal 2022-09-03 16:19:32 +02:00
parent 9c9f56e231
commit dba817c8b6
1 changed files with 16 additions and 15 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 I18n from 'retrospring/i18n';
@ -15,15 +15,17 @@ export function questionboxUserHandler(event: Event): void {
document.querySelector<HTMLInputElement>('textarea[name=qb-question]').readOnly = true;
button.disabled = true;
Rails.ajax({
url: '/ajax/ask',
type: 'POST',
data: new URLSearchParams({
post('/ajax/ask', {
body: {
rcpt: document.querySelector<HTMLInputElement>('input[name=qb-to]').value,
question: document.querySelector<HTMLInputElement>('textarea[name=qb-question]').value,
anonymousQuestion: String(anonymousQuestion)
}).toString(),
success: (data) => {
},
contentType: 'application/json'
})
.then(async response => {
const data = await response.json;
if (data.success) {
document.querySelector<HTMLInputElement>('textarea[name=qb-question]').value = '';
@ -37,15 +39,14 @@ export function questionboxUserHandler(event: Event): void {
}
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'));
},
complete: () => {
})
.finally(() => {
document.querySelector<HTMLInputElement>('textarea[name=qb-question]').readOnly = false;
button.disabled = false;
}
});
}