Skip to content

Commit

Permalink
fix: entity, which doesn't pass filter is only removed from table aft…
Browse files Browse the repository at this point in the history
…er short delay (#1590)

Co-authored-by: Simon <simon@aam-digital.com>
  • Loading branch information
sleidig and TheSlimvReal committed Dec 16, 2022
1 parent 53375b7 commit 6b16446
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/app/child-dev-project/notes/model/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@ export class Note extends Entity {
* (such as the date, author, e.t.c.) as well as copying the
* child-array
*/
copy(): Note {
const note: Note = super.copy() as Note;
copy(): this {
const note = super.copy();
note.children = [...this.children];
note.schools = [...this.schools];
note.relatedEntities = [...this.relatedEntities];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ describe("EntitySubrecordComponent", () => {

const sortedNames = component.recordsDataSource
._orderData(component.recordsDataSource.data)
.map((row: TableRow<Child>) => row.record.name);
.map((row) => row.record["name"]);

expect(sortedNames).toEqual(["A", "b", "C"]);
});
Expand Down Expand Up @@ -194,7 +194,7 @@ describe("EntitySubrecordComponent", () => {
const child = new Child();
child.name = "Child Name";
child.projectNumber = "01";
const tableRow: TableRow<Child> = { record: child };
const tableRow: TableRow<Entity> = { record: child };
const media = TestBed.inject(ScreenWidthObserver);
spyOn(media, "isDesktop").and.returnValue(true);

Expand Down Expand Up @@ -343,7 +343,7 @@ describe("EntitySubrecordComponent", () => {
const c3 = Child.create("Matching");
c3.dateOfBirth = new DateWithAge(moment().subtract(3, "years").toDate());
// get type-safety for filters
const childComponent = component as EntitySubrecordComponent<Child>;
const childComponent = component as any as EntitySubrecordComponent<Child>;
childComponent.records = [c1, c2, c3];

childComponent.filter = { name: "Matching" };
Expand Down Expand Up @@ -372,20 +372,22 @@ describe("EntitySubrecordComponent", () => {
]);
});

it("should remove an entity if it does not pass the filter anymore", async () => {
it("should remove an entity if it does not pass the filter anymore", fakeAsync(() => {
const entityMapper = TestBed.inject(EntityMapperService);
const child = new Child();
child.gender = genders[1];
await entityMapper.save(child);
entityMapper.save(child);
tick();
component.records = [child];
component.filter = { "gender.id": genders[1].id } as any;
component.ngOnInit();

expect(component.recordsDataSource.data).toEqual([{ record: child }]);

child.gender = genders[2];
await entityMapper.save(child);
entityMapper.save(child);
tick(5000);

expect(component.recordsDataSource.data).toEqual([]);
});
}));
});
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,19 @@ export class EntitySubrecordComponent<T extends Entity>
.subscribe(({ entity, type }) => {
if (type === "new") {
this.addToTable(entity);
} else if (type === "remove" || !this.predicate(entity)) {
} else if (type === "remove") {
this.removeFromDataTable(entity);
} else if (
type === "update" &&
!this._records.find((rec) => rec.getId() === entity.getId())
) {
this.addToTable(entity);
}

if (!this.predicate(entity)) {
// hide after a short delay to give a signal in the UI why records disappear by showing the changed values first
setTimeout(() => this.initDataSource(), 5000);
}
});
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/app/core/entity/model/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ export class Entity {
/**
* Get the class (Entity or the actual subclass of the instance) to call static methods on the correct class considering inheritance
*/
getConstructor<T extends Entity>(this: T): EntityConstructor<T> {
return this.constructor as EntityConstructor<T>;
getConstructor(): EntityConstructor<this> {
return this.constructor as EntityConstructor<this>;
}

/**
Expand Down Expand Up @@ -326,7 +326,7 @@ export class Entity {
* The resulting entity will be of the same type as this
* (taking into account subclassing)
*/
public copy(): Entity {
public copy(): this {
const other = new (this.getConstructor())(this._id);
Object.assign(other, this);
return other;
Expand Down

0 comments on commit 6b16446

Please sign in to comment.