From 918c514f12bd202f271d5ae928daece85c3eb176 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 3 Sep 2022 08:43:19 +0200 Subject: [PATCH] Refactor user privilege changing to use request.js --- .../features/moderation/privilege.ts | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/app/javascript/retrospring/features/moderation/privilege.ts b/app/javascript/retrospring/features/moderation/privilege.ts index 6446da88..07b0d822 100644 --- a/app/javascript/retrospring/features/moderation/privilege.ts +++ b/app/javascript/retrospring/features/moderation/privilege.ts @@ -1,4 +1,4 @@ -import Rails from '@rails/ujs'; +import { post } from '@rails/request.js'; import I18n from 'retrospring/i18n'; import { showNotification, showErrorNotification } from 'utilities/notifications'; @@ -9,29 +9,29 @@ export function privilegeCheckHandler(event: Event): void { const privilegeType = checkbox.dataset.type; - Rails.ajax({ - url: '/ajax/mod/privilege', - type: 'POST', - data: new URLSearchParams({ + post('/ajax/mod/privilege', { + body: { user: checkbox.dataset.user, type: privilegeType, status: String(checkbox.checked) - }).toString(), - success: (data) => { + }, + contentType: 'application/json' + }) + .then(async response => { + const data = await response.json; + if (data.success) { checkbox.checked = data.checked; } 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')); checkbox.checked = false; - }, - complete: () => { + }) + .finally(() => { checkbox.disabled = false; - } - }); - + }); } \ No newline at end of file