Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { ProfileFormComponent } from './profile-form.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import {
COPY_PROFILE_MOCK,
DRAFT_COPY_PROFILE_MOCK,
NEW_PROFILE_MOCK,
NEW_PROFILE_MOCK_DRAFT,
OUTDATED_DRAFT_PROFILE_MOCK,
Expand Down Expand Up @@ -333,16 +332,6 @@ describe('ProfileFormComponent', () => {
component.nameControl.hasError('has_same_profile_name')
).toBeTrue();
});

it('should have an error when uses the name of copy profile', fakeAsync(() => {
component.selectedProfile = DRAFT_COPY_PROFILE_MOCK;
component.profiles = [PROFILE_MOCK, PROFILE_MOCK_2, COPY_PROFILE_MOCK];
fixture.detectChanges();

expect(
component.nameControl.hasError('has_same_profile_name')
).toBeTrue();
}));
});

describe('with no profile', () => {
Expand Down Expand Up @@ -372,7 +361,7 @@ describe('ProfileFormComponent', () => {
ariaLabel: 'Discard the Risk Assessment changes',
data: {
title: 'Discard changes?',
content: `You have unsaved changes that would be permanently lost.`,
content: `You have unsaved changes in the New risk profile that would be permanently lost.`,
confirmName: 'Discard',
},
autoFocus: true,
Expand All @@ -397,9 +386,8 @@ describe('ProfileFormComponent', () => {
deleteCopyEmitSpy = spyOn(component.deleteCopy, 'emit');
});

it('should set isOpenProfile to false and return of(true) if profileHasNoChanges is true and not isCopyProfile', done => {
it('should set isOpenProfile to false and return of(true) if profileHasNoChanges is true and not copyProfile', done => {
spyOn(component, 'profileHasNoChanges').and.returnValue(true);
component.isCopyProfile = false;
component.profileForm.markAsDirty();

component.close().subscribe(result => {
Expand All @@ -411,10 +399,9 @@ describe('ProfileFormComponent', () => {
});
});

it('should set isOpenProfile to false and return of(true) if profileForm is pristine and not isCopyProfile', done => {
it('should set isOpenProfile to false and return of(true) if profileForm is pristine and not copyProfile', done => {
spyOn(component, 'profileHasNoChanges').and.returnValue(false);
component.profileForm.markAsPristine();
component.isCopyProfile = false;

component.close().subscribe(result => {
expect(result).toBeTrue();
Expand All @@ -425,10 +412,9 @@ describe('ProfileFormComponent', () => {
});
});

it('should open dialog if there are changes and not isCopyProfile, and dialog confirms (returns true)', done => {
it('should open dialog if there are changes and not copyProfile, and dialog confirms (returns true)', done => {
spyOn(component, 'profileHasNoChanges').and.returnValue(false);
component.profileForm.markAsDirty();
component.isCopyProfile = false;
openDialogSpy.and.returnValue(of(true));

component.close().subscribe(result => {
Expand All @@ -440,10 +426,9 @@ describe('ProfileFormComponent', () => {
});
});

it('should open dialog if there are changes and not isCopyProfile, and dialog cancels (returns false)', done => {
it('should open dialog if there are changes and not copyProfile, and dialog cancels (returns false)', done => {
spyOn(component, 'profileHasNoChanges').and.returnValue(false);
component.profileForm.markAsDirty();
component.isCopyProfile = false;
openDialogSpy.and.returnValue(of(false));

component.close().subscribe(result => {
Expand All @@ -460,7 +445,7 @@ describe('ProfileFormComponent', () => {
spyOn(component, 'profileHasNoChanges').and.returnValue(false);
component.profileForm.markAsDirty();
component.isCopyProfile = true;
component.selectedProfile = { ...PROFILE_MOCK };
component.selectedProfile = { ...COPY_PROFILE_MOCK };
openDialogSpy.and.returnValue(of(true));

component.close().subscribe(result => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
afterNextRender,
AfterViewInit,
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
EventEmitter,
inject,
Expand Down Expand Up @@ -80,7 +81,9 @@ export class ProfileFormComponent implements OnInit, AfterViewInit {
private profileValidators = inject(ProfileValidators);
private fb = inject(FormBuilder);
private store = inject(RiskAssessmentStore);
cd = inject(ChangeDetectorRef);
private profile: Profile | null = null;
private copyProfile: Profile | null = null;
private profileList!: Profile[];
private injector = inject(Injector);
private nameValidator!: ValidatorFn;
Expand All @@ -103,17 +106,23 @@ export class ProfileFormComponent implements OnInit, AfterViewInit {
}
@Input()
set selectedProfile(profile: Profile | null) {
if (this.isCopyProfile && this.profile) {
this.deleteCopy.emit(this.profile);
if (profile?.name.trim().startsWith('Copy')) {
this.copyProfile = profile;
}
if (this.changeProfile || this.profileHasNoChanges()) {
this.changeProfile = false;
this.profile = profile;
if (profile && this.nameControl) {
this.updateNameValidator(profile);
this.fillProfileForm(this.profileFormat, profile);
if (this.isCopyProfile && this.copyProfile) {
this.setCopy.emit(this.copyProfile);
}
} else {
this.profileForm.reset();
if (this.copyProfile) {
this.setCopy.emit(this.copyProfile);
}
}
} else if (this.profile != profile) {
// prevent select profile before user confirmation
Expand All @@ -128,6 +137,7 @@ export class ProfileFormComponent implements OnInit, AfterViewInit {

@Output() saveProfile = new EventEmitter<Profile>();
@Output() deleteCopy = new EventEmitter<Profile>();
@Output() setCopy = new EventEmitter<Profile>();
@Output() discard = new EventEmitter();
ngOnInit() {
this.profileForm = this.createProfileForm();
Expand Down Expand Up @@ -207,7 +217,7 @@ export class ProfileFormComponent implements OnInit, AfterViewInit {
}

private compareProfiles(profile1: Profile, profile2: Profile) {
if (profile1.name !== profile2.name || this.isCopyProfile) {
if (profile1.name !== profile2.name || this.copyProfile) {
return false;
}
if (
Expand Down Expand Up @@ -329,16 +339,16 @@ export class ProfileFormComponent implements OnInit, AfterViewInit {
close(): Observable<boolean> {
if (
(this.profileHasNoChanges() || this.profileForm.pristine) &&
!this.isCopyProfile
!this.copyProfile
) {
this.store.setIsOpenProfile(false);
return of(true);
}
return this.openCloseDialog().pipe(
tap(res => {
if (res) {
if (this.isCopyProfile && this.profile) {
this.deleteCopy.emit(this.profile);
if (this.copyProfile) {
this.deleteCopy.emit(this.copyProfile);
}
this.store.setIsOpenProfile(false);
}
Expand All @@ -348,11 +358,12 @@ export class ProfileFormComponent implements OnInit, AfterViewInit {
}

openCloseDialog() {
const profileName = this.profile?.name || 'New risk profile';
const dialogRef = this.dialog.open(SimpleDialogComponent, {
ariaLabel: 'Discard the Risk Assessment changes',
data: {
title: 'Discard changes?',
content: `You have unsaved changes that would be permanently lost.`,
content: `You have unsaved changes in the ${profileName} that would be permanently lost.`,
confirmName: 'Discard',
},
autoFocus: true,
Expand All @@ -367,6 +378,10 @@ export class ProfileFormComponent implements OnInit, AfterViewInit {
private openCloseDialogToChangeProfile(profile: Profile | null) {
this.openCloseDialog().subscribe(close => {
if (close) {
if (this.copyProfile) {
this.deleteCopy.emit(this.copyProfile);
this.copyProfile = null;
}
this.changeProfile = true;
this.store.updateSelectedProfile(profile);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
[profileFormat]="vm.profileFormat"
(saveProfile)="saveProfileClicked($event, vm.selectedProfile)"
(deleteCopy)="deleteCopy($event, vm.profiles)"
(setCopy)="setCopy($event, vm.profiles)"
(discard)="discard($event, vm.profiles)"></app-profile-form>
</ng-template>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,7 @@ describe('RiskAssessmentComponent', () => {
it('#copyProfileAndOpenForm should call openForm with copy of profile', fakeAsync(() => {
spyOn(component, 'openForm');

component.copyProfileAndOpenForm(PROFILE_MOCK, [
PROFILE_MOCK,
PROFILE_MOCK,
]);
component.copyProfileAndOpenForm(PROFILE_MOCK);
tick();

expect(component.openForm).toHaveBeenCalledWith(DRAFT_COPY_PROFILE_MOCK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,9 @@ export class RiskAssessmentComponent
this.cd.detectChanges();
}

async copyProfileAndOpenForm(profile: Profile, profiles: Profile[]) {
async copyProfileAndOpenForm(profile: Profile) {
this.isCopyProfile = true;
const copyOfProfile = this.getCopyOfProfile(profile);
this.store.updateProfiles([copyOfProfile, ...profiles]);
await this.openForm(copyOfProfile);
}

Expand Down Expand Up @@ -290,6 +289,11 @@ export class RiskAssessmentComponent
deleteCopy(copyOfProfile: Profile, profiles: Profile[]) {
this.isCopyProfile = false;
this.store.removeProfile(copyOfProfile.name, profiles);
this.cd.markForCheck();
}
setCopy(copyOfProfile: Profile, profiles: Profile[]) {
this.store.updateProfiles([copyOfProfile, ...profiles]);
this.cd.detectChanges();
}

actions(actions: EntityAction[]) {
Expand All @@ -312,7 +316,7 @@ export class RiskAssessmentComponent
) {
switch (action) {
case ProfileAction.Copy:
this.copyProfileAndOpenForm(entity, profiles);
this.copyProfileAndOpenForm(entity);
break;
case ProfileAction.Delete:
this.deleteProfile(entity, profiles, selectedProfile);
Expand Down