I'm trying to implement custom confirm dialog functionality which is planned to be available app-wide - across all components.
For that I use Angular Material
.
The modal is a separate component which I resolve in another component in the following way:
const dialogRef = this.confirmDialog.open(ConfirmDialogComponent, ... });
The problem I face - with this approach I have to duplicate the code in every component. DRY principle
is violated.
For more details:
...
export class SearchComponent extends AppComponentBase {...
constructor(public confirmDialog: MatDialog, ...) { super(injector); }
confirm(title: string, message: string) {
var promise = new Promise((resolve, reject) => {
const dialogRef = this.confirmDialog.open(ConfirmDialogComponent, {
width: '250px',
data: { title: title, message: message }
});
dialogRef.afterClosed().subscribe(result => {
if (result) {
resolve();
} else {
reject();
}
});
});
return promise;
}
Obviously I could move the shared code to a base component - AppComponentBase
. Although there will still be bits of duplicated code - e.g. constructor-related code.
But, is there a better / cleaner approach in terms of software design to refactor what I have?
Thanks.