From 9e2a9a2be9c819abeb65f8c96b7327cee1909419 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 8 Jan 2022 02:53:16 +0100 Subject: [PATCH] Port moderator privilege feature to TypeScript --- .../features/moderation/privilege.ts | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 app/javascript/retrospring/features/moderation/privilege.ts diff --git a/app/javascript/retrospring/features/moderation/privilege.ts b/app/javascript/retrospring/features/moderation/privilege.ts new file mode 100644 index 00000000..fe57c7fa --- /dev/null +++ b/app/javascript/retrospring/features/moderation/privilege.ts @@ -0,0 +1,64 @@ +import Rails from '@rails/ujs'; + +import I18n from '../../../legacy/i18n'; +import { showNotification, showErrorNotification } from 'utilities/notifications'; + +export function privilegeCheckHandler(event: Event): void { + const checkbox = event.target as HTMLInputElement; + checkbox.disabled = true; + + const privilegeType = checkbox.dataset.type; + + Rails.ajax({ + url: '/ajax/mod/privilege', + type: 'POST', + data: new URLSearchParams({ + user: checkbox.dataset.user, + type: privilegeType, + status: String(checkbox.checked) + }).toString(), + success: (data) => { + if (data.success) { + checkbox.checked = data.checked; + } + + showNotification(data.message, data.success); + }, + error: (data, status, xhr) => { + console.log(data, status, xhr); + showErrorNotification(I18n.translate('frontend.error.message')); + checkbox.checked = false; + }, + complete: () => { + checkbox.disabled = false; + } + }); + + + /* + ($ document).on "click", "input[type=checkbox][name=check-your-privileges]", -> + box = $(this) + box.attr 'disabled', 'disabled' + + privType = box[0].dataset.type + boxChecked = box[0].checked + + $.ajax + url: '/ajax/mod/privilege' + type: 'POST' + data: + user: box[0].dataset.user + type: privType + status: boxChecked + success: (data, status, jqxhr) -> + if data.success + box[0].checked = if data.checked? then data.checked else !boxChecked + showNotification data.message, data.success + error: (jqxhr, status, error) -> + box[0].checked = false + console.log jqxhr, status, error + showNotification translate('frontend.error.message'), false + complete: (jqxhr, status) -> + box.removeAttr "disabled" + */ +} \ No newline at end of file