Refactor removal of anon blocks to use request.js

This commit is contained in:
Andreas Nedbal 2022-08-31 21:46:10 +02:00
parent af137cc445
commit d521800a59
1 changed files with 11 additions and 17 deletions

View File

@ -1,27 +1,21 @@
import Rails from '@rails/ujs'; import { destroy } 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';
export function unblockAnonymousHandler(event: Event): void { export function unblockAnonymousHandler(event: Event): void {
const button: HTMLButtonElement = event.currentTarget as HTMLButtonElement; const button: HTMLButtonElement = event.currentTarget as HTMLButtonElement;
const targetId = button.dataset.target; const targetId = button.dataset.target;
let success = false;
Rails.ajax({ destroy(`/ajax/block_anon/${targetId}`)
url: `/ajax/block_anon/${targetId}`, .then(async response => {
type: 'DELETE', if (!response.ok) return;
success: (data) => {
success = data.success; const data = await response.json;
showNotification(data.message, data.success); showNotification(data.message, data.success);
},
error: (data, status, xhr) => {
console.log(data, status, xhr);
showErrorNotification(I18n.translate('frontend.error.message'));
},
complete: () => {
if (!success) return;
button.closest('.list-group-item').remove(); button.closest('.list-group-item').remove();
} })
}); .catch(err => {
console.log(err);
showErrorNotification(I18n.translate('frontend.error.message'));
});
} }