Skip to content
This repository was archived by the owner on Jul 15, 2022. It is now read-only.

Commit 92e63a0

Browse files
author
Leonardo Chaia
committed
chore: added a dialog service for confirmation and errors
1 parent a31bf24 commit 92e63a0

File tree

6 files changed

+160
-0
lines changed

6 files changed

+160
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { MatDialogRef } from '@angular/material';
2+
import { TimDialogRendererComponent } from './tim-dialog-renderer/tim-dialog-renderer.component';
3+
4+
export interface TimDialogData {
5+
title?: string;
6+
titleClass: string;
7+
message: string;
8+
confirmButton: TimDialogButtonConfiguration;
9+
cancelButton: TimDialogButtonConfiguration;
10+
debuggingInfo?: any;
11+
}
12+
13+
export interface TimDialogButtonConfiguration {
14+
text: string;
15+
icon: string;
16+
action?: (dialog?: MatDialogRef<TimDialogRendererComponent>) => void;
17+
color?: 'primary' | 'accent' | 'warn';
18+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<div class="h-100"
2+
fxLayout="column">
3+
4+
<h1 fxFlex
5+
mat-dialog-title
6+
*ngIf="data.title"
7+
[ngClass]="data.titleClass">
8+
{{data.title}}
9+
</h1>
10+
11+
<div>
12+
<h2>
13+
{{data.message}}
14+
</h2>
15+
16+
<ng-container *ngIf="data.debuggingInfo">
17+
<pre style="background: #ccc;
18+
padding: 10px;
19+
white-space: pre-wrap;
20+
border-radius: 5px;">{{data.debuggingInfo|json}}</pre>
21+
</ng-container>
22+
</div>
23+
24+
<div fxFlex>
25+
<button mat-raised-button
26+
(click)="onConfirm()"
27+
[color]="data.confirmButton.color"
28+
*ngIf="data.confirmButton">
29+
<mat-icon *ngIf="data.confirmButton.icon">{{data.confirmButton.icon}}</mat-icon>
30+
{{data.confirmButton.text}}
31+
</button>
32+
<button mat-raised-button
33+
*ngIf="data.cancelButton"
34+
[color]="data.cancelButton.color"
35+
(click)="onClose()"
36+
style="float: right;">
37+
<mat-icon *ngIf="data.cancelButton.icon">{{data.cancelButton.icon}}</mat-icon>
38+
{{data.cancelButton.text}}
39+
</button>
40+
</div>
41+
42+
</div>

src/app/tim-dialog/tim-dialog-renderer/tim-dialog-renderer.component.scss

Whitespace-only changes.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { Component, OnInit, Inject } from '@angular/core';
2+
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material';
3+
import { TimDialogData } from '../tim-dialog-model';
4+
5+
@Component({
6+
selector: 'tim-dialog-renderer',
7+
templateUrl: './tim-dialog-renderer.component.html',
8+
styleUrls: ['./tim-dialog-renderer.component.scss']
9+
})
10+
export class TimDialogRendererComponent implements OnInit {
11+
12+
constructor(
13+
private dialog: MatDialogRef<TimDialogRendererComponent>,
14+
@Inject(MAT_DIALOG_DATA)
15+
public data: TimDialogData) { }
16+
17+
public ngOnInit() {
18+
}
19+
20+
public onConfirm() {
21+
if (this.data.confirmButton && this.data.confirmButton.action) {
22+
this.data.confirmButton.action(this.dialog);
23+
}
24+
this.dialog.close();
25+
}
26+
27+
public onClose() {
28+
if (this.data.cancelButton && this.data.cancelButton.action) {
29+
this.data.cancelButton.action(this.dialog);
30+
}
31+
this.dialog.close();
32+
}
33+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { NgModule } from '@angular/core';
2+
import { CommonModule } from '@angular/common';
3+
import { MatDialogModule, MatButtonModule, MatIconModule, MatInputModule } from '@angular/material';
4+
import { FlexLayoutModule } from '@angular/flex-layout';
5+
import { FormsModule } from '@angular/forms';
6+
import { TimDialogRendererComponent } from './tim-dialog-renderer/tim-dialog-renderer.component';
7+
import { TimDialogService } from './tim-dialog.service';
8+
9+
@NgModule({
10+
imports: [
11+
CommonModule,
12+
FormsModule,
13+
14+
MatDialogModule,
15+
MatButtonModule,
16+
MatInputModule,
17+
MatIconModule,
18+
FlexLayoutModule,
19+
],
20+
declarations: [
21+
TimDialogRendererComponent
22+
],
23+
entryComponents: [
24+
TimDialogRendererComponent,
25+
],
26+
providers: [
27+
TimDialogService,
28+
]
29+
})
30+
export class TimDialogModule { }
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { Injectable } from '@angular/core';
2+
import { MatDialog } from '@angular/material';
3+
import { TimDialogData } from './tim-dialog-model';
4+
import { TimDialogRendererComponent } from './tim-dialog-renderer/tim-dialog-renderer.component';
5+
6+
@Injectable()
7+
export class TimDialogService {
8+
9+
constructor(private dialog: MatDialog) { }
10+
11+
public openErrorMessageModal(title: string, error: string, debuggingInfo?: string) {
12+
return this.openMessageModal({
13+
title: title,
14+
titleClass: 'mat-text-warn',
15+
message: error,
16+
debuggingInfo: debuggingInfo,
17+
cancelButton: {
18+
text: 'Close',
19+
icon: 'close',
20+
}
21+
});
22+
}
23+
24+
public openMessageModal(data: Partial<TimDialogData>) {
25+
if (!data.cancelButton) {
26+
data.cancelButton = {
27+
text: 'Cancel',
28+
icon: 'close',
29+
};
30+
}
31+
32+
return this.dialog.open(TimDialogRendererComponent, {
33+
data: data,
34+
panelClass: 'mat-typography',
35+
});
36+
}
37+
}

0 commit comments

Comments
 (0)