// Copyright (c) Cratis. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. import { DialogResponse, DialogResult, IDialogComponents, ConfirmationDialogRequest, BusyIndicatorDialogRequest, CloseDialog } from '@cratis/applications.react/dialogs'; import { DialogButtons } from '@cratis/applications.react/dialogs/DialogButtons'; import { IDialogs } from './IDialogs'; import { BusyIndicator } from './BusyIndicator'; import { IDialogMediatorHandler } from './IDialogMediatorHandler'; /** * Represents an implementation of {@link IDialogs}. */ export class Dialogs extends IDialogs { /** * Initializes a new instance of the {@link Dialogs} class. */ constructor( private readonly _dialogMediatorHandler: IDialogMediatorHandler, dialogComponents: IDialogComponents) { super(); /* eslint-disable @typescript-eslint/no-empty-function */ _dialogMediatorHandler.subscribe(ConfirmationDialogRequest, async (request, resolver) => { const [result] = await dialogComponents.showConfirmation(request as ConfirmationDialogRequest); resolver(result); }, () => { }); /* eslint-enable @typescript-eslint/no-empty-function */ let busyIndicatorResolver: CloseDialog | undefined = undefined; _dialogMediatorHandler.subscribe(BusyIndicatorDialogRequest, (request, resolver) => { busyIndicatorResolver = resolver; return dialogComponents.showBusyIndicator(request as BusyIndicatorDialogRequest); }, () => { busyIndicatorResolver?.(DialogResult.Cancelled, undefined); }); } /** @inheritdoc */ show(request: TRequest): Promise> { return this._dialogMediatorHandler.show(request); } /** @inheritdoc */ async showConfirmation(title: string, message: string, buttons: DialogButtons): Promise { const [result] = await this.show(new ConfirmationDialogRequest(title, message, buttons)); return result; } /** @inheritdoc */ showBusyIndicator(title: string, message: string): BusyIndicator { this.show(new BusyIndicatorDialogRequest(title, message)); const registration = this._dialogMediatorHandler.getRegistration(BusyIndicatorDialogRequest); const busyIndicator = new BusyIndicator(registration.resolver); return busyIndicator; } }