Skip to content

Commit 5d3d3e2

Browse files
makdenissgkrajniak
andauthored
Feat/translate status fields (#410)
Co-authored-by: makdeniss <[email protected]> Co-authored-by: Grzegorz Krajniak <[email protected]>
1 parent 5cd07fe commit 5d3d3e2

File tree

8 files changed

+150
-4
lines changed

8 files changed

+150
-4
lines changed

projects/wc/src/app/components/generic-ui/detail-view/detail-view.component.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@
4343
@for (field of resourceFields; track field.property) {
4444
<div class="resource-info-cell">
4545
<ui5-label>{{ field.label }}</ui5-label>
46-
<p>{{ getResourceValueByJsonPath(resource(), field) }}</p>
46+
<p>
47+
<value-cell [value]="getResourceValueByJsonPath(resource(), field)"></value-cell>
48+
</p>
4749
</div>
4850
}
4951
</div>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
ToolbarButtonComponent,
3131
ToolbarComponent,
3232
} from '@ui5/webcomponents-ngx';
33+
import { ValueCellComponent } from '../value-cell/value-cell.component';
3334

3435
const defaultFields: FieldDefinition[] = [
3536
{
@@ -52,6 +53,7 @@ const defaultFields: FieldDefinition[] = [
5253
ToolbarButtonComponent,
5354
DynamicPageHeaderComponent,
5455
LabelComponent,
56+
ValueCellComponent,
5557
],
5658
templateUrl: './detail-view.component.html',
5759
styleUrl: './detail-view.component.scss',

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@
5151
@for (item of resources(); track item.metadata.name) {
5252
<ui5-table-row interactive (click)="navigateToResource(item)">
5353
@for (column of columns; track column.label) {
54-
<ui5-table-cell>{{
55-
getResourceValueByJsonPath(item, column)
56-
}}</ui5-table-cell>
54+
<ui5-table-cell>
55+
<value-cell [value]="getResourceValueByJsonPath(item, column)"></value-cell>
56+
</ui5-table-cell>
5757
}
5858
<ui5-table-cell
5959
class="actions-column delete-item"

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
ToolbarButtonComponent,
4040
ToolbarComponent,
4141
} from '@ui5/webcomponents-ngx';
42+
import { ValueCellComponent } from '../value-cell/value-cell.component';
4243

4344
const defaultColumns: FieldDefinition[] = [
4445
{
@@ -74,6 +75,7 @@ const defaultColumns: FieldDefinition[] = [
7475
TitleComponent,
7576
ToolbarButtonComponent,
7677
ToolbarComponent,
78+
ValueCellComponent,
7779
],
7880
})
7981
export class ListViewComponent implements OnInit {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@if (isBoolLike()) {
2+
<ui5-icon [name]="iconName()" [design]="iconDesign()"></ui5-icon>
3+
} @else {
4+
{{ value() }}
5+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { ValueCellComponent } from './value-cell.component';
2+
import {
3+
ICON_DESIGN_NEGATIVE,
4+
ICON_DESIGN_POSITIVE,
5+
ICON_NAME_NEGATIVE,
6+
ICON_NAME_POSITIVE,
7+
} from './value-cell.constants';
8+
import { ComponentFixture, TestBed } from '@angular/core/testing';
9+
10+
jest.mock('@ui5/webcomponents-ngx', () => ({ IconComponent: class {} }), {
11+
virtual: true,
12+
});
13+
14+
describe('ValueCellComponent', () => {
15+
let component: ValueCellComponent;
16+
let fixture: ComponentFixture<ValueCellComponent>;
17+
18+
const makeComponent = (value: unknown) => {
19+
fixture = TestBed.createComponent(ValueCellComponent);
20+
component = fixture.componentInstance;
21+
22+
fixture.componentRef.setInput('value', value as any);
23+
24+
fixture.detectChanges();
25+
26+
return { component, fixture };
27+
};
28+
29+
beforeEach(() => {
30+
TestBed.configureTestingModule({
31+
imports: [ValueCellComponent],
32+
}).overrideComponent(ValueCellComponent, {
33+
set: { template: '<div></div>', imports: [] },
34+
});
35+
});
36+
37+
it('should create', () => {
38+
const { component } = makeComponent('r1');
39+
expect(component).toBeTruthy();
40+
});
41+
42+
it('should accept non-boolean value and mark as not boolean-like', () => {
43+
const { component } = makeComponent('cluster-a');
44+
45+
expect(component.isBoolLike()).toBe(false);
46+
expect(component.value()).toBe('cluster-a');
47+
expect(component.iconDesign()).toBeUndefined();
48+
expect(component.iconName()).toBeUndefined();
49+
});
50+
51+
it("should accept boolean-like 'true' string and set positive icon and design", () => {
52+
const { component } = makeComponent('true');
53+
54+
expect(component.isBoolLike()).toBe(true);
55+
expect(component.iconDesign()).toBe(ICON_DESIGN_POSITIVE);
56+
expect(component.iconName()).toBe(ICON_NAME_POSITIVE);
57+
});
58+
59+
it("should accept boolean-like 'false' string and set negative icon and design", () => {
60+
const { component } = makeComponent('false');
61+
62+
expect(component.isBoolLike()).toBe(true);
63+
expect(component.iconDesign()).toBe(ICON_DESIGN_NEGATIVE);
64+
expect(component.iconName()).toBe(ICON_NAME_NEGATIVE);
65+
});
66+
67+
it('should accept boolean value true and set positive icon', () => {
68+
const { component } = makeComponent(true);
69+
70+
expect(component.isBoolLike()).toBe(true);
71+
expect(component.iconDesign()).toBe(ICON_DESIGN_POSITIVE);
72+
expect(component.iconName()).toBe(ICON_NAME_POSITIVE);
73+
});
74+
});
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import {
2+
ICON_DESIGN_NEGATIVE,
3+
ICON_DESIGN_POSITIVE,
4+
ICON_NAME_NEGATIVE,
5+
ICON_NAME_POSITIVE,
6+
} from './value-cell.constants';
7+
import {
8+
ChangeDetectionStrategy,
9+
Component,
10+
computed,
11+
input,
12+
} from '@angular/core';
13+
import { IconComponent } from '@ui5/webcomponents-ngx';
14+
15+
export type IconDesignType =
16+
| typeof ICON_DESIGN_POSITIVE
17+
| typeof ICON_DESIGN_NEGATIVE;
18+
19+
@Component({
20+
selector: 'value-cell',
21+
standalone: true,
22+
imports: [IconComponent],
23+
templateUrl: './value-cell.component.html',
24+
changeDetection: ChangeDetectionStrategy.OnPush,
25+
})
26+
export class ValueCellComponent {
27+
value = input<unknown>();
28+
isBoolLike = computed(() => this.boolValue() !== undefined);
29+
iconDesign = computed<IconDesignType | undefined>(() => {
30+
return this.boolValue() === undefined
31+
? undefined
32+
: this.boolValue()
33+
? ICON_DESIGN_POSITIVE
34+
: ICON_DESIGN_NEGATIVE;
35+
});
36+
iconName = computed<string | undefined>(() => {
37+
return this.boolValue() === undefined
38+
? undefined
39+
: this.boolValue()
40+
? ICON_NAME_POSITIVE
41+
: ICON_NAME_NEGATIVE;
42+
});
43+
44+
private boolValue = computed(() => this.normalizeBoolean(this.value()));
45+
46+
private normalizeBoolean(value: unknown): boolean | undefined {
47+
const normalizedValue = value?.toString()?.toLowerCase();
48+
if (normalizedValue === 'true') {
49+
return true;
50+
}
51+
if (normalizedValue === 'false') {
52+
return false;
53+
}
54+
return undefined;
55+
}
56+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const ICON_DESIGN_POSITIVE = 'Positive';
2+
export const ICON_DESIGN_NEGATIVE = 'Negative';
3+
4+
export const ICON_NAME_POSITIVE = 'accept';
5+
export const ICON_NAME_NEGATIVE = 'decline';

0 commit comments

Comments
 (0)