forked from tomalaforge/angular-challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsensitive-action.component.ts
More file actions
58 lines (51 loc) · 1.82 KB
/
Copy pathsensitive-action.component.ts
File metadata and controls
58 lines (51 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { Component, inject } from '@angular/core';
import { MatButtonModule } from '@angular/material/button';
import { MatDialog, MatDialogRef } from '@angular/material/dialog';
import { BackButtonNavigationHandler } from '../back-button-navigation-handler';
import { DialogComponent } from '../dialog/dialog.component';
@Component({
imports: [MatButtonModule],
selector: 'app-sensitive-action',
templateUrl: './sensitive-action.component.html',
})
export class SensitiveActionComponent implements BackButtonNavigationHandler {
readonly #dialog = inject(MatDialog);
#initialDialogRef: MatDialogRef<DialogComponent> | null = null;
#confirmationDialogRef: MatDialogRef<DialogComponent> | null = null;
openDialog(): void {
this.#initialDialogRef = this.#dialog.open(DialogComponent, {
width: '250px',
closeOnNavigation: false,
});
this.#initialDialogRef.afterClosed().subscribe(() => {
this.#initialDialogRef = null;
this.#confirmationDialogRef?.close();
this.#confirmationDialogRef = null;
});
}
canDeactivate(): boolean {
return !this.#initialDialogRef && !this.#confirmationDialogRef;
}
handleBackNavigation(): void {
if (this.#confirmationDialogRef) {
this.#confirmationDialogRef.close();
return;
}
if (this.#initialDialogRef) {
this.#confirmationDialogRef = this.#dialog.open(DialogComponent, {
width: '250px',
closeOnNavigation: false,
data: {
title: 'Confirm navigation',
content:
'A sensitive dialog is still open. Press Back again to close this confirmation and stay here.',
cancelLabel: 'Stay',
confirmLabel: 'Ok',
},
});
this.#confirmationDialogRef.afterClosed().subscribe(() => {
this.#confirmationDialogRef = null;
});
}
}
}