Refactor list membership functionality to use request.js

This commit is contained in:
Andreas Nedbal 2022-09-03 07:51:30 +02:00
parent 41fea7fd5e
commit 5aab54acdb
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 { showNotification, showErrorNotification } from 'utilities/notifications'; import { showNotification, showErrorNotification } from 'utilities/notifications';
import I18n from 'retrospring/i18n'; import I18n from 'retrospring/i18n';
@ -12,15 +12,17 @@ export function listMembershipHandler(event: Event): void {
memberCount += checkbox.checked ? 1 : -1; memberCount += checkbox.checked ? 1 : -1;
Rails.ajax({ post('/ajax/list_membership', {
url: '/ajax/list_membership', body: {
type: 'POST',
data: new URLSearchParams({
list: list, list: list,
user: checkbox.dataset.user, user: checkbox.dataset.user,
add: String(checkbox.checked) add: String(checkbox.checked)
}).toString(), },
success: (data) => { contentType: 'application/json'
})
.then(async response => {
const data = await response.json;
if (data.success) { if (data.success) {
checkbox.checked = data.checked; checkbox.checked = data.checked;
memberCountElement.innerHTML = I18n.t('frontend.list.item.members', { count: memberCount }); memberCountElement.innerHTML = I18n.t('frontend.list.item.members', { count: memberCount });
@ -28,13 +30,12 @@ export function listMembershipHandler(event: Event): void {
} }
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(() => {
checkbox.removeAttribute('disabled'); checkbox.removeAttribute('disabled');
} });
});
} }