import { Component , Input , OnInit } from "@angular/core" ; import { ChildrenService } from "../children.service" ; import { Router } from "@angular/router" ; import { OnInitDynamicComponent } from "../../../core/view/dynamic-components/on-init-dynamic-component.interface" ; import { take } from "rxjs/operators" ; import { ConfigurableEnumValue } from "../../../core/configurable-enum/configurable-enum.interface" ; import { Child } from "../model/child" ; import { UntilDestroy , untilDestroyed } from "@ngneat/until-destroy" ; @ UntilDestroy ( ) @ Component ( { selector : "app-children-count-dashboard" , templateUrl : "./children-count-dashboard.component.html" , styleUrls : [ "./children-count-dashboard.component.scss" ] , } ) export class ChildrenCountDashboardComponent implements OnInitDynamicComponent , OnInit { /**
     * The property of the Child entities to group counts by.
     *
     * Default is "center".
     */ @ Input ( ) groupBy : string = "center" ; totalChildren : number ; childrenGroupCounts : { label : string ; value : number ; } [ ] = [ ] ; constructor ( private childrenService , public router ) { } onInitFromDynamicConfig ( config ) { if ( config ?. groupBy ) { this . groupBy = config . groupBy ; } } ngOnInit ( ) { this . childrenService . getChildren ( ) . pipe ( untilDestroyed ( this ) , take ( 1 ) ) . subscribe ( ( results ) => { this . updateCounts ( results ) ; } ) ; } goToChildrenList ( filterString ) { const params = { } ; params [ this . groupBy ] = filterString . toLocaleLowerCase ( ) ; const path = "/" + Child . ENTITY_TYPE . toLowerCase ( ) ; this . router . navigate ( [ path ] , { queryParams : params } ) ; } private updateCounts ( children ) { this . totalChildren = 0 ; const countMap = new Map < any , number > ( ) ; children . forEach ( ( child ) => { if ( child . isActive ) { let count = countMap . get ( child [ this . groupBy ] ) ; if ( count === undefined ) { count = 0 ; } count ++ ; this . totalChildren ++ ; countMap . set ( child [ this . groupBy ] , count ) ; } } ) ; this . childrenGroupCounts = Array . from ( countMap . entries ( ) ) . map ( ( entry ) => ( { label : extractHumanReadableLabel ( entry [ 0 ] ) , value : entry [ 1 ] , } ) ) ; } } param value function extractHumanReadableLabel ( value ) { if ( value === undefined ) { return "" ; } if ( typeof value === "string" ) { return value ; } if ( value ?. label ) { return value . label ; } return String ( value ) ; }