Refactor answer subscribing to use request.js

This commit is contained in:
Andreas Nedbal 2022-09-03 05:05:43 +02:00
parent 0aff344581
commit 5295dc83c8
1 changed files with 13 additions and 12 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';
@ -20,13 +20,15 @@ export function answerboxSubscribeHandler(event: Event): void {
targetUrl = '/ajax/unsubscribe'; targetUrl = '/ajax/unsubscribe';
} }
Rails.ajax({ post(targetUrl, {
url: targetUrl, body: {
type: 'POST',
data: new URLSearchParams({
answer: id answer: id
}).toString(), },
success: (data) => { contentType: 'application/json'
})
.then(async response => {
const data = await response.json;
if (data.success) { if (data.success) {
button.dataset.torpedo = ["yes", "no"][torpedo]; button.dataset.torpedo = ["yes", "no"][torpedo];
button.children[0].nextSibling.textContent = ' ' + (torpedo ? I18n.translate('voc.unsubscribe') : I18n.translate('voc.subscribe')); button.children[0].nextSibling.textContent = ' ' + (torpedo ? I18n.translate('voc.unsubscribe') : I18n.translate('voc.subscribe'));
@ -34,10 +36,9 @@ export function answerboxSubscribeHandler(event: Event): void {
} else { } else {
showErrorNotification(I18n.translate(`frontend.subscription.fail.${torpedo ? 'subscribe' : 'unsubscribe'}`)); showErrorNotification(I18n.translate(`frontend.subscription.fail.${torpedo ? 'subscribe' : 'unsubscribe'}`));
} }
}, })
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'));
}
}); });
} }