refactor: begin splitting alertService into standalone functions
This commit is contained in:
@@ -2,6 +2,45 @@
|
|||||||
import { SNAlertService } from 'snjs';
|
import { SNAlertService } 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 */
|
||||||
|
export function confirmDialog({
|
||||||
|
text,
|
||||||
|
title,
|
||||||
|
confirmButtonText = 'Confirm',
|
||||||
|
cancelButtonText = 'Cancel',
|
||||||
|
confirmButtonStyle = 'info'
|
||||||
|
}: {
|
||||||
|
text: string,
|
||||||
|
title?: string,
|
||||||
|
confirmButtonText?: string,
|
||||||
|
cancelButtonText?: string,
|
||||||
|
confirmButtonStyle?: 'danger' | 'info'
|
||||||
|
}) {
|
||||||
|
return new Promise<boolean>((resolve) => {
|
||||||
|
const alert = new SKAlert({
|
||||||
|
title,
|
||||||
|
text,
|
||||||
|
buttons: [
|
||||||
|
{
|
||||||
|
text: cancelButtonText,
|
||||||
|
style: 'neutral',
|
||||||
|
action() {
|
||||||
|
resolve(false);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
text: confirmButtonText,
|
||||||
|
style: confirmButtonStyle,
|
||||||
|
action() {
|
||||||
|
resolve(true);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
alert.present();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
export class AlertService extends SNAlertService {
|
export class AlertService extends SNAlertService {
|
||||||
async alert(
|
async alert(
|
||||||
text: string,
|
text: string,
|
||||||
@@ -13,20 +52,21 @@ export class AlertService extends SNAlertService {
|
|||||||
const buttons = [
|
const buttons = [
|
||||||
{
|
{
|
||||||
text: closeButtonText,
|
text: closeButtonText,
|
||||||
style: "neutral",
|
style: 'neutral',
|
||||||
action: async () => {
|
action: async () => {
|
||||||
if (onClose) {
|
if (onClose) {
|
||||||
this.deviceInterface!.timeout(onClose);
|
this.deviceInterface!.timeout(onClose);
|
||||||
}
|
}
|
||||||
resolve(true);
|
resolve(true);
|
||||||
}
|
},
|
||||||
}
|
},
|
||||||
];
|
];
|
||||||
const alert = new SKAlert({ title, text, buttons });
|
const alert = new SKAlert({ title, text, buttons });
|
||||||
alert.present();
|
alert.present();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @deprecated use confirmDialog instead */
|
||||||
async confirm(
|
async confirm(
|
||||||
text: string,
|
text: string,
|
||||||
title: string,
|
title: string,
|
||||||
@@ -36,31 +76,18 @@ export class AlertService extends SNAlertService {
|
|||||||
onCancel: () => void,
|
onCancel: () => void,
|
||||||
destructive = false
|
destructive = false
|
||||||
) {
|
) {
|
||||||
return new Promise((resolve, reject) => {
|
const confirmed = await confirmDialog({
|
||||||
const buttons = [
|
text,
|
||||||
{
|
title,
|
||||||
text: cancelButtonText,
|
confirmButtonText,
|
||||||
style: "neutral",
|
cancelButtonText,
|
||||||
action: async () => {
|
confirmButtonStyle: destructive ? 'danger' : 'info'
|
||||||
if (onCancel) {
|
|
||||||
this.deviceInterface!.timeout(onCancel);
|
|
||||||
}
|
|
||||||
reject(false);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
text: confirmButtonText,
|
|
||||||
style: destructive ? "danger" : "info",
|
|
||||||
action: async () => {
|
|
||||||
if (onConfirm) {
|
|
||||||
this.deviceInterface!.timeout(onConfirm);
|
|
||||||
}
|
|
||||||
resolve(true);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
];
|
|
||||||
const alert = new SKAlert({ title, text, buttons });
|
|
||||||
alert.present();
|
|
||||||
});
|
});
|
||||||
|
if (confirmed) {
|
||||||
|
onConfirm();
|
||||||
|
} else {
|
||||||
|
onCancel();
|
||||||
|
}
|
||||||
|
return confirmed;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user