import { Component , OnInit } from "@angular/core" ; import { Router } from "@angular/router" ; import { HealthCheck } from "../../health-checkup/model/health-check" ; import { OnInitDynamicComponent } from "../../../core/view/dynamic-components/on-init-dynamic-component.interface" ; import { take } from "rxjs/operators" ; import { ChildrenService } from "../children.service" ; import { Child } from "../model/child" ; import { WarningLevel } from "../../../core/entity/model/warning-level" ; import { UntilDestroy , untilDestroyed } from "@ngneat/until-destroy" ; interface BmiRow { childId : string ; bmi : number ; } @ UntilDestroy ( ) @ Component ( { selector : "app-children-bmi-dashboard" , templateUrl : "./children-bmi-dashboard.component.html" , styleUrls : [ "./children-bmi-dashboard.component.scss" ] , } ) export class ChildrenBmiDashboardComponent implements OnInit , OnInitDynamicComponent { public currentHealthCheck : HealthCheck ; bmiRows : BmiRow [ ] = [ ] ; constructor ( private childrenService , public router ) { } ngOnInit ( ) { this . childrenService . getChildren ( ) . pipe ( untilDestroyed ( this ) , take ( 1 ) ) . subscribe ( ( results ) => { this . filterBMI ( results ) ; } ) ; } onInitFromDynamicConfig ( config ) { } recordTrackByFunction = ( index , item ) => item . childId ; filterBMI ( children ) { children . forEach ( ( child ) => { this . childrenService . getHealthChecksOfChild ( child . getId ( ) ) . pipe ( untilDestroyed ( this ) ) . subscribe ( ( results ) => { /** get latest HealthCheck */ if ( results . length > 0 ) { this . currentHealthCheck = results . reduce ( ( prev , cur ) => cur . date > prev . date ? cur : prev ) ; /**Check health status */ if ( this . currentHealthCheck . getWarningLevel ( ) === WarningLevel . URGENT ) { this . bmiRows . push ( { childId : child . getId ( ) , bmi : this . currentHealthCheck . bmi , } ) ; } } } ) ; } ) ; } goToChild ( childId ) { const path = "/" + Child . ENTITY_TYPE . toLowerCase ( ) ; this . router . navigate ( [ path , childId ] ) ; } }