-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Answer:48): avoid losing form data solution
- Loading branch information
Showing
7 changed files
with
81 additions
and
10 deletions.
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
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,11 @@ | ||
import { ApplicationConfig } from '@angular/core'; | ||
import { provideRouter, withComponentInputBinding } from '@angular/router'; | ||
import { appRoutes } from './app.routes'; | ||
import { PendingChangesGuard } from './guards/pending-changes.guard'; | ||
|
||
export const appConfig: ApplicationConfig = { | ||
providers: [provideRouter(appRoutes, withComponentInputBinding())], | ||
providers: [ | ||
provideRouter(appRoutes, withComponentInputBinding()), | ||
PendingChangesGuard, | ||
], | ||
}; |
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
15 changes: 15 additions & 0 deletions
15
apps/forms/form-dialog-alert/src/app/guards/pending-changes.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,15 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { Observable } from 'rxjs'; | ||
|
||
export interface ComponentCanDeactivate { | ||
canDeactivate: () => boolean | Observable<boolean>; | ||
} | ||
|
||
@Injectable() | ||
export class PendingChangesGuard { | ||
canDeactivate( | ||
component: ComponentCanDeactivate, | ||
): boolean | Observable<boolean> { | ||
return component.canDeactivate(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
apps/forms/form-dialog-alert/src/app/guards/unload-window.directive.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,13 @@ | ||
import { Directive, HostListener, inject } from '@angular/core'; | ||
import { Observable } from 'rxjs'; | ||
import { FormComponent } from '../ui/form.component'; | ||
|
||
@Directive({ selector: 'app-form[appUnloadGuard]', standalone: true }) | ||
export class UnloadGuardDirective { | ||
readonly component = inject(FormComponent); | ||
|
||
@HostListener('window:beforeunload') | ||
canDeactivate(): Observable<boolean> | boolean { | ||
return !this.component.form!.dirty; | ||
} | ||
} |
32 changes: 28 additions & 4 deletions
32
apps/forms/form-dialog-alert/src/app/pages/join.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,16 +1,40 @@ | ||
import { ChangeDetectionStrategy, Component } from '@angular/core'; | ||
import { | ||
ChangeDetectionStrategy, | ||
Component, | ||
inject, | ||
viewChild, | ||
} from '@angular/core'; | ||
import { MatDialog } from '@angular/material/dialog'; | ||
import { Observable, of } from 'rxjs'; | ||
import { PendingChangesGuard } from '../guards/pending-changes.guard'; | ||
import { UnloadGuardDirective } from '../guards/unload-window.directive'; | ||
import { AlertDialogComponent } from '../ui/dialog.component'; | ||
import { FormComponent } from '../ui/form.component'; | ||
|
||
@Component({ | ||
standalone: true, | ||
imports: [FormComponent], | ||
imports: [FormComponent, UnloadGuardDirective], | ||
template: ` | ||
<section class="mx-auto max-w-screen-sm"> | ||
<div class="rounded-lg bg-white p-8 shadow-lg lg:p-12"> | ||
<app-form /> | ||
<app-form appUnloadGuard /> | ||
</div> | ||
</section> | ||
`, | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class JoinComponent {} | ||
export class JoinComponent implements PendingChangesGuard { | ||
private dialog = inject(MatDialog); | ||
formComponent = viewChild(FormComponent); | ||
|
||
canDeactivate(): boolean | Observable<boolean> { | ||
if (this.formComponent()!.form.dirty) { | ||
const dialogRef = this.dialog.open(AlertDialogComponent, { | ||
disableClose: true, | ||
}); | ||
return dialogRef.afterClosed(); | ||
} else { | ||
return of(true); | ||
} | ||
} | ||
} |
18 changes: 14 additions & 4 deletions
18
apps/forms/form-dialog-alert/src/app/ui/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