refactor: AlertService

This commit is contained in:
Baptiste Grob
2020-07-09 15:43:58 +02:00
parent a9aba87033
commit 5656edb385
4 changed files with 70 additions and 101 deletions

View File

@@ -296,21 +296,16 @@ class AccountMenuCtrl extends PureViewCtrl {
} }
} }
mergeLocalChanged() { async mergeLocalChanged() {
if (!this.getState().formData.mergeLocal) { if (!this.getState().formData.mergeLocal) {
this.application!.alertService!.confirm( if (await confirmDialog({
STRING_ACCOUNT_MENU_UNCHECK_MERGE, text: STRING_ACCOUNT_MENU_UNCHECK_MERGE,
undefined, confirmButtonStyle: 'danger'
undefined, })) {
undefined, this.setFormDataState({
undefined, mergeLocal: true
() => { });
this.setFormDataState({ }
mergeLocal: true
});
},
true,
);
} }
} }
@@ -553,17 +548,12 @@ class AccountMenuCtrl extends PureViewCtrl {
if (!signedIn) { if (!signedIn) {
message += STRING_REMOVE_PASSCODE_OFFLINE_ADDENDUM; message += STRING_REMOVE_PASSCODE_OFFLINE_ADDENDUM;
} }
this.application!.alertService!.confirm( if (await confirmDialog({
message, text: message,
undefined, confirmButtonStyle: 'danger'
undefined, })) {
undefined, this.application!.removePasscode();
() => { }
this.application!.removePasscode();
},
undefined,
true,
);
}; };
const needsPrivilege = await this.application!.privilegesService!.actionRequiresPrivilege( const needsPrivilege = await this.application!.privilegesService!.actionRequiresPrivilege(
ProtectedAction.ManagePasscode ProtectedAction.ManagePasscode

View File

@@ -1,5 +1,5 @@
/* eslint-disable prefer-promise-reject-errors */ /* eslint-disable prefer-promise-reject-errors */
import { SNAlertService } from 'snjs'; import { SNAlertService, ButtonType, DismissBlockingDialog } from 'snjs';
import { SKAlert } from 'sn-stylekit'; import { SKAlert } from 'sn-stylekit';
/** @returns a promise resolving to true if the user confirmed, false if they canceled */ /** @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 { export class AlertService implements SNAlertService {
async alert( alert(
text: string, text: string,
title: string, title: string,
closeButtonText = 'OK', closeButtonText = 'OK',
onClose: () => void
) { ) {
return new Promise((resolve) => { return new Promise<void>((resolve) => {
const buttons = [ const alert = new SKAlert({
{ title,
text,
buttons: [{
text: closeButtonText, text: closeButtonText,
style: 'neutral', style: 'neutral',
action: async () => { action: resolve,
if (onClose) { }],
this.deviceInterface!.timeout(onClose); });
}
resolve(true);
},
},
];
const alert = new SKAlert({ title, text, buttons });
alert.present(); alert.present();
}); });
} }
/** @deprecated use confirmDialog instead */ confirm(
async confirm(
text: string, text: string,
title: string, title?: string,
confirmButtonText = 'Confirm', confirmButtonText?: string,
cancelButtonText = 'Cancel', confirmButtonType?: ButtonType,
onConfirm?: () => void, cancelButtonText?: string
onCancel?: () => void, ): Promise<boolean> {
destructive = false
) {
return confirmDialog({ return confirmDialog({
text, text,
title, title,
confirmButtonText, confirmButtonText,
cancelButtonText, cancelButtonText,
confirmButtonStyle: destructive ? 'danger' : 'info' confirmButtonStyle: confirmButtonType === ButtonType.Danger ? 'danger' : 'info'
}).then(confirmed => new Promise((resolve, reject) => { });
if (confirmed) { }
onConfirm?.();
resolve(true); blockingDialog(text: string) {
} else { const alert = new SKAlert({ text });
onCancel?.(); alert.present();
reject(false); return () => {
} alert.dismiss();
})) };
} }
} }

View File

@@ -63,13 +63,9 @@ export class WebApplication extends SNApplication {
platformFromString(getPlatformString()), platformFromString(getPlatformString()),
deviceInterface, deviceInterface,
new SNWebCrypto(), new SNWebCrypto(),
new AlertService(),
namespace, namespace,
[ undefined,
{
swap: SNAlertService,
with: AlertService
}
],
undefined, undefined,
); );
this.$compile = $compile; this.$compile = $compile;

View File

@@ -32,6 +32,7 @@ import {
StringDeleteNote, StringDeleteNote,
StringEmptyTrash StringEmptyTrash
} from '@/strings'; } from '@/strings';
import { confirmDialog } from '@/services/alertService';
const NOTE_PREVIEW_CHAR_LIMIT = 80; const NOTE_PREVIEW_CHAR_LIMIT = 80;
const MINIMUM_STATUS_DURATION = 400; const MINIMUM_STATUS_DURATION = 400;
@@ -685,7 +686,7 @@ class EditorViewCtrl extends PureViewCtrl<{}, EditorState> {
); );
return; return;
} }
const run = () => { const run = async () => {
if (this.note.locked) { if (this.note.locked) {
this.application.alertService!.alert( this.application.alertService!.alert(
STRING_DELETE_LOCKED_ATTEMPT STRING_DELETE_LOCKED_ATTEMPT
@@ -699,28 +700,23 @@ class EditorViewCtrl extends PureViewCtrl<{}, EditorState> {
title, title,
permanently permanently
); );
this.application.alertService!.confirm( if (await confirmDialog({
text, text,
undefined, confirmButtonStyle: 'danger'
undefined, })) {
undefined, if (permanently) {
() => { this.performNoteDeletion(this.note);
if (permanently) { } else {
this.performNoteDeletion(this.note); this.saveNote(
} else { true,
this.saveNote( false,
true, true,
false, (mutator) => {
true, mutator.trashed = true;
(mutator) => { }
mutator.trashed = true; );
} }
); };
}
},
undefined,
true,
);
}; };
const requiresPrivilege = await this.application.privilegesService!.actionRequiresPrivilege( const requiresPrivilege = await this.application.privilegesService!.actionRequiresPrivilege(
ProtectedAction.DeleteNote ProtectedAction.DeleteNote
@@ -761,20 +757,15 @@ class EditorViewCtrl extends PureViewCtrl<{}, EditorState> {
return this.application.getTrashedItems().length; return this.application.getTrashedItems().length;
} }
emptyTrash() { async emptyTrash() {
const count = this.getTrashCount(); const count = this.getTrashCount();
this.application.alertService!.confirm( if (await confirmDialog({
StringEmptyTrash(count), text: StringEmptyTrash(count),
undefined, confirmButtonStyle: 'danger'
undefined, })) {
undefined, this.application.emptyTrash();
() => { this.application.sync();
this.application.emptyTrash(); }
this.application.sync();
},
undefined,
true,
);
} }
togglePin() { togglePin() {