From 663951990546ac24d452c1a5e07521413e23d82b Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 3 Sep 2022 08:42:17 +0200 Subject: [PATCH 1/5] Refactor user banning to use request.js --- .../retrospring/features/moderation/ban.ts | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/app/javascript/retrospring/features/moderation/ban.ts b/app/javascript/retrospring/features/moderation/ban.ts index 41449ceb..d286dac4 100644 --- a/app/javascript/retrospring/features/moderation/ban.ts +++ b/app/javascript/retrospring/features/moderation/ban.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'; @@ -44,16 +44,17 @@ export function banFormHandler(event: Event): void { } } - Rails.ajax({ - url: '/ajax/mod/ban', - type: 'POST', - data: new URLSearchParams(data).toString(), - success: (data) => { + post('/ajax/mod/ban', { + body: data, + contentType: 'application/json' + }) + .then(async response => { + const data = await response.json; + 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')); - } - }); + }); } \ No newline at end of file From 389e1271d7a072442b756f62072c9e447d81dc47 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 3 Sep 2022 08:42:37 +0200 Subject: [PATCH 2/5] Refactor global anon blocks to use request.js --- .../features/moderation/blockAnon.ts | 71 ++++++++++--------- 1 file changed, 36 insertions(+), 35 deletions(-) diff --git a/app/javascript/retrospring/features/moderation/blockAnon.ts b/app/javascript/retrospring/features/moderation/blockAnon.ts index 5c587300..4f04004c 100644 --- a/app/javascript/retrospring/features/moderation/blockAnon.ts +++ b/app/javascript/retrospring/features/moderation/blockAnon.ts @@ -1,46 +1,47 @@ -import Rails from '@rails/ujs'; +import { post } from '@rails/request.js'; import swal from 'sweetalert'; import { showErrorNotification, showNotification } from "utilities/notifications"; import I18n from "retrospring/i18n"; export function blockAnonEventHandler(event: Event): void { - event.preventDefault(); - - swal({ - title: I18n.translate('frontend.mod_mute.confirm.title'), - text: I18n.translate('frontend.mod_mute.confirm.text'), - type: 'warning', - showCancelButton: true, - confirmButtonColor: "#DD6B55", - confirmButtonText: I18n.translate('voc.y'), - cancelButtonText: I18n.translate('voc.n'), - closeOnConfirm: true, - }, (dialogResult) => { - if (!dialogResult) { - return; - } + event.preventDefault(); + + swal({ + title: I18n.translate('frontend.mod_mute.confirm.title'), + text: I18n.translate('frontend.mod_mute.confirm.text'), + type: 'warning', + showCancelButton: true, + confirmButtonColor: "#DD6B55", + confirmButtonText: I18n.translate('voc.y'), + cancelButtonText: I18n.translate('voc.n'), + closeOnConfirm: true, + }, (dialogResult) => { + if (!dialogResult) { + return; + } - const sender: HTMLAnchorElement = event.target as HTMLAnchorElement; + const sender: HTMLAnchorElement = event.target as HTMLAnchorElement; - const data = { - question: sender.getAttribute('data-q-id'), - global: 'true' - }; + const data = { + question: sender.getAttribute('data-q-id'), + global: 'true' + }; - Rails.ajax({ - url: '/ajax/block_anon', - type: 'POST', - data: new URLSearchParams(data).toString(), - success: (data) => { - if (!data.success) return false; + post('/ajax/block_anon', { + body: data, + contentType: 'application/json' + }) + .then(async response => { + const data = await response.json; - showNotification(data.message); - }, - error: (data, status, xhr) => { - console.log(data, status, xhr); - showErrorNotification(I18n.translate('frontend.error.message')); - } - }); - }); + if (!data.success) return false; + + showNotification(data.message); + }) + .catch(err => { + console.log(err); + showErrorNotification(I18n.translate('frontend.error.message')); + }); + }); } \ No newline at end of file From fe6b6b198ff51764d63b6a67e025c163882381a5 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 3 Sep 2022 08:42:55 +0200 Subject: [PATCH 3/5] Refactor report removal to use request.js --- .../features/moderation/destroy.ts | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/app/javascript/retrospring/features/moderation/destroy.ts b/app/javascript/retrospring/features/moderation/destroy.ts index 765f2387..77c1376e 100644 --- a/app/javascript/retrospring/features/moderation/destroy.ts +++ b/app/javascript/retrospring/features/moderation/destroy.ts @@ -1,4 +1,4 @@ -import Rails from '@rails/ujs'; +import { post } from '@rails/request.js'; import swal from 'sweetalert'; import I18n from 'retrospring/i18n'; @@ -18,22 +18,24 @@ export function destroyReportHandler(event: Event): void { cancelButtonText: I18n.translate('voc.cancel'), closeOnConfirm: true }, () => { - Rails.ajax({ - url: '/ajax/mod/destroy_report', - type: 'POST', - data: new URLSearchParams({ + post('/ajax/mod/destroy_report', { + body: { id: id - }).toString(), - success: (data) => { + }, + contentType: 'application/json' + }) + .then(async response => { + const data = await response.json; + if (data.success) { document.querySelector(`.moderationbox[data-id="${id}"]`).remove(); } + 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')); - } - }); + }); }); } \ No newline at end of file From 918c514f12bd202f271d5ae928daece85c3eb176 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 3 Sep 2022 08:43:19 +0200 Subject: [PATCH 4/5] 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 From 6a083230ddcaec697b38aee1e1562feff8dfc943 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 3 Sep 2022 16:42:17 +0200 Subject: [PATCH 5/5] Fixed wrong call of translation method in ban action --- app/controllers/ajax/moderation_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/ajax/moderation_controller.rb b/app/controllers/ajax/moderation_controller.rb index dc34405b..3c1fe669 100644 --- a/app/controllers/ajax/moderation_controller.rb +++ b/app/controllers/ajax/moderation_controller.rb @@ -59,7 +59,7 @@ class Ajax::ModerationController < AjaxController raise Errors::InvalidBanDuration unless %w[hours days weeks months].include? duration_unit expiry = DateTime.now + duration.public_send(duration_unit) - @response[:message] = I18n.t(".success.temporary", date: expiry.to_s) + @response[:message] = t(".success.temporary", date: expiry.to_s) end UseCase::User::Ban.call(