Refactor question asking to use request.js

This commit is contained in:
Andreas Nedbal 2022-09-03 16:19:00 +02:00
parent 2f05708a65
commit 9c9f56e231
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 { showErrorNotification, showNotification } from 'utilities/notifications';
import I18n from 'retrospring/i18n'; import I18n from 'retrospring/i18n';
@ -8,30 +8,31 @@ export function questionboxAllHandler(event: Event): void {
button.disabled = true; button.disabled = true;
document.querySelector<HTMLInputElement>('textarea[name=qb-all-question]').readOnly = true; document.querySelector<HTMLInputElement>('textarea[name=qb-all-question]').readOnly = true;
Rails.ajax({ post('/ajax/ask', {
url: '/ajax/ask', body: {
type: 'POST',
data: new URLSearchParams({
rcpt: 'followers', rcpt: 'followers',
question: document.querySelector<HTMLInputElement>('textarea[name=qb-all-question]').value, question: document.querySelector<HTMLInputElement>('textarea[name=qb-all-question]').value,
anonymousQuestion: 'false' anonymousQuestion: 'false'
}).toString(), },
success: (data) => { contentType: 'application/json'
})
.then(async response => {
const data = await response.json;
if (data.success) { if (data.success) {
document.querySelector<HTMLInputElement>('textarea[name=qb-all-question]').value = ''; document.querySelector<HTMLInputElement>('textarea[name=qb-all-question]').value = '';
window['$']('#modal-ask-followers').modal('hide'); window['$']('#modal-ask-followers').modal('hide');
} }
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(() => {
button.disabled = false; button.disabled = false;
document.querySelector<HTMLInputElement>('textarea[name=qb-all-question]').readOnly = false; document.querySelector<HTMLInputElement>('textarea[name=qb-all-question]').readOnly = false;
}
}); });
} }