diff --git a/src/app/child-dev-project/schools/activities-overview/activities-overview.component.html b/src/app/child-dev-project/schools/activities-overview/activities-overview.component.html index 6d80f2895a..4e38fbac19 100644 --- a/src/app/child-dev-project/schools/activities-overview/activities-overview.component.html +++ b/src/app/child-dev-project/schools/activities-overview/activities-overview.component.html @@ -1,5 +1,5 @@ { let component: ActivitiesOverviewComponent; @@ -20,7 +27,7 @@ describe("ActivitiesOverviewComponent", () => { beforeEach(async () => { entityMapper = mockEntityMapper(); await TestBed.configureTestingModule({ - declarations: [ActivitiesOverviewComponent], + imports: [SchoolsModule, MockedTestingModule.withState()], providers: [{ provide: EntityMapperService, useValue: entityMapper }], }).compileComponents(); }); @@ -28,6 +35,7 @@ describe("ActivitiesOverviewComponent", () => { beforeEach(() => { fixture = TestBed.createComponent(ActivitiesOverviewComponent); component = fixture.componentInstance; + component.entity = new School(); fixture.detectChanges(); }); @@ -55,20 +63,24 @@ describe("ActivitiesOverviewComponent", () => { expect(newRecurringActivity().linkedGroups).toEqual(["school1"]); }); - it("should remove the recurring activity from the table view if the current school is removed as a group of this recurring activity", async () => { + it("should remove the recurring activity from the table view if the current school is removed as a group of this recurring activity", fakeAsync(() => { const school1 = new School("school1"); const activity1 = new RecurringActivity(); activity1.linkedGroups = ["school1"]; const activity2 = new RecurringActivity(); - activity1.linkedGroups = ["school1", "school2", "school3"]; + activity2.linkedGroups = ["school1", "school2", "school3"]; entityMapper.addAll([activity1, activity2]); const subject = new Subject>(); spyOn(entityMapper, "receiveUpdates").and.returnValue(subject); - await component.onInitFromDynamicConfig({ entity: school1 }); + component.onInitFromDynamicConfig({ entity: school1 }); + tick(); + + expect(component.records).toEqual([activity1, activity2]); activity2.linkedGroups = ["school2", "school3"]; subject.next({ entity: activity2, type: "update" }); + tick(); expect(component.records).toEqual([activity1]); - }); + })); }); diff --git a/src/app/child-dev-project/schools/activities-overview/activities-overview.component.ts b/src/app/child-dev-project/schools/activities-overview/activities-overview.component.ts index 5f11e79e02..9d6f091cf7 100644 --- a/src/app/child-dev-project/schools/activities-overview/activities-overview.component.ts +++ b/src/app/child-dev-project/schools/activities-overview/activities-overview.component.ts @@ -6,6 +6,7 @@ import { EntityMapperService } from "app/core/entity/entity-mapper.service"; import { Entity } from "app/core/entity/model/entity"; import { DynamicComponent } from "app/core/view/dynamic-components/dynamic-component.decorator"; import { OnInitDynamicComponent } from "app/core/view/dynamic-components/on-init-dynamic-component.interface"; +import { delay } from "rxjs"; @DynamicComponent("ActivitiesOverview") @Component({ @@ -24,7 +25,6 @@ export class ActivitiesOverviewComponent implements OnInitDynamicComponent { entity: Entity; records: RecurringActivity[] = []; listConfig: EntityListConfig; - activityConstructor = RecurringActivity; constructor(private entityMapper: EntityMapperService) {} @@ -37,8 +37,11 @@ export class ActivitiesOverviewComponent implements OnInitDynamicComponent { this.records = ( await this.entityMapper.loadType(RecurringActivity) ).filter((activity) => activity.linkedGroups.includes(this.entity.getId())); + this.entityMapper .receiveUpdates(RecurringActivity) + // using short delay to make sure the EntitySubrecord's `receiveUpdates` code is executed before this + .pipe(delay(0)) .subscribe((updateEntity) => { if (updateEntity.type === "update") { this.records = this.records.filter((activity) => diff --git a/src/app/core/entity-components/entity-subrecord/entity-subrecord/entity-subrecord.component.ts b/src/app/core/entity-components/entity-subrecord/entity-subrecord/entity-subrecord.component.ts index 890f03800e..c7562bb660 100644 --- a/src/app/core/entity-components/entity-subrecord/entity-subrecord/entity-subrecord.component.ts +++ b/src/app/core/entity-components/entity-subrecord/entity-subrecord/entity-subrecord.component.ts @@ -1,8 +1,10 @@ import { Component, + EventEmitter, Input, OnChanges, OnInit, + Output, SimpleChanges, ViewChild, } from "@angular/core"; @@ -72,9 +74,9 @@ export class EntitySubrecordComponent _columns: FormFieldConfig[] = []; filteredColumns: FormFieldConfig[] = []; - /** data to be displayed */ + /** data to be displayed, can also be used as two-way-binding */ @Input() - set records(value: Array) { + set records(value: T[]) { this._records = value; this.recordsDataSource.data = this._records.map((rec) => { return { @@ -86,7 +88,9 @@ export class EntitySubrecordComponent new (this._records[0].getConstructor() as EntityConstructor)(); } } - private _records: Array = []; + private _records: T[] = []; + + @Output() recordsChange = new EventEmitter(); /** * factory method to create a new instance of the displayed Entity type @@ -318,11 +322,13 @@ export class EntitySubrecordComponent this.records = this._records.filter( (rec) => rec.getId() !== deleted.getId() ); + this.recordsChange.emit(this._records); } private addToTable(record: T) { // use setter so datasource is also updated this.records = [record].concat(this._records); + this.recordsChange.emit(this._records); } /** diff --git a/src/app/core/entity-components/entity-subrecord/row-details/row-details.component.html b/src/app/core/entity-components/entity-subrecord/row-details/row-details.component.html index 3b069f67c8..c31553ff53 100644 --- a/src/app/core/entity-components/entity-subrecord/row-details/row-details.component.html +++ b/src/app/core/entity-components/entity-subrecord/row-details/row-details.component.html @@ -60,16 +60,14 @@