Skip to content

Commit 44362cb

Browse files
Fix scalar value setting and displaying (#156)
Co-authored-by: Grzegorz Krajniak <[email protected]>
1 parent be0e64b commit 44362cb

File tree

3 files changed

+48
-20
lines changed

3 files changed

+48
-20
lines changed

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

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,24 @@
88
}
99
</ui5-title>
1010
</ui5-bar>
11-
<section test-id="create-resource-dialog-form" class="form" [formGroup]="form">
11+
<section
12+
test-id="create-resource-dialog-form"
13+
class="form"
14+
[formGroup]="form"
15+
>
1216
@for (field of fields(); track field.property) {
13-
@let fieldProperty = sanitizePropertyName(field.property);
14-
<div class="inputs" [attr.test-id]="'create-field-container-' + fieldProperty">
17+
@let fieldProperty = sanitizePropertyName(field);
18+
<div
19+
class="inputs"
20+
[attr.test-id]="'create-field-container-' + fieldProperty"
21+
>
1522
<ui5-label
1623
[attr.test-id]="'create-field-label-' + fieldProperty"
1724
for="username"
1825
show-colon
1926
[required]="field.required"
20-
>{{
21-
field.label
22-
}}</ui5-label>
27+
>{{ field.label }}</ui5-label
28+
>
2329
@if (field.values?.length) {
2430
<ui5-select
2531
class="input"
@@ -34,7 +40,12 @@
3440
>
3541
@for (value of [''].concat(field.values ?? []); track value) {
3642
<ui5-option
37-
[attr.test-id]="'create-field-' + fieldProperty + '-option-' + (value || 'empty')"
43+
[attr.test-id]="
44+
'create-field-' +
45+
fieldProperty +
46+
'-option-' +
47+
(value || 'empty')
48+
"
3849
[value]="value"
3950
[selected]="value === form.controls[fieldProperty].value"
4051
>{{ value }}</ui5-option
@@ -51,7 +62,8 @@
5162
(change)="setFormControlValue($event, fieldProperty)"
5263
(blur)="onFieldBlur(fieldProperty)"
5364
[required]="!!field.required"
54-
[valueState]="getValueState(fieldProperty)" />
65+
[valueState]="getValueState(fieldProperty)"
66+
/>
5567
} @else {
5668
<ui5-input
5769
class="input"
@@ -66,7 +78,9 @@
6678
>
6779
@if (form.controls[fieldProperty].errors?.k8sNameInvalid) {
6880
<div slot="valueStateMessage">
69-
<a href="{{ k8sMessages.RFC_1035.href }}" target="_blank">{{ k8sMessages.RFC_1035.message }}</a>
81+
<a href="{{ k8sMessages.RFC_1035.href }}" target="_blank">{{
82+
k8sMessages.RFC_1035.message
83+
}}</a>
7084
</div>
7185
}
7286
</ui5-input>

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,20 +189,29 @@ describe('CreateResourceModalComponent', () => {
189189

190190
describe('sanitizePropertyName', () => {
191191
it('should replace dots with underscores in property name', () => {
192-
const property = 'metadata.name.firstName';
193-
const result = (component as any).sanitizePropertyName(property);
192+
const field = { property: 'metadata.name.firstName' };
193+
const result = (component as any).sanitizePropertyName(field);
194194
expect(result).toBe('metadata_name_firstName');
195195
});
196196

197+
it('should take propertyField key into account', () => {
198+
const field = {
199+
property: 'metadata.name.firstName',
200+
propertyField: { key: 'secret' },
201+
};
202+
const result = (component as any).sanitizePropertyName(field);
203+
expect(result).toBe('metadata_name_firstName_secret');
204+
});
205+
197206
it('should handle property names without dots', () => {
198-
const property = 'name';
199-
const result = (component as any).sanitizePropertyName(property);
207+
const field = { property: 'name' };
208+
const result = (component as any).sanitizePropertyName(field);
200209
expect(result).toBe('name');
201210
});
202211

203212
it('should throw error when property is an array', () => {
204-
const property = ['name', 'firstName'];
205-
expect(() => (component as any).sanitizePropertyName(property)).toThrow(
213+
const field = { property: ['name', 'firstName'] };
214+
expect(() => (component as any).sanitizePropertyName(field)).toThrow(
206215
'Wrong property type, array not supported',
207216
);
208217
});

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
} from '@angular/forms';
2424
import { FieldDefinition, Resource } from '@platform-mesh/portal-ui-lib/models';
2525
import { ResourceNodeContext } from '@platform-mesh/portal-ui-lib/services';
26-
import { getValueByPath } from '@platform-mesh/portal-ui-lib/utils';
26+
import { getResourceValueByJsonPath } from '@platform-mesh/portal-ui-lib/utils';
2727
import {
2828
BarComponent,
2929
DialogComponent,
@@ -125,11 +125,16 @@ export class CreateResourceModalComponent implements OnInit {
125125
this.form.controls[formControlName].markAsTouched();
126126
}
127127

128-
sanitizePropertyName(property: string | string[]) {
128+
sanitizePropertyName(field: FieldDefinition) {
129+
const property: string | string[] = field.property;
129130
if (property instanceof Array) {
130131
throw new Error('Wrong property type, array not supported');
131132
}
132-
return (property as string).replaceAll('.', '_');
133+
134+
return (
135+
(property as string).replaceAll('.', '_') +
136+
(field.propertyField ? `_${field.propertyField?.key}` : '')
137+
);
133138
}
134139

135140
isEditMode() {
@@ -148,10 +153,10 @@ export class CreateResourceModalComponent implements OnInit {
148153
return this.fields().reduce(
149154
(obj, fieldDefinition) => {
150155
const validators = this.getValidator(fieldDefinition);
151-
const fieldName = this.sanitizePropertyName(fieldDefinition.property);
156+
const fieldName = this.sanitizePropertyName(fieldDefinition);
152157
const fieldValue =
153158
resource && typeof fieldDefinition.property === 'string'
154-
? getValueByPath(resource, fieldDefinition.property)
159+
? getResourceValueByJsonPath(resource, fieldDefinition)
155160
: '';
156161
obj[fieldName] = new FormControl(fieldValue, validators);
157162

0 commit comments

Comments
 (0)