refactor: extract alert method to a standalone function

This commit is contained in:
Baptiste Grob
2020-07-20 11:31:32 +02:00
parent c6a25d6ae4
commit 966783c8b9

View File

@@ -8,13 +8,13 @@ export function confirmDialog({
title, title,
confirmButtonText = 'Confirm', confirmButtonText = 'Confirm',
cancelButtonText = 'Cancel', cancelButtonText = 'Cancel',
confirmButtonStyle = 'info' confirmButtonStyle = 'info',
}: { }: {
text: string, text: string;
title?: string, title?: string;
confirmButtonText?: string, confirmButtonText?: string;
cancelButtonText?: string, cancelButtonText?: string;
confirmButtonStyle?: 'danger' | 'info' confirmButtonStyle?: 'danger' | 'info';
}) { }) {
return new Promise<boolean>((resolve) => { return new Promise<boolean>((resolve) => {
const alert = new SKAlert({ const alert = new SKAlert({
@@ -41,24 +41,37 @@ export function confirmDialog({
}); });
} }
export class AlertService implements SNAlertService { export function alertDialog({
alert( title,
text: string, text,
title: string, closeButtonText = 'OK',
closeButtonText = 'OK', }: {
) { title?: string;
return new Promise<void>((resolve) => { text: string;
const alert = new SKAlert({ closeButtonText?: string;
title, }) {
text, return new Promise<void>((resolve) => {
buttons: [{ const alert = new SKAlert({
title,
text,
buttons: [
{
text: closeButtonText, text: closeButtonText,
style: 'neutral', style: 'neutral',
action: resolve, action: resolve,
}], },
}); ],
alert.present();
}); });
alert.present();
});
}
export class AlertService implements SNAlertService {
/**
* @deprecated use the standalone `alertDialog` function instead
*/
alert(text: string, title?: string, closeButtonText?: string) {
return alertDialog({ text, title, closeButtonText });
} }
confirm( confirm(
@@ -73,7 +86,8 @@ export class AlertService implements SNAlertService {
title, title,
confirmButtonText, confirmButtonText,
cancelButtonText, cancelButtonText,
confirmButtonStyle: confirmButtonType === ButtonType.Danger ? 'danger' : 'info' confirmButtonStyle:
confirmButtonType === ButtonType.Danger ? 'danger' : 'info',
}); });
} }