-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NAS-125917: Ask user when they try to close the form after making edits
- Loading branch information
1 parent
07149af
commit 00066b7
Showing
93 changed files
with
263 additions
and
2 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
src/app/directives/form-change-guard/form-change-guard-for-slide-in.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,81 @@ | ||
import { | ||
Directive, Input, OnInit, | ||
} from '@angular/core'; | ||
import { FormGroup } from '@angular/forms'; | ||
import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy'; | ||
import { TranslateService } from '@ngx-translate/core'; | ||
import { Observable, of } from 'rxjs'; | ||
import { filter, switchMap } from 'rxjs/operators'; | ||
import { DialogService } from 'app/modules/dialog/dialog.service'; | ||
import { IxSlideInRef } from 'app/modules/forms/ix-forms/components/ix-slide-in/ix-slide-in-ref'; | ||
|
||
@UntilDestroy() | ||
@Directive({ | ||
selector: '[formChangeGuardForSlideIn]', | ||
standalone: true, | ||
}) | ||
export class FormChangeGuardForSlideInDirective<T> implements OnInit { | ||
@Input() formGroup: FormGroup; | ||
|
||
private formChanged = false; | ||
|
||
constructor( | ||
private translate: TranslateService, | ||
private dialogService: DialogService, | ||
private slideInRef: IxSlideInRef<T>, | ||
) {} | ||
|
||
ngOnInit(): void { | ||
this.trackFormChanges(); | ||
this.overrideSlideInClose(); | ||
} | ||
|
||
private trackFormChanges(): void { | ||
this.formGroup.valueChanges | ||
.pipe( | ||
filter(() => !this.formGroup.pristine), | ||
untilDestroyed(this), | ||
) | ||
.subscribe(() => { | ||
this.formChanged = true; | ||
}); | ||
} | ||
|
||
private overrideSlideInClose(): void { | ||
this.slideInRef.close = (response?: T) => this.closeWithConfirmation(response) | ||
.pipe(untilDestroyed(this)) | ||
.subscribe(); | ||
} | ||
|
||
private closeWithConfirmation(response?: T): Observable<boolean> { | ||
if (!this.formChanged) { | ||
this.emitClose(response); | ||
return of(true); | ||
} | ||
|
||
return this.showConfirmDialog().pipe( | ||
switchMap((shouldClose) => { | ||
if (shouldClose) { | ||
this.formChanged = false; | ||
this.emitClose(response); | ||
} | ||
return of(shouldClose); | ||
}), | ||
); | ||
} | ||
|
||
private showConfirmDialog(): Observable<boolean> { | ||
return this.dialogService.confirm({ | ||
title: this.translate.instant('Unsaved Changes'), | ||
message: this.translate.instant('You have unsaved changes. Are you sure you want to close?'), | ||
cancelText: this.translate.instant('No'), | ||
buttonText: this.translate.instant('Yes'), | ||
hideCheckbox: true, | ||
}); | ||
} | ||
|
||
private emitClose(response?: T): void { | ||
this.slideInRef.slideInClosed$.next(response); | ||
this.slideInRef.slideInClosed$.complete(); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
src/app/pages/credentials/users/user-form/user-form.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
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
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
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
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
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
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
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
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
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
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
Oops, something went wrong.