From 5656edb385fd58b85376969b76a928e9f19d7288 Mon Sep 17 00:00:00 2001 From: Baptiste Grob <60621355+baptiste-grob@users.noreply.github.com> Date: Thu, 9 Jul 2020 15:43:58 +0200 Subject: [PATCH] refactor: AlertService --- .../directives/views/accountMenu.ts | 40 +++++------- .../javascripts/services/alertService.ts | 62 ++++++++----------- .../javascripts/ui_models/application.ts | 8 +-- .../javascripts/views/editor/editor_view.ts | 61 ++++++++---------- 4 files changed, 70 insertions(+), 101 deletions(-) diff --git a/app/assets/javascripts/directives/views/accountMenu.ts b/app/assets/javascripts/directives/views/accountMenu.ts index 97c9bef0d..93302611c 100644 --- a/app/assets/javascripts/directives/views/accountMenu.ts +++ b/app/assets/javascripts/directives/views/accountMenu.ts @@ -296,21 +296,16 @@ class AccountMenuCtrl extends PureViewCtrl { } } - mergeLocalChanged() { + async mergeLocalChanged() { if (!this.getState().formData.mergeLocal) { - this.application!.alertService!.confirm( - STRING_ACCOUNT_MENU_UNCHECK_MERGE, - undefined, - undefined, - undefined, - undefined, - () => { - this.setFormDataState({ - mergeLocal: true - }); - }, - true, - ); + if (await confirmDialog({ + text: STRING_ACCOUNT_MENU_UNCHECK_MERGE, + confirmButtonStyle: 'danger' + })) { + this.setFormDataState({ + mergeLocal: true + }); + } } } @@ -553,17 +548,12 @@ class AccountMenuCtrl extends PureViewCtrl { if (!signedIn) { message += STRING_REMOVE_PASSCODE_OFFLINE_ADDENDUM; } - this.application!.alertService!.confirm( - message, - undefined, - undefined, - undefined, - () => { - this.application!.removePasscode(); - }, - undefined, - true, - ); + if (await confirmDialog({ + text: message, + confirmButtonStyle: 'danger' + })) { + this.application!.removePasscode(); + } }; const needsPrivilege = await this.application!.privilegesService!.actionRequiresPrivilege( ProtectedAction.ManagePasscode diff --git a/app/assets/javascripts/services/alertService.ts b/app/assets/javascripts/services/alertService.ts index e6491a4d4..2ed1a816d 100644 --- a/app/assets/javascripts/services/alertService.ts +++ b/app/assets/javascripts/services/alertService.ts @@ -1,5 +1,5 @@ /* eslint-disable prefer-promise-reject-errors */ -import { SNAlertService } from 'snjs'; +import { SNAlertService, ButtonType, DismissBlockingDialog } from 'snjs'; import { SKAlert } from 'sn-stylekit'; /** @returns a promise resolving to true if the user confirmed, false if they canceled */ @@ -41,55 +41,47 @@ export function confirmDialog({ }); } -export class AlertService extends SNAlertService { - async alert( +export class AlertService implements SNAlertService { + alert( text: string, title: string, closeButtonText = 'OK', - onClose: () => void ) { - return new Promise((resolve) => { - const buttons = [ - { + return new Promise((resolve) => { + const alert = new SKAlert({ + title, + text, + buttons: [{ text: closeButtonText, style: 'neutral', - action: async () => { - if (onClose) { - this.deviceInterface!.timeout(onClose); - } - resolve(true); - }, - }, - ]; - const alert = new SKAlert({ title, text, buttons }); + action: resolve, + }], + }); alert.present(); }); } - /** @deprecated use confirmDialog instead */ - async confirm( + confirm( text: string, - title: string, - confirmButtonText = 'Confirm', - cancelButtonText = 'Cancel', - onConfirm?: () => void, - onCancel?: () => void, - destructive = false - ) { + title?: string, + confirmButtonText?: string, + confirmButtonType?: ButtonType, + cancelButtonText?: string + ): Promise { return confirmDialog({ text, title, confirmButtonText, cancelButtonText, - confirmButtonStyle: destructive ? 'danger' : 'info' - }).then(confirmed => new Promise((resolve, reject) => { - if (confirmed) { - onConfirm?.(); - resolve(true); - } else { - onCancel?.(); - reject(false); - } - })) + confirmButtonStyle: confirmButtonType === ButtonType.Danger ? 'danger' : 'info' + }); + } + + blockingDialog(text: string) { + const alert = new SKAlert({ text }); + alert.present(); + return () => { + alert.dismiss(); + }; } } diff --git a/app/assets/javascripts/ui_models/application.ts b/app/assets/javascripts/ui_models/application.ts index 51d6594f2..dad9bd931 100644 --- a/app/assets/javascripts/ui_models/application.ts +++ b/app/assets/javascripts/ui_models/application.ts @@ -63,13 +63,9 @@ export class WebApplication extends SNApplication { platformFromString(getPlatformString()), deviceInterface, new SNWebCrypto(), + new AlertService(), namespace, - [ - { - swap: SNAlertService, - with: AlertService - } - ], + undefined, undefined, ); this.$compile = $compile; diff --git a/app/assets/javascripts/views/editor/editor_view.ts b/app/assets/javascripts/views/editor/editor_view.ts index fece8e2fc..03199d0d5 100644 --- a/app/assets/javascripts/views/editor/editor_view.ts +++ b/app/assets/javascripts/views/editor/editor_view.ts @@ -32,6 +32,7 @@ import { StringDeleteNote, StringEmptyTrash } from '@/strings'; +import { confirmDialog } from '@/services/alertService'; const NOTE_PREVIEW_CHAR_LIMIT = 80; const MINIMUM_STATUS_DURATION = 400; @@ -685,7 +686,7 @@ class EditorViewCtrl extends PureViewCtrl<{}, EditorState> { ); return; } - const run = () => { + const run = async () => { if (this.note.locked) { this.application.alertService!.alert( STRING_DELETE_LOCKED_ATTEMPT @@ -699,28 +700,23 @@ class EditorViewCtrl extends PureViewCtrl<{}, EditorState> { title, permanently ); - this.application.alertService!.confirm( + if (await confirmDialog({ text, - undefined, - undefined, - undefined, - () => { - if (permanently) { - this.performNoteDeletion(this.note); - } else { - this.saveNote( - true, - false, - true, - (mutator) => { - mutator.trashed = true; - } - ); - } - }, - undefined, - true, - ); + confirmButtonStyle: 'danger' + })) { + if (permanently) { + this.performNoteDeletion(this.note); + } else { + this.saveNote( + true, + false, + true, + (mutator) => { + mutator.trashed = true; + } + ); + } + }; }; const requiresPrivilege = await this.application.privilegesService!.actionRequiresPrivilege( ProtectedAction.DeleteNote @@ -761,20 +757,15 @@ class EditorViewCtrl extends PureViewCtrl<{}, EditorState> { return this.application.getTrashedItems().length; } - emptyTrash() { + async emptyTrash() { const count = this.getTrashCount(); - this.application.alertService!.confirm( - StringEmptyTrash(count), - undefined, - undefined, - undefined, - () => { - this.application.emptyTrash(); - this.application.sync(); - }, - undefined, - true, - ); + if (await confirmDialog({ + text: StringEmptyTrash(count), + confirmButtonStyle: 'danger' + })) { + this.application.emptyTrash(); + this.application.sync(); + } } togglePin() {