Skip to content

Commit 85771eb

Browse files
Merge pull request #1657 from rocket-admin/signals-migration
Signals migration
2 parents 9e7bd77 + f8aa46e commit 85771eb

231 files changed

Lines changed: 5090 additions & 2213 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

frontend/src/app/components/ui-components/record-edit-fields/base-row-field/base-row-field.component.spec.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { ComponentFixture, TestBed } from '@angular/core/testing';
2-
32
import { BaseEditFieldComponent } from './base-row-field.component';
43

54
describe('BaseEditFieldComponent', () => {
@@ -13,10 +12,26 @@ describe('BaseEditFieldComponent', () => {
1312

1413
fixture = TestBed.createComponent(BaseEditFieldComponent);
1514
component = fixture.componentInstance;
16-
fixture.detectChanges();
1715
});
1816

1917
it('should create', () => {
2018
expect(component).toBeTruthy();
2119
});
20+
21+
it('should normalize label on init', () => {
22+
fixture.componentRef.setInput('label', 'user_first_name');
23+
component.ngOnInit();
24+
expect(component.normalizedLabel()).toBeTruthy();
25+
});
26+
27+
it('should set normalizedLabel from label input', () => {
28+
fixture.componentRef.setInput('label', 'test_field');
29+
component.ngOnInit();
30+
expect(component.normalizedLabel()).toBeDefined();
31+
expect(typeof component.normalizedLabel()).toBe('string');
32+
});
33+
34+
it('should have onFieldChange event emitter', () => {
35+
expect(component.onFieldChange).toBeDefined();
36+
});
2237
});
Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { CommonModule } from '@angular/common';
2-
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
2+
import { Component, computed, input, OnInit, output } from '@angular/core';
33
import { TableField, TableForeignKey, WidgetStructure } from 'src/app/models/table';
44
import { normalizeFieldName } from '../../../../lib/normalize';
55

