From ecb079b1d4f2fb78b264bf6c730e2e21eec0219b Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sun, 9 Jan 2022 02:14:53 +0100 Subject: [PATCH] Port report dialog functionality to TypeScript --- app/javascript/legacy/report.coffee | 29 -------------- app/javascript/packs/application.ts | 2 + app/javascript/packs/legacy.coffee | 10 ----- .../retrospring/utilities/reportDialog.ts | 40 +++++++++++++++++++ 4 files changed, 42 insertions(+), 39 deletions(-) delete mode 100644 app/javascript/legacy/report.coffee create mode 100644 app/javascript/retrospring/utilities/reportDialog.ts diff --git a/app/javascript/legacy/report.coffee b/app/javascript/legacy/report.coffee deleted file mode 100644 index 66cf0a3f..00000000 --- a/app/javascript/legacy/report.coffee +++ /dev/null @@ -1,29 +0,0 @@ -window.reportDialog = (type, target, callback) -> - swal - title: translate('frontend.report.confirm.title', {type: type}) - text: translate('frontend.report.confirm.text') - type: "input" - showCancelButton: true - confirmButtonColor: "#DD6B55" - confirmButtonText: translate('views.actions.report') - cancelButtonText: translate('views.actions.cancel') - closeOnConfirm: true - inputPlaceholder: translate('frontend.report.confirm.input') - , (value) -> - if typeof value == "boolean" && value == false - return false - - $.ajax - url: '/ajax/report' - type: 'POST' - data: - id: target - type: type - reason: value - success: (data, status, jqxhr) -> - showNotification data.message, data.success - error: (jqxhr, status, error) -> - console.log jqxhr, status, error - showNotification translate('frontend.error.message'), false - complete: (jqxhr, status) -> - callback type, target, jqxhr, status diff --git a/app/javascript/packs/application.ts b/app/javascript/packs/application.ts index cf9c3bcd..ebabaa44 100644 --- a/app/javascript/packs/application.ts +++ b/app/javascript/packs/application.ts @@ -1,3 +1,5 @@ +require('sweetalert/dist/sweetalert.css'); + import start from 'retrospring/common'; import initAnswerbox from 'retrospring/features/answerbox/index'; import initInbox from 'retrospring/features/inbox/index'; diff --git a/app/javascript/packs/legacy.coffee b/app/javascript/packs/legacy.coffee index 149a5445..b465dd66 100644 --- a/app/javascript/packs/legacy.coffee +++ b/app/javascript/packs/legacy.coffee @@ -12,15 +12,12 @@ import Cookies from 'js-cookie' require('nprogress/nprogress.css') require('jquery.growl/stylesheets/jquery.growl.css') -require('sweetalert/dist/sweetalert.css') # this file is generated by Rails import I18n from '../legacy/i18n' - import '../legacy/memes' import '../legacy/notifications' import '../legacy/pagination' -import '../legacy/report' import '../legacy/locale-box' import '../legacy/util' @@ -40,17 +37,10 @@ window.showNotification = (text, success=true) -> I18n.defaultLocale = 'en'; I18n.locale = Cookies.get('hl') || 'en'; -window.showNotificationXHRError = (jqxhr, status, error) -> - console.log jqxhr, status, error - showNotification translate('frontend.error.message'), false - $(document).on "click", "button#create-account", -> Turbolinks.visit "/sign_up" _ready = -> - if typeof sweetAlertInitialize != "undefined" - sweetAlertInitialize() - if document.getElementById('particles')? jumbo = $ '.j2-jumbo' bodyColorOrig = jumbo.css 'background-color' diff --git a/app/javascript/retrospring/utilities/reportDialog.ts b/app/javascript/retrospring/utilities/reportDialog.ts new file mode 100644 index 00000000..067e6e3c --- /dev/null +++ b/app/javascript/retrospring/utilities/reportDialog.ts @@ -0,0 +1,40 @@ +import Rails from '@rails/ujs'; +import swal from 'sweetalert'; + +import I18n from '../../legacy/i18n'; +import { showNotification, showErrorNotification } from 'utilities/notifications'; + +export function reportDialog(type: string, target: string): void { + swal({ + title: I18n.translate('frontend.report.confirm.title', { type: type }), + text: I18n.translate('frontend.report.confirm.text'), + type: 'input', + showCancelButton: true, + confirmButtonColor: "#DD6B55", + confirmButtonText: I18n.translate('views.actions.report'), + cancelButtonText: I18n.translate('views.actions.cancel'), + closeOnConfirm: true, + inputPlaceholder: I18n.translate('frontend.report.confirm.input') + }, (returnValue) => { + if (returnValue === false) { + return; + } + + Rails.ajax({ + url: '/ajax/report', + type: 'POST', + data: new URLSearchParams({ + id: String(target), + type: type, + reason: returnValue + }).toString(), + success: (data) => { + showNotification(data.message); + }, + error: (data, status, xhr) => { + console.log(data, status, xhr); + showErrorNotification(I18n.translate('frontend.error.message')); + } + }); + }); +} \ No newline at end of file