Skip to content

Commit 9ee8324

Browse files
makdenissmakdeniss
authored andcommitted
fix: address PR comments (#335)
1 parent 4b26591 commit 9ee8324

File tree

5 files changed

+17
-35
lines changed

5 files changed

+17
-35
lines changed

projects/wc/src/app/components/generic-ui/list-view/create-resource-modal/create-resource-modal.component.spec.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { CreateResourceModalComponent } from './create-resource-modal.component';
2-
import { DialogMode } from './create-resource-modal.enums';
32
import { CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA } from '@angular/core';
43
import { ComponentFixture, TestBed } from '@angular/core/testing';
54
import { ReactiveFormsModule } from '@angular/forms';
@@ -59,7 +58,7 @@ describe('CreateResourceModalComponent', () => {
5958
});
6059

6160
it('should open dialog when open method is called', () => {
62-
component.open(DialogMode.Create);
61+
component.open();
6362
expect(mockDialog.open).toBeTruthy();
6463
});
6564

@@ -82,7 +81,7 @@ describe('CreateResourceModalComponent', () => {
8281

8382
const updateSpy = spyOn(component.updateResource, 'emit');
8483

85-
component.open(DialogMode.Edit, resource);
84+
component.open(resource);
8685
expect(mockDialog.open).toBeTruthy();
8786

8887
expect(component.form.controls['metadata_name'].value).toBe('res1');
@@ -222,10 +221,10 @@ describe('CreateResourceModalComponent', () => {
222221
expect(component.isCreateFieldOnly(otherField)).toBeFalsy();
223222
});
224223

225-
it('should set header to Create and open dialog using open function', () => {
224+
it('should open dialog using open function', () => {
226225
mockDialog.open = false;
227226

228-
component.open(DialogMode.Create);
227+
component.open();
229228

230229
expect(mockDialog.open).toBeTruthy();
231230
});

projects/wc/src/app/components/generic-ui/list-view/create-resource-modal/create-resource-modal.component.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
import { DynamicSelectComponent } from '../../../dynamic-select/dynamic-select.component';
2-
import {
3-
CreateOnlyResourceFieldNames,
4-
DialogMode,
5-
} from './create-resource-modal.enums';
2+
import { CreateOnlyResourceFieldNames } from './create-resource-modal.enums';
63
import {
74
Component,
85
OnInit,
96
ViewEncapsulation,
10-
computed,
117
inject,
128
input,
139
output,
@@ -67,17 +63,14 @@ export class CreateResourceModalComponent implements OnInit {
6763
fb = inject(FormBuilder);
6864
form: FormGroup;
6965

70-
private mode = signal<DialogMode>(DialogMode.Create);
7166
private originalResource = signal<Resource | null>(null);
7267

7368
ngOnInit(): void {
7469
this.form = this.fb.group(this.createControls());
7570
}
7671

77-
open(dialogMode: DialogMode, resource?: Resource) {
78-
this.mode.set(dialogMode);
79-
80-
if (dialogMode === DialogMode.Edit && resource) {
72+
open(resource?: Resource) {
73+
if (resource) {
8174
this.prepareForEdit(resource);
8275
} else {
8376
this.prepareForCreate();
@@ -94,7 +87,6 @@ export class CreateResourceModalComponent implements OnInit {
9487
dialog.open = false;
9588
this.form.reset();
9689
this.setEditFieldsDisabled(false);
97-
this.mode.set(null);
9890
this.originalResource.set(null);
9991
}
10092
}
@@ -106,9 +98,9 @@ export class CreateResourceModalComponent implements OnInit {
10698
set(result, key.replaceAll('_', '.'), this.form.value[key]);
10799
}
108100

109-
if (this.isEditMode()) {
110-
const orig = this.originalResource();
111-
if (orig?.metadata) {
101+
const orig = this.originalResource();
102+
if (orig) {
103+
if (orig.metadata) {
112104
result['metadata'] = { ...orig.metadata, ...result['metadata'] };
113105
}
114106
this.updateResource.emit(result);
@@ -141,7 +133,9 @@ export class CreateResourceModalComponent implements OnInit {
141133
return (property as string).replaceAll('.', '_');
142134
}
143135

144-
isEditMode = computed(() => this.mode() === DialogMode.Edit);
136+
isEditMode() {
137+
return !!this.originalResource();
138+
}
145139

146140
isCreateFieldOnly(field: FieldDefinition): boolean {
147141
return (

projects/wc/src/app/components/generic-ui/list-view/create-resource-modal/create-resource-modal.enums.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
export enum DialogMode {
2-
Create = 'create',
3-
Edit = 'edit',
4-
}
5-
6-
export enum DialogHeaderText {
7-
Create = 'Create',
8-
Edit = 'Edit',
9-
}
10-
111
export enum CreateOnlyResourceFieldNames {
122
MetadataName = 'metadata.name',
133
SpecType = 'spec.type',

projects/wc/src/app/components/generic-ui/list-view/list-view.component.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ describe('ListViewComponent', () => {
120120
const openSpy = jest.fn();
121121
(component as any).createModal = () => ({ open: openSpy });
122122
component.openCreateResourceModal();
123-
expect(openSpy).toHaveBeenCalledWith('create');
123+
expect(openSpy).toHaveBeenCalledWith();
124124
});
125125

126126
it('should open delete resource modal and stop event propagation', () => {
@@ -144,7 +144,7 @@ describe('ListViewComponent', () => {
144144
component.openEditResourceModal(event, resource);
145145

146146
expect(event.stopPropagation).toHaveBeenCalled();
147-
expect(openSpy).toHaveBeenCalledWith('edit', resource);
147+
expect(openSpy).toHaveBeenCalledWith(resource);
148148
});
149149

150150
it('should check create view fields existence', () => {

projects/wc/src/app/components/generic-ui/list-view/list-view.component.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { ValueCellComponent } from '../value-cell/value-cell.component';
22
import { CreateResourceModalComponent } from './create-resource-modal/create-resource-modal.component';
3-
import { DialogMode } from './create-resource-modal/create-resource-modal.enums';
43
import { DeleteResourceModalComponent } from './delete-resource-confirmation-modal/delete-resource-modal.component';
54
import {
65
ChangeDetectionStrategy,
@@ -158,12 +157,12 @@ export class ListViewComponent implements OnInit {
158157
}
159158

160159
openCreateResourceModal() {
161-
this.createModal()?.open(DialogMode.Create);
160+
this.createModal()?.open();
162161
}
163162

164163
openEditResourceModal(event: MouseEvent, resource: Resource) {
165164
event.stopPropagation?.();
166-
this.createModal()?.open(DialogMode.Edit, resource);
165+
this.createModal()?.open(resource);
167166
}
168167

169168
openDeleteResourceModal(event: MouseEvent, resource: Resource) {

0 commit comments

Comments
 (0)