-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Answer:55 #1037
Open
tsironis13
wants to merge
4
commits into
tomalaforge:main
Choose a base branch
from
tsironis13:angular-back-button-navigation-challenge
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Answer:55 #1037
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a5ea6bd
feat: fix angular back button navigation challenge
tsironis13 5e1a1bc
feat: angular back button navigation challenge proposed solution
tsironis13 cb6fdb8
Merge branch 'main' into angular-back-button-navigation-challenge
fefbb7b
Merge branch 'main' into angular-back-button-navigation-challenge
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
apps/angular/55-back-button-navigation/src/app/base-dialog.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { | ||
AfterContentInit, | ||
ChangeDetectionStrategy, | ||
Component, | ||
inject, | ||
} from '@angular/core'; | ||
import { MAT_DIALOG_DATA } from '@angular/material/dialog'; | ||
import { DialogData, DialogStrategyType } from './dialog-strategy'; | ||
import { DialogService } from './dialog.service'; | ||
|
||
@Component({ | ||
// eslint-disable-next-line @angular-eslint/component-selector | ||
selector: '', | ||
template: '', | ||
standalone: true, | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class BaseDialogComponent implements AfterContentInit { | ||
readonly #data = inject<DialogData>(MAT_DIALOG_DATA); | ||
protected readonly dialogService = inject(DialogService); | ||
|
||
ngAfterContentInit(): void { | ||
this.initStrategy(); | ||
} | ||
|
||
private initStrategy() { | ||
const strategyType: DialogStrategyType = this.#data?.strategy?.type | ||
? this.#data?.strategy?.type | ||
: 'default'; | ||
|
||
this.dialogService.setStrategy(strategyType); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
apps/angular/55-back-button-navigation/src/app/can-deactivate.guard.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { CanDeactivateFn, UrlTree } from '@angular/router'; | ||
import { Observable } from 'rxjs'; | ||
|
||
export type CanDeactivateType = | ||
| Observable<boolean | UrlTree> | ||
| Promise<boolean | UrlTree> | ||
| boolean | ||
| UrlTree; | ||
|
||
export interface CanComponentDeactivate { | ||
canDeactivate: () => CanDeactivateType; | ||
} | ||
|
||
export const canDeactivateGuard: CanDeactivateFn<CanComponentDeactivate> = ( | ||
component: CanComponentDeactivate, | ||
) => { | ||
return component.canDeactivate ? component.canDeactivate() : true; | ||
}; | ||
19 changes: 19 additions & 0 deletions
19
apps/angular/55-back-button-navigation/src/app/confirm-dialog/confirm-dialog.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<h2 mat-dialog-title>Confirm Action</h2> | ||
<mat-dialog-content> | ||
Are you sure you want to perform this action? | ||
</mat-dialog-content> | ||
<mat-dialog-actions> | ||
<button | ||
mat-button | ||
mat-dialog-close | ||
(click)="dialogService.closeActiveDialog()"> | ||
No | ||
</button> | ||
<button | ||
mat-button | ||
mat-dialog-close | ||
cdkFocusInitial | ||
(click)="dialogService.closeAll()"> | ||
Yes | ||
</button> | ||
</mat-dialog-actions> |
28 changes: 28 additions & 0 deletions
28
apps/angular/55-back-button-navigation/src/app/confirm-dialog/confirm-dialog.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { ChangeDetectionStrategy, Component } from '@angular/core'; | ||
import { MatButtonModule } from '@angular/material/button'; | ||
import { | ||
MatDialogActions, | ||
MatDialogClose, | ||
MatDialogContent, | ||
MatDialogTitle, | ||
} from '@angular/material/dialog'; | ||
import { BaseDialogComponent } from '../base-dialog'; | ||
|
||
@Component({ | ||
selector: 'app-confirm-dialog', | ||
templateUrl: './confirm-dialog.component.html', | ||
standalone: true, | ||
imports: [ | ||
MatButtonModule, | ||
MatDialogActions, | ||
MatDialogClose, | ||
MatDialogTitle, | ||
MatDialogContent, | ||
], | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class ConfirmDialogComponent extends BaseDialogComponent { | ||
constructor() { | ||
super(); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
apps/angular/55-back-button-navigation/src/app/dialog-strategy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { Injectable, inject } from '@angular/core'; | ||
import { ConfirmDialogComponent } from './confirm-dialog/confirm-dialog.component'; | ||
import { DialogService } from './dialog.service'; | ||
|
||
export type DialogStrategyType = 'default' | 'confirm'; | ||
|
||
export type DialogData = { | ||
strategy: { | ||
type: DialogStrategyType; | ||
}; | ||
}; | ||
|
||
export abstract class DialogStrategy { | ||
protected readonly dialogService = inject(DialogService); | ||
|
||
abstract onBackBrowserNavigation(): boolean; | ||
} | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class DefaultDialogStrategy extends DialogStrategy { | ||
override onBackBrowserNavigation(): boolean { | ||
this.dialogService.closeActiveDialog(); | ||
|
||
return false; | ||
} | ||
} | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class ConfirmDialogStrategy extends DialogStrategy { | ||
override onBackBrowserNavigation(): boolean { | ||
this.dialogService.openDialog(ConfirmDialogComponent, { | ||
width: '250px', | ||
closeOnNavigation: false, | ||
}); | ||
|
||
return false; | ||
} | ||
} | ||
|
||
export const DialogStrategyMap = new Map<DialogStrategyType, any>([ | ||
['default', DefaultDialogStrategy], | ||
['confirm', ConfirmDialogStrategy], | ||
]); |
109 changes: 109 additions & 0 deletions
109
apps/angular/55-back-button-navigation/src/app/dialog.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { ComponentType } from '@angular/cdk/portal'; | ||
import { Injectable, Injector, inject } from '@angular/core'; | ||
import { MatDialog, MatDialogConfig } from '@angular/material/dialog'; | ||
import { take, tap } from 'rxjs'; | ||
import { | ||
DialogStrategy, | ||
DialogStrategyMap, | ||
DialogStrategyType, | ||
} from './dialog-strategy'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class DialogService { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice service, a bit out of the challenge, but great generic service for people to add to their projects 👍 |
||
readonly #dialog = inject(MatDialog); | ||
readonly #dialogsStrategyState: { | ||
id: string; | ||
type: DialogStrategy; | ||
active: boolean; | ||
}[] = []; | ||
|
||
readonly #injector = inject(Injector); | ||
|
||
openDialog<T extends ComponentType<any>, Z extends MatDialogConfig<any>>( | ||
component: T, | ||
config: Z, | ||
) { | ||
const dialogRef = this.#dialog.open<T, Z>(component, config); | ||
|
||
dialogRef | ||
.afterClosed() | ||
.pipe( | ||
take(1), | ||
tap(() => this.removeDialogStrategyStateById(dialogRef.id)), | ||
) | ||
.subscribe(); | ||
} | ||
|
||
setStrategy(type: DialogStrategyType) { | ||
this.#dialog.openDialogs.forEach((d) => { | ||
const state = this.getDialogStrategyStateById(d.id); | ||
|
||
if (!state) { | ||
this.addDialogStrategyState(d.id, type); | ||
} | ||
}); | ||
} | ||
|
||
getStrategyType() { | ||
const active = this.getActiveDialog(); | ||
|
||
if (!active) { | ||
return null; | ||
} | ||
|
||
return this.getDialogStrategyStateById(active.id)?.type; | ||
} | ||
|
||
closeActiveDialog() { | ||
const activeDialog = this.#dialogsStrategyState.find((d) => d.active); | ||
|
||
if (!activeDialog) { | ||
return; | ||
} | ||
|
||
this.#dialog.getDialogById(activeDialog.id)?.close(); | ||
} | ||
|
||
closeAll() { | ||
this.#dialogsStrategyState.splice(0, this.#dialogsStrategyState.length); | ||
|
||
this.#dialog.closeAll(); | ||
} | ||
|
||
getActiveDialog() { | ||
return this.#dialogsStrategyState.find((d) => d.active); | ||
} | ||
|
||
addDialogStrategyState(id: string, type: DialogStrategyType) { | ||
const t = this.#injector.get<DialogStrategy>(DialogStrategyMap.get(type)); | ||
|
||
this.#dialogsStrategyState.map((x) => (x.active = false)); | ||
|
||
this.#dialogsStrategyState.push({ id, type: t, active: true }); | ||
} | ||
|
||
getDialogStrategyStateById(id: string) { | ||
return this.#dialogsStrategyState.find((s) => s.id === id); | ||
} | ||
|
||
removeDialogStrategyStateById(id: string) { | ||
const indexToRemove = this.#dialogsStrategyState.findIndex( | ||
(d) => d.id === id, | ||
); | ||
|
||
if (indexToRemove > 0) { | ||
this.#dialogsStrategyState[indexToRemove - 1] = { | ||
...this.#dialogsStrategyState[indexToRemove - 1], | ||
active: true, | ||
}; | ||
} | ||
|
||
this.#dialogsStrategyState.splice(indexToRemove, 1); | ||
} | ||
|
||
getDialogsStrategyState() { | ||
return this.#dialogsStrategyState; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
apps/angular/55-back-button-navigation/src/app/home/home.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
<a mat-raised-button routerLink="/simple-action" routerLinkActive="active"> | ||
<a mat-raised-button routerLink="/simple-action"> | ||
Go to simple dialog action page | ||
</a> | ||
|
||
<a mat-raised-button routerLink="/sensitive-action" routerLinkActive="active"> | ||
<a mat-raised-button routerLink="/sensitive-action"> | ||
Go to sensitive dialog action page | ||
</a> |
2 changes: 2 additions & 0 deletions
2
...ngular/55-back-button-navigation/src/app/sensitive-action/sensitive-action.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
<button mat-raised-button (click)="openDialog()"> | ||
Open dialog with confirmation dialog on browser back button click | ||
</button> | ||
|
||
<a mat-raised-button routerLink="/">Home</a> |
25 changes: 18 additions & 7 deletions
25
.../angular/55-back-button-navigation/src/app/sensitive-action/sensitive-action.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,30 @@ | ||
import { Component, inject } from '@angular/core'; | ||
import { MatButtonModule } from '@angular/material/button'; | ||
import { MatDialog } from '@angular/material/dialog'; | ||
import { DialogComponent } from '../dialog/dialog.component'; | ||
import { RouterLink } from '@angular/router'; | ||
import { CanComponentDeactivate } from '../can-deactivate.guard'; | ||
import { DialogService } from '../dialog.service'; | ||
import { SensitiveDialogComponent } from '../sensitive-dialog/sensitive-dialog.component'; | ||
|
||
@Component({ | ||
imports: [MatButtonModule], | ||
standalone: true, | ||
imports: [MatButtonModule, RouterLink], | ||
selector: 'app-sensitive-action', | ||
templateUrl: './sensitive-action.component.html', | ||
}) | ||
export class SensitiveActionComponent { | ||
readonly #dialog = inject(MatDialog); | ||
export class SensitiveActionComponent implements CanComponentDeactivate { | ||
readonly #dialogService = inject(DialogService); | ||
|
||
openDialog(): void { | ||
this.#dialog.open(DialogComponent, { | ||
width: '250px', | ||
this.#dialogService.openDialog(SensitiveDialogComponent, { | ||
width: '450px', | ||
data: { strategy: { type: 'confirm' } }, | ||
closeOnNavigation: false, | ||
}); | ||
} | ||
|
||
canDeactivate() { | ||
return ( | ||
this.#dialogService.getStrategyType()?.onBackBrowserNavigation() ?? true | ||
); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...ngular/55-back-button-navigation/src/app/sensitive-dialog/sensitive-dialog.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<h2 mat-dialog-title>Sensitive File Deletion</h2> | ||
<mat-dialog-content>Would you like to delete cat.jpeg?</mat-dialog-content> | ||
<mat-dialog-actions> | ||
<button mat-button mat-dialog-close>No</button> | ||
<button mat-button mat-dialog-close cdkFocusInitial>Ok</button> | ||
</mat-dialog-actions> |
28 changes: 28 additions & 0 deletions
28
.../angular/55-back-button-navigation/src/app/sensitive-dialog/sensitive-dialog.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { ChangeDetectionStrategy, Component } from '@angular/core'; | ||
import { MatButtonModule } from '@angular/material/button'; | ||
import { | ||
MatDialogActions, | ||
MatDialogClose, | ||
MatDialogContent, | ||
MatDialogTitle, | ||
} from '@angular/material/dialog'; | ||
import { BaseDialogComponent } from '../base-dialog'; | ||
|
||
@Component({ | ||
selector: 'app-sensitive-dialog', | ||
templateUrl: './sensitive-dialog.component.html', | ||
standalone: true, | ||
imports: [ | ||
MatButtonModule, | ||
MatDialogActions, | ||
MatDialogClose, | ||
MatDialogTitle, | ||
MatDialogContent, | ||
], | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class SensitiveDialogComponent extends BaseDialogComponent { | ||
constructor() { | ||
super(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