Skip to content

Commit

Permalink
fix: show new health an observation records immediately
Browse files Browse the repository at this point in the history
fixes #2186
  • Loading branch information
sleidig committed Jan 22, 2024
1 parent 8ede717 commit d622ba8
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 62 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,53 @@ import { Child } from "../../model/child";
import { FormFieldConfig } from "../../../../core/common-components/entity-form/FormConfig";
import { DynamicComponent } from "../../../../core/config/dynamic-components/dynamic-component.decorator";
import { EntitiesTableComponent } from "../../../../core/common-components/entities-table/entities-table.component";
import { RelatedEntitiesComponent } from "../../../../core/entity-details/related-entities/related-entities.component";
import { EntityMapperService } from "../../../../core/entity/entity-mapper/entity-mapper.service";
import { EntityRegistry } from "../../../../core/entity/database-entity.decorator";
import { ScreenWidthObserver } from "../../../../utils/media/screen-size-observer.service";

@DynamicComponent("HealthCheckup")
@Component({
selector: "app-health-checkup",
templateUrl: "./health-checkup.component.html",
templateUrl:
"../../../../core/entity-details/related-entities/related-entities.component.html",
imports: [EntitiesTableComponent],
standalone: true,
})
export class HealthCheckupComponent implements OnInit {
records: HealthCheck[] = [];
export class HealthCheckupComponent
extends RelatedEntitiesComponent<HealthCheck>
implements OnInit
{
@Input() entity: Child;
property = "child";
entityCtr = HealthCheck;

/**
* Column Description for the SubentityRecordComponent
* Column Description
* The Date-Column needs to be transformed to apply the MathFormCheck in the SubentityRecordComponent
* BMI is rounded to 2 decimal digits
*/
@Input() config: { columns: FormFieldConfig[] } = {
columns: [
{ id: "date" },
{ id: "height" },
{ id: "weight" },
{
id: "bmi",
label: $localize`:Table header, Short for Body Mass Index:BMI`,
viewComponent: "ReadonlyFunction",
description: $localize`:Tooltip for BMI info:This is calculated using the height and the weight measure`,
additional: (entity: HealthCheck) => this.getBMI(entity),
},
],
};
@Input() entity: Child;
override _columns: FormFieldConfig[] = [
{ id: "date" },
{ id: "height" },
{ id: "weight" },
{
id: "bmi",
label: $localize`:Table header, Short for Body Mass Index:BMI`,
viewComponent: "ReadonlyFunction",
description: $localize`:Tooltip for BMI info:This is calculated using the height and the weight measure`,
additional: (entity: HealthCheck) => this.getBMI(entity),
},
];

constructor(private childrenService: ChildrenService) {}
constructor(
private childrenService: ChildrenService,
entityMapper: EntityMapperService,
entityRegistry: EntityRegistry,
screenWidthObserver: ScreenWidthObserver,
) {
super(entityMapper, entityRegistry, screenWidthObserver);
}

private getBMI(healthCheck: HealthCheck): string {
const bmi = healthCheck.bmi;
Expand All @@ -49,16 +62,11 @@ export class HealthCheckupComponent implements OnInit {
}
}

ngOnInit() {
return this.loadData();
}

generateNewRecordFactory() {
override createNewRecordFactory() {
return () => {
const newHC = new HealthCheck(Date.now().toString());
const newHC = new HealthCheck();

// use last entered date as default, otherwise today's date
newHC.date = this.records.length > 0 ? this.records[0].date : new Date();
newHC.date = new Date();
newHC.child = this.entity.getId();

return newHC;
Expand All @@ -68,11 +76,10 @@ export class HealthCheckupComponent implements OnInit {
/**
* implements the health check loading from the children service and is called in the onInit()
*/
async loadData() {
this.records = await this.childrenService.getHealthChecksOfChild(
this.entity.getId(),
);
this.records.sort(
override async initData() {
this.data = (
await this.childrenService.getHealthChecksOfChild(this.entity.getId())
).sort(
(a, b) =>
(b.date ? b.date.valueOf() : 0) - (a.date ? a.date.valueOf() : 0),
);
Expand Down
18 changes: 10 additions & 8 deletions src/app/core/config/config-fix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -640,14 +640,16 @@ export const defaultJsonConfig = {
{
"title": "",
"component": "HistoricalDataComponent",
"config": [
"date",
{ "id": "isMotivatedDuringClass", "visibleFrom": "lg" },
{ "id": "isParticipatingInClass", "visibleFrom": "lg" },
{ "id": "isInteractingWithOthers", "visibleFrom": "lg" },
{ "id": "doesHomework", "visibleFrom": "lg" },
{ "id": "asksQuestions", "visibleFrom": "lg" },
]
"config": {
"columns": [
"date",
{ "id": "isMotivatedDuringClass", "visibleFrom": "lg" },
{ "id": "isParticipatingInClass", "visibleFrom": "lg" },
{ "id": "isInteractingWithOthers", "visibleFrom": "lg" },
{ "id": "doesHomework", "visibleFrom": "lg" },
{ "id": "asksQuestions", "visibleFrom": "lg" },
]
}
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe("HistoricalDataComponent", () => {

await component.ngOnInit();

expect(component.entries).toEqual([relatedData]);
expect(component.data).toEqual([relatedData]);
expect(mockHistoricalDataService.getHistoricalDataFor).toHaveBeenCalledWith(
component.entity.getId(),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import { Component, Input, OnInit } from "@angular/core";
import { HistoricalEntityData } from "../model/historical-entity-data";
import { Entity } from "../../../core/entity/model/entity";
import { HistoricalDataService } from "../historical-data.service";
import { FormFieldConfig } from "../../../core/common-components/entity-form/FormConfig";
import { DynamicComponent } from "../../../core/config/dynamic-components/dynamic-component.decorator";
import { EntitiesTableComponent } from "../../../core/common-components/entities-table/entities-table.component";
import { RelatedEntitiesComponent } from "../../../core/entity-details/related-entities/related-entities.component";
import { EntityMapperService } from "../../../core/entity/entity-mapper/entity-mapper.service";
import { EntityRegistry } from "../../../core/entity/database-entity.decorator";
import { ScreenWidthObserver } from "../../../utils/media/screen-size-observer.service";
import { FormFieldConfig } from "../../../core/common-components/entity-form/FormConfig";

/**
* A general component that can be included on a entity details page through the config.
Expand All @@ -14,26 +18,37 @@ import { EntitiesTableComponent } from "../../../core/common-components/entities
@DynamicComponent("HistoricalDataComponent")
@Component({
selector: "app-historical-data",
template: ` <app-entities-table
[entityType]="entityConstructor"
[records]="entries"
[customColumns]="config"
[newRecordFactory]="getNewEntryFunction()"
></app-entities-table>`,
templateUrl:
"../../../core/entity-details/related-entities/related-entities.component.html",
imports: [EntitiesTableComponent],
standalone: true,
})
export class HistoricalDataComponent implements OnInit {
export class HistoricalDataComponent
extends RelatedEntitiesComponent<HistoricalEntityData>
implements OnInit
{
@Input() entity: Entity;
@Input() config: FormFieldConfig[] = [];
entries: HistoricalEntityData[];
property = "relatedEntity";
entityCtr = HistoricalEntityData;

entityConstructor = HistoricalEntityData;
/** @deprecated use @Input() columns instead */
@Input() set config(value: FormFieldConfig[]) {
if (Array.isArray(value)) {
this.columns = value;
}
}

constructor(private historicalDataService: HistoricalDataService) {}
constructor(
private historicalDataService: HistoricalDataService,
entityMapper: EntityMapperService,
entityRegistry: EntityRegistry,
screenWidthObserver: ScreenWidthObserver,
) {
super(entityMapper, entityRegistry, screenWidthObserver);
}

async ngOnInit() {
this.entries = await this.historicalDataService.getHistoricalDataFor(
override async initData() {
this.data = await this.historicalDataService.getHistoricalDataFor(
this.entity.getId(),
);
}
Expand Down

0 comments on commit d622ba8

Please sign in to comment.