-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NAS-125917 / 25.04 / Ask user when they try to close the form after m…
…aking edits (#10844) * NAS-125917: Ask user when they try to close the form after making edits * NAS-125917: PR Update * NAS-125917: PR Update * NAS-125917: PR Update
- Loading branch information
1 parent
751868c
commit d9ab633
Showing
94 changed files
with
329 additions
and
2 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
src/app/directives/warn-about-unsaved-changes/warn-about-unsaved-changes.directive.spec.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,65 @@ | ||
import { FormGroup, ReactiveFormsModule } from '@angular/forms'; | ||
import { | ||
createHostFactory, SpectatorHost, mockProvider, | ||
} from '@ngneat/spectator/jest'; | ||
import { of, Subject } from 'rxjs'; | ||
import { DialogService } from 'app/modules/dialog/dialog.service'; | ||
import { IxSlideInRef } from 'app/modules/forms/ix-forms/components/ix-slide-in/ix-slide-in-ref'; | ||
import { WarnAboutUnsavedChangesDirective } from './warn-about-unsaved-changes.directive'; | ||
|
||
describe('WarnAboutUnsavedChangesDirective', () => { | ||
let spectator: SpectatorHost<WarnAboutUnsavedChangesDirective<unknown>>; | ||
|
||
const createHost = createHostFactory({ | ||
component: WarnAboutUnsavedChangesDirective, | ||
imports: [ReactiveFormsModule], | ||
providers: [ | ||
mockProvider(DialogService, { | ||
confirm: jest.fn(() => of(true)), | ||
}), | ||
{ | ||
provide: IxSlideInRef, | ||
useFactory: () => ({ | ||
close: jest.fn(), | ||
slideInClosed$: new Subject<void>(), | ||
}), | ||
}, | ||
], | ||
}); | ||
|
||
beforeEach(() => { | ||
spectator = createHost(` | ||
<form [formGroup]="form" warnAboutUnsavedChanges></form> | ||
`, { | ||
hostProps: { | ||
form: new FormGroup({}), | ||
}, | ||
}); | ||
}); | ||
|
||
it('should set formChanged to true when form value changes', () => { | ||
spectator.component.formGroup.markAsPristine(); | ||
|
||
spectator.component.formGroup.valueChanges.subscribe(() => { | ||
expect(spectator.component.formChanged).toBe(true); | ||
}); | ||
}); | ||
|
||
it('should emit close event if there are no unsaved changes', () => { | ||
spectator.component.formGroup.markAsPristine(); | ||
|
||
spectator.detectChanges(); | ||
|
||
spectator.component.closeWithConfirmation().subscribe((shouldClose) => { | ||
expect(shouldClose).toBe(true); | ||
}); | ||
}); | ||
|
||
it('should call confirmation dialog if there are unsaved changes', () => { | ||
const dialogService = spectator.inject(DialogService); | ||
|
||
spectator.component.closeWithConfirmation().subscribe(() => { | ||
expect(dialogService.confirm).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
82 changes: 82 additions & 0 deletions
82
src/app/directives/warn-about-unsaved-changes/warn-about-unsaved-changes.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,82 @@ | ||
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: '[warnAboutUnsavedChanges]', | ||
standalone: true, | ||
}) | ||
export class WarnAboutUnsavedChangesDirective<T> implements OnInit { | ||
@Input() formGroup: FormGroup; | ||
|
||
formChanged = false; | ||
|
||
constructor( | ||
private translate: TranslateService, | ||
private dialogService: DialogService, | ||
private slideInRef: IxSlideInRef<T>, | ||
) {} | ||
|
||
ngOnInit(): void { | ||
this.trackFormChanges(); | ||
this.overrideSlideInClose(); | ||
} | ||
|
||
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 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 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'), | ||
buttonColor: 'red', | ||
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
Oops, something went wrong.