Skip to content

Commit e865380

Browse files
author
Gérard Collin
committed
fix: upgrade and selection through model working
1 parent 21da8e6 commit e865380

12 files changed

Lines changed: 45 additions & 83 deletions

File tree

apps/xt-host/projects/host/src/app/entity-manager/entity-manager.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ <h1 class="entity-manager__title">{{ entName }}</h1>
7979
<xt-render
8080
displayMode="LIST_VIEW"
8181
[value]="store?.entities()"
82-
(models)="listModelChanged($event)" [models]="listModel"
82+
[models]="listModel"
8383
[valueType]="entityName()">
8484
</xt-render>
8585
</div>

apps/xt-host/projects/host/src/app/entity-manager/entity-manager.component.spec.ts

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,8 @@ describe('EntityManagerComponent', () => {
8282
btnNew.children[0].nativeElement.click();
8383
harness.fixture.detectChanges();
8484
// For some reasons, we have to wait multiple times for stability
85-
await harness.fixture.whenStable();
86-
await harness.fixture.whenStable();
87-
await harness.fixture.whenStable();
88-
await harness.fixture.whenStable();
85+
for (let i=0;i<10;i++)
86+
await harness.fixture.whenStable();
8987

9088
//expect(newSpy).toHaveBeenCalledOnce();
9189
const form=component.editForm().get('editor') as FormGroup;
@@ -119,10 +117,8 @@ describe('EntityManagerComponent', () => {
119117
const btnSave = harness.fixture.debugElement.query(By.css("#btn-save"));
120118
btnSave.children[0].nativeElement.click();
121119
harness.fixture.detectChanges();
122-
await harness.fixture.whenStable();
123-
await harness.fixture.whenStable();
124-
await harness.fixture.whenStable();
125-
await harness.fixture.whenStable();
120+
for (let i=0;i<10;i++)
121+
await harness.fixture.whenStable();
126122

127123
//expect(saveSpy).toHaveBeenCalledOnce();
128124

@@ -150,10 +146,8 @@ describe('EntityManagerComponent', () => {
150146
row.children[0].nativeElement.click();
151147
row.children[0].nativeElement.click();
152148
harness.fixture.detectChanges();
153-
await harness.fixture.whenStable();
154-
await harness.fixture.whenStable();
155-
await harness.fixture.whenStable();
156-
await harness.fixture.whenStable();
149+
for (let i=0;i<10;i++)
150+
await harness.fixture.whenStable();
157151

158152
//It should have switched to edit mode
159153
expect (component.viewMode()).toEqual("edit");
@@ -164,11 +158,8 @@ describe('EntityManagerComponent', () => {
164158

165159
btnSave.children[0].nativeElement.click();
166160
harness.fixture.detectChanges();
167-
await harness.fixture.whenStable();
168-
await harness.fixture.whenStable();
169-
await harness.fixture.whenStable();
170-
await harness.fixture.whenStable();
171-
await harness.fixture.whenStable();
161+
for (let i=0;i<10;i++)
162+
await harness.fixture.whenStable();
172163

173164

174165
expect (component.selectedEntity()).toEqual({
@@ -190,9 +181,8 @@ describe('EntityManagerComponent', () => {
190181
const btnDelete = harness.fixture.debugElement.query(By.css("#btn-delete"));
191182
btnDelete.children[0].nativeElement.click();
192183
harness.fixture.detectChanges();
193-
await harness.fixture.whenStable();
194-
await harness.fixture.whenStable();
195-
await harness.fixture.whenStable();
184+
for (let i=0;i<10;i++)
185+
await harness.fixture.whenStable();
196186

197187
//expect(deleteSpy).toHaveBeenCalledOnce();
198188
list = harness.fixture.debugElement.query(By.directive(Table));

apps/xt-host/projects/host/src/app/entity-manager/entity-manager.component.ts

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import {
22
ChangeDetectionStrategy,
33
Component,
4-
computed,
4+
computed, effect,
55
inject,
66
input,
77
linkedSignal,
8+
model, ModelSignal,
89
OnDestroy,
910
signal
1011
} from '@angular/core';
1112
import { ActivatedRoute } from '@angular/router';
1213
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
13-
import { updateFormGroupWithValue,
14-
XtBaseModel, XtComponentModel, XtComponentOutput, XtRenderComponent, XtResolverService } from 'xt-components';
14+
import { updateFormGroupWithValue, XtBaseModel, XtRenderComponent, XtResolverService } from 'xt-components';
1515
import { FormBuilder, FormGroup, PristineChangeEvent, ReactiveFormsModule } from '@angular/forms';
1616
import { ManagedData } from 'xt-type';
1717
import { Tab, TabList, TabPanel, TabPanels, Tabs } from 'primeng/tabs';
@@ -20,7 +20,7 @@ import { Button } from 'primeng/button';
2020
import { Subscription } from 'rxjs';
2121
import { ErrorHandlerService } from '../error-handler/error-handler.service';
2222
import { ProgressSpinner } from 'primeng/progressspinner';
23-
import { XtStoreManagerService, XtSignalStore } from 'xt-store';
23+
import { XtSignalStore, XtStoreManagerService } from 'xt-store';
2424

2525
@Component({
2626
selector: 'app-entity-manager',
@@ -50,8 +50,6 @@ export class EntityManagerComponent implements OnDestroy {
5050

5151
editForm = signal<FormGroup>(this.formBuilder.group ({ editor: this.formBuilder.group({}) }));
5252

53-
selectedEntity = signal<ManagedData|null>(null);
54-
5553
canEdit= computed(()=> {
5654
if (this.selectedEntity()!=null)
5755
return true;
@@ -72,11 +70,19 @@ export class EntityManagerComponent implements OnDestroy {
7270
deleting = signal (false);
7371
updating = signal (false);
7472

75-
listModel =new XtBaseModel<any>();
73+
selectedEntity= model<any>();
74+
75+
listModel = new XtBaseModel<any>();
76+
77+
selectedEntityChanged= effect( ()=> {
78+
const selected=this.selectedEntity();
79+
this.updateEditForm();
80+
});
7681

7782
private subscriptions=new Subscription();
7883

7984
constructor() {
85+
this.listModel.valueSelected=this.selectedEntity;
8086
this.updateStore();
8187
this.route.paramMap.pipe(
8288
takeUntilDestroyed(),
@@ -106,16 +112,6 @@ export class EntityManagerComponent implements OnDestroy {
106112
}
107113
}
108114

109-
listModelChanged($event : any) {
110-
const newValue=$event as XtComponentModel;
111-
if (newValue?.valueSelected!=null) {
112-
newValue?.valueSelected.subscribe (selected => {
113-
this.selectedEntity.set(selected);
114-
this.updateEditForm();
115-
});
116-
}
117-
}
118-
119115
updateEditForm () {
120116
const entity = this.selectedEntity();
121117
const form = this.formBuilder.group({}, {updateOn: 'change'});
@@ -136,7 +132,7 @@ export class EntityManagerComponent implements OnDestroy {
136132
this.saving.set(true);
137133
const savedValue = await this.safeStore().storeEntity (toSave);
138134
this.selectedEntity.set(savedValue);
139-
this.updateEditForm();
135+
//this.updateEditForm();
140136
this.canSave.set(false);
141137
this.viewMode.set("list");
142138
} catch (error) {
@@ -182,7 +178,7 @@ export class EntityManagerComponent implements OnDestroy {
182178
if (deleted) {
183179
this.selectedEntity.set(null);
184180
this.viewMode.set("list");
185-
this.updateEditForm();
181+
//this.updateEditForm();
186182
}
187183
}
188184

@@ -191,7 +187,7 @@ export class EntityManagerComponent implements OnDestroy {
191187
this.newing.set(true);
192188
const newOne = await this.safeStore().storeEntity({} as ManagedData);
193189
this.selectedEntity.set(newOne);
194-
this.updateEditForm();
190+
//this.updateEditForm();
195191
this.viewMode.set("edit");
196192

197193
} catch (error) {

libs/xt-components/projects/xt-components/src/lib/output/xt-base-model.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { XtComponentModel } from '../xt-component';
33
import { ISortBy, IStoreCriteria } from '../store/store-support';
44

55
export class XtBaseModel<T> implements XtComponentModel {
6-
valueSelected: ModelSignal<T|null> | undefined;
7-
sortBy: ModelSignal<ISortBy<T>[]> | undefined;
8-
filterBy: ModelSignal<IStoreCriteria<T>[]> | undefined;
6+
valueSelected?: ModelSignal<T|null> | undefined;
7+
sortBy?: ModelSignal<ISortBy<T>[]> | undefined;
8+
filterBy?: ModelSignal<IStoreCriteria<T>[]> | undefined;
99

1010
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
{{componentType()}}
2-
<ng-container *ngComponentOutlet="type(); inputs: {context:realContext ()}" />
2+
<ng-container *ngComponentOutlet="type(); inputs: {context:realContext (), models:models()}" />

libs/xt-components/projects/xt-components/src/lib/render/xt-render-sub.component.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export class XtRenderSubComponent<T> implements AfterViewInit {
4949

5050
inputs = input<XtBaseInput>();
5151
outputs = output<XtComponentOutput>();
52-
models = model<XtComponentModel> (new XtBaseModel());
52+
models = input<XtComponentModel> ();
5353

5454
outlet = viewChild.required(NgComponentOutlet);
5555

@@ -97,19 +97,6 @@ export class XtRenderSubComponent<T> implements AfterViewInit {
9797
}
9898
}
9999

100-
const instanceModels=instance?.models?instance?.models():null;
101-
if ((instanceModels!=null) &&
102-
(Object.keys(instanceModels).length>0) ) {
103-
const models=this.models();
104-
105-
// Sets the model values defined as inputs
106-
for (const key of Object.keys(instanceModels) as XtModelType[] ) {
107-
instanceModels[key] = models[key];
108-
}
109-
// Bind the component model to this model
110-
this.models.set(instanceModels);
111-
}
112-
113100
const inputs = this.inputs();
114101
if ((inputs!=null) && (instance?.inputsObject!=null)) {
115102
for (const key of Object.keys(inputs) as XtInputType[] ) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<ng-container *ngComponentOutlet="type(); inputs: {context:context ()}" />
1+
<ng-container *ngComponentOutlet="type(); inputs: {context:context (), models:models()}" />

libs/xt-components/projects/xt-components/src/lib/render/xt-render.component.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
inject,
66
input,
77
model,
8-
output,
8+
output, signal,
99
Signal,
1010
Type,
1111
viewChild
@@ -60,7 +60,7 @@ export class XtRenderComponent<T> implements AfterViewInit {
6060

6161
inputs = input<XtBaseInput>();
6262
outputs = output<XtComponentOutput>();
63-
models = model<XtComponentModel> (new XtBaseModel());
63+
models = input<XtComponentModel> ();
6464

6565
outlet = viewChild.required(NgComponentOutlet);
6666

@@ -132,19 +132,6 @@ export class XtRenderComponent<T> implements AfterViewInit {
132132
}
133133
}
134134

135-
const instanceModels=instance?.models?instance?.models():null;
136-
if ((instanceModels!=null) &&
137-
(Object.keys(instanceModels).length>0) ) {
138-
const models=this.models();
139-
140-
// Sets the model values defined as inputs
141-
for (const key of Object.keys(instanceModels) as XtModelType[] ) {
142-
instanceModels[key] = models[key];
143-
}
144-
// Bind the component model to this model
145-
this.models.set(instanceModels);
146-
}
147-
148135
const inputs = this.inputs();
149136
if ((inputs!=null) && (instance?.inputsObject!=null)) {
150137
for (const key of Object.keys(inputs) as XtInputType[] ) {

libs/xt-components/projects/xt-components/src/lib/xt-component.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ export type XtComponent<T=any> = {
77

88
inputsObject?: XtComponentInput;
99
outputsObject?: XtComponentOutput;
10+
modelsObject?: XtComponentModel;
1011

11-
models?: ModelSignal<XtComponentModel|undefined>;
12+
models?: InputSignal<XtComponentModel|undefined>;
1213
inputs?: InputSignal<XtComponentInput>;
1314
outputs?: OutputEmitterRef<XtComponentOutput>;
1415

@@ -26,7 +27,7 @@ export type XtInputType = 'valueSelected';
2627
export type XtModelType = 'valueSelected' | 'sortBy' | 'filterBy';
2728

2829
export type XtComponentModel = {
29-
[key in XtModelType]: ModelSignal<any>|undefined;
30+
[key in XtModelType]?: ModelSignal<any>|undefined;
3031
}
3132

3233
export type XtComponentOutput = {

libs/xt-components/projects/xt-components/src/lib/xt-simple/xt-simple.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class XtSimpleComponent<T = any> implements XtComponent<T>, OnInit{
2121
inputsObject = new XtBaseInput();
2222

2323
outputs=output<XtComponentOutput>();
24-
models=model<XtComponentModel>();
24+
models=input<XtComponentModel>();
2525

2626
isInForm = computed<boolean> ( () => {
2727
return this.context()?.isInForm()??false;

0 commit comments

Comments
 (0)