@@ -10,20 +10,18 @@ import { normalizeFieldName } from '../../../../lib/normalize';
1010
imports: [CommonModule],
1111
})
1212
export class BaseEditFieldComponent implements OnInit {
13-
@Input() key: string;
14-
@Input() label: string;
15-
@Input() required: boolean;
16-
@Input() readonly: boolean;
17-
@Input() structure: TableField;
18-
@Input() disabled: boolean;
19-
@Input() widgetStructure: WidgetStructure;
20-
@Input() relations: TableForeignKey;
13+
readonly key = input<string>();
14+
readonly label = input<string>();
15+
readonly required = input<boolean>(false);
16+
readonly readonly = input<boolean>(false);
17+
readonly structure = input<TableField>();
18+
readonly disabled = input<boolean>(false);
19+
readonly widgetStructure = input<WidgetStructure>();
20+
readonly relations = input<TableForeignKey>();
2121

22-
@Output() onFieldChange = new EventEmitter<any>();
22+
readonly onFieldChange = output<any>();
2323

24-
public normalizedLabel: string;
24+
readonly normalizedLabel = computed(() => normalizeFieldName(this.label() || ''));
2525

26-
ngOnInit(): void {
27-
this.normalizedLabel = normalizeFieldName(this.label);
28-
}
26+
ngOnInit(): void {}
2927
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<div class="static-text">
2-
<span>{{normalizedLabel}}</span>
2+
<span>{{normalizedLabel()}}</span>
33
<span class="static-text__value"
4-
attr.data-testid="record-{{label}}-binary-data-caption">
4+
attr.data-testid="record-{{label()}}-binary-data-caption">
55
binary data
66
</span>
77
</div>
Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
<div *ngIf="isRadiogroup; else checkboxElement" class="radio-line">
2-
<label id="">{{normalizedLabel}}</label>
3-
<mat-button-toggle-group name="{{label}}-{{key}}"
4-
attr.data-testid="record-{{label}}-boolean-radio-group"
5-
[hideSingleSelectionIndicator]="true"
6-
[disabled]="disabled"
7-
[value]="value">
8-
<mat-button-toggle [value]=true (click)="onToggleChange(true)">Yes</mat-button-toggle>
9-
<mat-button-toggle [value]=false (click)="onToggleChange(false)">No</mat-button-toggle>
10-
</mat-button-toggle-group>
11-
</div>
12-
13-
<ng-template #checkboxElement>
1+
@if (isRadiogroup) {
142
<div class="radio-line">
15-
<label id="">{{normalizedLabel}}</label>
16-
<mat-button-toggle-group name="{{label}}-{{key}}"
17-
attr.data-testid="record-{{label}}-boolean-radio-group"
3+
<label id="">{{normalizedLabel()}}</label>
4+
<mat-button-toggle-group name="{{label()}}-{{key()}}"
5+
attr.data-testid="record-{{label()}}-boolean-radio-group"
186
[hideSingleSelectionIndicator]="true"
19-
[disabled]="disabled"
7+
[disabled]="disabled()"
8+
[value]="value()">
9+
<mat-button-toggle [value]=true (click)="onToggleChange(true)">Yes</mat-button-toggle>
10+
<mat-button-toggle [value]=false (click)="onToggleChange(false)">No</mat-button-toggle>
11+
</mat-button-toggle-group>
12+
</div>
13+
} @else {
14+
<div class="radio-line">
15+
<label id="">{{normalizedLabel()}}</label>
16+
<mat-button-toggle-group name="{{label()}}-{{key()}}"
17+
attr.data-testid="record-{{label()}}-boolean-radio-group"
18+
[hideSingleSelectionIndicator]="true"
19+
[disabled]="disabled()"
2020
[(ngModel)]="value"
2121
(ngModelChange)="onFieldChange.emit($event)">
2222
<mat-button-toggle [value]=true>Yes</mat-button-toggle>
2323
<mat-button-toggle [value]=false>No</mat-button-toggle>
2424
</mat-button-toggle-group>
2525
</div>
26-
</ng-template>
26+
}

frontend/src/app/components/ui-components/record-edit-fields/boolean/boolean.component.spec.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,40 +37,40 @@ describe('BooleanEditComponent', () => {
3737
});
3838

3939
it('should set value in true when input value contain anything', () => {
40-
component.value = 'anything';
41-
component.structure = fakeStructure;
40+
fixture.componentRef.setInput('value', 'anything');
41+
fixture.componentRef.setInput('structure', fakeStructure);
4242
component.ngOnInit();
4343

44-
expect(component.value).toBeTruthy();
44+
expect(component.value()).toBeTruthy();
4545
});
4646

4747
it('should set value in felse when input value is 0', () => {
48-
component.value = 0;
49-
component.structure = fakeStructure;
48+
fixture.componentRef.setInput('value', 0);
49+
fixture.componentRef.setInput('structure', fakeStructure);
5050
component.ngOnInit();
5151

52-
expect(component.value).toBeFalsy();
52+
expect(component.value()).toBeFalsy();
5353
});
5454

5555
it('should set value in null when input value is undefined', () => {
56-
component.value = undefined;
57-
component.structure = fakeStructure;
56+
fixture.componentRef.setInput('value', undefined);
57+
fixture.componentRef.setInput('structure', fakeStructure);
5858
component.ngOnInit();
5959

60-
expect(component.value).toEqual(null);
60+
expect(component.value()).toEqual(null);
6161
});
6262

6363
it('should set isRadiogroup in false if allow_null is false', () => {
64-
component.value = undefined;
65-
component.structure = fakeStructure;
64+
fixture.componentRef.setInput('value', undefined);
65+
fixture.componentRef.setInput('structure', fakeStructure);
6666
component.ngOnInit();
6767

6868
expect(component.isRadiogroup).toEqual(false);
6969
});
7070

7171
it('should set isRadiogroup in true if allow_null is true', () => {
72-
component.value = undefined;
73-
component.structure = {
72+
fixture.componentRef.setInput('value', undefined);
73+
fixture.componentRef.setInput('structure', {
7474
column_name: 'banned',
7575
column_default: '0',
7676
data_type: 'tinyint',
@@ -79,7 +79,7 @@ describe('BooleanEditComponent', () => {
7979
auto_increment: false,
8080
allow_null: true,
8181
character_maximum_length: 1,
82-
};
82+
});
8383
component.ngOnInit();
8484

8585
expect(component.isRadiogroup).toEqual(true);
Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { CommonModule } from '@angular/common';
2-
import { Component, Input } from '@angular/core';
2+
import { Component, inject, model } from '@angular/core';
33
import { FormsModule } from '@angular/forms';
44
import { MatButtonToggleModule } from '@angular/material/button-toggle';
55
import { DBtype } from 'src/app/models/connection';
@@ -13,48 +13,43 @@ import { BaseEditFieldComponent } from '../base-row-field/base-row-field.compone
1313
imports: [CommonModule, FormsModule, MatButtonToggleModule],
1414
})
1515
export class BooleanEditComponent extends BaseEditFieldComponent {
16-
@Input() value: boolean | number | string | null;
16+
readonly value = model<boolean | number | string | null>();
1717

1818
public isRadiogroup: boolean;
1919
connectionType: DBtype;
2020

21-
constructor(private _connections: ConnectionsService) {
22-
super();
23-
}
21+
private _connections = inject(ConnectionsService);
2422

2523
ngOnInit(): void {
2624
super.ngOnInit();
2725
this.connectionType = this._connections.currentConnection.type;
2826

29-
if (this.value) {
30-
this.value = true;
31-
} else if (this.value === 0 || this.value === '' || this.value === false) {
32-
this.value = false;
27+
const val = this.value();
28+
if (val) {
29+
this.value.set(true);
30+
} else if (val === 0 || val === '' || val === false) {
31+
this.value.set(false);
3332
} else {
34-
this.value = null;
33+
this.value.set(null);
3534
}
3635

37-
this.onFieldChange.emit(this.value);
36+
this.onFieldChange.emit(this.value());
3837

39-
// Parse widget parameters if available
4038
let parsedParams = null;
41-
if (this.widgetStructure?.widget_params) {
42-
parsedParams =
43-
typeof this.widgetStructure.widget_params === 'string'
44-
? JSON.parse(this.widgetStructure.widget_params)
45-
: this.widgetStructure.widget_params;
39+
const ws = this.widgetStructure();
40+
if (ws?.widget_params) {
41+
parsedParams = typeof ws.widget_params === 'string' ? JSON.parse(ws.widget_params) : ws.widget_params;
4642
}
4743

48-
// Check allow_null from either structure or widget params
49-
this.isRadiogroup = this.structure?.allow_null || !!parsedParams?.allow_null;
44+
this.isRadiogroup = this.structure()?.allow_null || !!parsedParams?.allow_null;
5045
}
5146

5247
onToggleChange(optionValue: boolean): void {
53-
if (this.value === optionValue) {
54-
this.value = null;
48+
if (this.value() === optionValue) {
49+
this.value.set(null);
5550
} else {
56-
this.value = optionValue;
51+
this.value.set(optionValue);
5752
}
58-
this.onFieldChange.emit(this.value);
53+
this.onFieldChange.emit(this.value());
5954
}
6055
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
<span class="mat-body-1">{{ normalizedLabel }} {{ required ? '*' : '' }}</span>
1+
<span class="mat-body-1">{{ normalizedLabel() }} {{ required() ? '*' : '' }}</span>
22

33
<div class="code-editor-box ph-no-capture">
44
<ngs-code-editor
55
[theme]="codeEditorTheme"
66
[codeModel]="mutableCodeModel"
77
[options]="codeEditorOptions"
8-
[readOnly]="readonly"
8+
[readOnly]="readonly()"
99
(valueChanged)="onFieldChange.emit($event)">
1010
</ngs-code-editor>
1111
</div>

frontend/src/app/components/ui-components/record-edit-fields/code/code.component.spec.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ describe('CodeEditComponent', () => {
2929
fixture = TestBed.createComponent(CodeEditComponent);
3030
component = fixture.componentInstance;
3131

32-
component.widgetStructure = {
32+
fixture.componentRef.setInput('widgetStructure', {
3333
widget_params: {
3434
language: 'css',
3535
},
36-
} as any;
37-
component.label = 'styles';
38-
component.value = '.container { display: flex; }';
36+
} as any);
37+
fixture.componentRef.setInput('label', 'styles');
38+
fixture.componentRef.setInput('value', '.container { display: flex; }');
3939

4040
fixture.detectChanges();
4141
});
@@ -69,21 +69,21 @@ describe('CodeEditComponent', () => {
6969
});
7070

7171
it('should support different languages', () => {
72-
component.widgetStructure = {
72+
fixture.componentRef.setInput('widgetStructure', {
7373
widget_params: {
7474
language: 'javascript',
7575
},
76-
} as any;
77-
component.label = 'script';
78-
component.value = 'console.log("hello");';
76+
} as any);
77+
fixture.componentRef.setInput('label', 'script');
78+
fixture.componentRef.setInput('value', 'console.log("hello");');
7979
component.ngOnInit();
8080

8181
expect((component.mutableCodeModel as any).language).toBe('javascript');
8282
});
8383

8484
it('should normalize label from base class', () => {
85-
component.label = 'custom_styles';
85+
fixture.componentRef.setInput('label', 'custom_styles');
8686
component.ngOnInit();
87-
expect(component.normalizedLabel).toBeDefined();
87+
expect(component.normalizedLabel()).toBeDefined();
8888
});
8989
});

frontend/src/app/components/ui-components/record-edit-fields/code/code.component.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { CommonModule } from '@angular/common';
2-
import { Component, Input } from '@angular/core';
2+
import { Component, inject, model } from '@angular/core';
33
import { CodeEditorModule } from '@ngstack/code-editor';
44
import { UiSettingsService } from 'src/app/services/ui-settings.service';
55
import { BaseEditFieldComponent } from '../base-row-field/base-row-field.component';
@@ -11,7 +11,9 @@ import { BaseEditFieldComponent } from '../base-row-field/base-row-field.compone
1111
imports: [CommonModule, CodeEditorModule],
1212
})
1313
export class CodeEditComponent extends BaseEditFieldComponent {
14-
@Input() value;
14+
readonly value = model<any>();
15+
16+
private _uiSettings = inject(UiSettingsService);
1517

1618
public mutableCodeModel: Object;
1719
public codeEditorOptions = {
@@ -22,16 +24,12 @@ export class CodeEditComponent extends BaseEditFieldComponent {
2224
};
2325
public codeEditorTheme = 'vs-dark';
2426

25-
constructor(private _uiSettings: UiSettingsService) {
26-
super();
27-
}
28-
2927
ngOnInit(): void {
3028
super.ngOnInit();
3129
this.mutableCodeModel = {
32-
language: `${this.widgetStructure.widget_params.language}`,
33-
uri: `${this.label}.json`,
34-
value: this.value,
30+
language: `${this.widgetStructure().widget_params.language}`,
31+
uri: `${this.label()}.json`,
32+
value: this.value(),
3533
};
3634

3735
this.codeEditorTheme = this._uiSettings.isDarkMode ? 'vs-dark' : 'vs';

frontend/src/app/components/ui-components/record-edit-fields/color/color.component.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
<div class="color-edit-container">
22
<mat-form-field class="color-form-field" appearance="outline">
3-
<mat-label>{{normalizedLabel}}</mat-label>
4-
<input matInput type="text" name="{{label}}-{{key}}"
5-
[required]="required" [disabled]="disabled" [readonly]="readonly"
6-
attr.data-testid="record-{{label}}-color"
3+
<mat-label>{{normalizedLabel()}}</mat-label>
4+
<input matInput type="text" name="{{label()}}-{{key()}}"
5+
[required]="required()" [disabled]="disabled()" [readonly]="readonly()"
6+
attr.data-testid="record-{{label()}}-color"
77
[(ngModel)]="value" (ngModelChange)="onTextInputChange()"
88
placeholder="e.g. #000000, rgb(0,0,0), hsl(0,0%,0%)">
99
<div matSuffix class="color-picker-container">
10-
<input type="color"
10+
<input type="color"
1111
class="color-picker-input"
1212
[value]="normalizedColorForPicker"
13-
[disabled]="disabled || readonly"
13+
[disabled]="disabled() || readonly()"
1414
(change)="onColorPickerChange($event)"
1515
title="Choose color">
1616
</div>

0 commit comments

Comments
 (0)