|
| 1 | +/** |
| 2 | + * Copyright since 2025 Mifos Initiative |
| 3 | + * |
| 4 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 5 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 7 | + */ |
| 8 | + |
| 9 | +/* eslint-disable @angular-eslint/prefer-inject */ |
| 10 | +/** Angular Imports */ |
| 11 | +import { Component, Input, OnChanges, OnDestroy, OnInit, SimpleChanges } from '@angular/core'; |
| 12 | +import { UntypedFormBuilder, UntypedFormGroup } from '@angular/forms'; |
| 13 | +import { Subscription, forkJoin } from 'rxjs'; |
| 14 | +import { map } from 'rxjs/operators'; |
| 15 | +import { MatButtonToggle, MatButtonToggleGroup } from '@angular/material/button-toggle'; |
| 16 | +/** Custom Services */ |
| 17 | +import { AuthenticationService } from 'app/core/authentication/authentication.service'; |
| 18 | +import { AnalyticsDataSourceService } from '../services/analytics-data-source.service'; |
| 19 | +import { AnalyticsVisibilityService } from '../services/analytics-visibility.service'; |
| 20 | +/** Custom Models */ |
| 21 | +import { |
| 22 | + AnalyticsDashboardDefinition, |
| 23 | + AnalyticsFilters, |
| 24 | + AnalyticsWidgetDefinition, |
| 25 | + AnalyticsWidgetState |
| 26 | +} from '../models/analytics-dashboard.model'; |
| 27 | +/** Custom Imports */ |
| 28 | +import { STANDALONE_SHARED_IMPORTS } from 'app/standalone-shared.module'; |
| 29 | +import { DashboardWidgetComponent } from '../dashboard-widget/dashboard-widget.component'; |
| 30 | + |
| 31 | +@Component({ |
| 32 | + selector: 'mifosx-analytics-dashboard', |
| 33 | + standalone: true, |
| 34 | + templateUrl: './dashboard-engine.component.html', |
| 35 | + styleUrls: ['./dashboard-engine.component.scss'], |
| 36 | + imports: [ |
| 37 | + ...STANDALONE_SHARED_IMPORTS, |
| 38 | + MatButtonToggleGroup, |
| 39 | + MatButtonToggle, |
| 40 | + DashboardWidgetComponent |
| 41 | + ] |
| 42 | +}) |
| 43 | +export class DashboardEngineComponent implements OnInit, OnChanges, OnDestroy { |
| 44 | + @Input({ required: true }) dashboard!: AnalyticsDashboardDefinition; |
| 45 | + @Input() offices: any[] = []; |
| 46 | + |
| 47 | + filtersForm!: UntypedFormGroup; |
| 48 | + visibleWidgets: AnalyticsWidgetDefinition[] = []; |
| 49 | + widgetStateMap: Record<string, AnalyticsWidgetState> = {}; |
| 50 | + |
| 51 | + private filtersSubscription?: Subscription; |
| 52 | + private loadSubscription?: Subscription; |
| 53 | + |
| 54 | + constructor( |
| 55 | + private formBuilder: UntypedFormBuilder, |
| 56 | + private authenticationService: AuthenticationService, |
| 57 | + private analyticsDataSourceService: AnalyticsDataSourceService, |
| 58 | + private analyticsVisibilityService: AnalyticsVisibilityService |
| 59 | + ) {} |
| 60 | + get metricWidgets(): AnalyticsWidgetDefinition[] { |
| 61 | + return this.visibleWidgets.filter((widget) => widget.type === 'metric'); |
| 62 | + } |
| 63 | + |
| 64 | + get chartWidgets(): AnalyticsWidgetDefinition[] { |
| 65 | + return this.visibleWidgets.filter((widget) => widget.type === 'chart'); |
| 66 | + } |
| 67 | + |
| 68 | + ngOnInit(): void { |
| 69 | + this.updateVisibleWidgets(); |
| 70 | + |
| 71 | + this.filtersForm = this.formBuilder.group({ |
| 72 | + officeId: [this.resolveDefaultOfficeId()], |
| 73 | + timescale: ['Month'] |
| 74 | + }); |
| 75 | + |
| 76 | + this.filtersSubscription = this.filtersForm.valueChanges.subscribe(() => { |
| 77 | + this.reloadDashboard(); |
| 78 | + }); |
| 79 | + |
| 80 | + this.reloadDashboard(); |
| 81 | + } |
| 82 | + |
| 83 | + ngOnChanges(changes: SimpleChanges): void { |
| 84 | + if (changes['dashboard']) { |
| 85 | + this.updateVisibleWidgets(); |
| 86 | + if (this.filtersForm) { |
| 87 | + this.reloadDashboard(); |
| 88 | + } |
| 89 | + } |
| 90 | + if ( |
| 91 | + changes['offices'] && |
| 92 | + this.filtersForm && |
| 93 | + !this.offices.some((office) => office.id === this.filtersForm.value.officeId) |
| 94 | + ) { |
| 95 | + this.filtersForm.patchValue( |
| 96 | + { |
| 97 | + officeId: this.resolveDefaultOfficeId() |
| 98 | + }, |
| 99 | + { emitEvent: false } |
| 100 | + ); |
| 101 | + this.reloadDashboard(); |
| 102 | + } |
| 103 | + } |
| 104 | + ngOnDestroy(): void { |
| 105 | + if (this.filtersSubscription) { |
| 106 | + this.filtersSubscription.unsubscribe(); |
| 107 | + } |
| 108 | + |
| 109 | + if (this.loadSubscription) { |
| 110 | + this.loadSubscription.unsubscribe(); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + reloadDashboard(forceRefresh: boolean = false): void { |
| 115 | + if (!this.visibleWidgets.length) { |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + if (forceRefresh) { |
| 120 | + this.analyticsDataSourceService.clearCache(); |
| 121 | + } |
| 122 | + |
| 123 | + if (this.loadSubscription) { |
| 124 | + this.loadSubscription.unsubscribe(); |
| 125 | + } |
| 126 | + |
| 127 | + const filters = this.filtersForm.getRawValue() as AnalyticsFilters; |
| 128 | + this.widgetStateMap = this.visibleWidgets.reduce( |
| 129 | + (accumulator, widget) => ({ |
| 130 | + ...accumulator, |
| 131 | + [widget.id]: { |
| 132 | + loading: true, |
| 133 | + empty: false |
| 134 | + } |
| 135 | + }), |
| 136 | + {} |
| 137 | + ); |
| 138 | + |
| 139 | + this.loadSubscription = forkJoin( |
| 140 | + this.visibleWidgets.map((widget) => |
| 141 | + this.analyticsDataSourceService.loadWidget(widget, filters).pipe( |
| 142 | + map((state) => ({ |
| 143 | + widgetId: widget.id, |
| 144 | + state |
| 145 | + })) |
| 146 | + ) |
| 147 | + ) |
| 148 | + ).subscribe({ |
| 149 | + next: (results) => { |
| 150 | + this.widgetStateMap = results.reduce( |
| 151 | + (accumulator, result) => ({ |
| 152 | + ...accumulator, |
| 153 | + [result.widgetId]: result.state |
| 154 | + }), |
| 155 | + {} |
| 156 | + ); |
| 157 | + }, |
| 158 | + error: () => { |
| 159 | + this.widgetStateMap = this.visibleWidgets.reduce( |
| 160 | + (accumulator, widget) => ({ |
| 161 | + ...accumulator, |
| 162 | + [widget.id]: { |
| 163 | + loading: false, |
| 164 | + empty: true |
| 165 | + } |
| 166 | + }), |
| 167 | + {} |
| 168 | + ); |
| 169 | + } |
| 170 | + }); |
| 171 | + } |
| 172 | + |
| 173 | + private resolveDefaultOfficeId(): number | null { |
| 174 | + const credentials = this.authenticationService.getCredentials(); |
| 175 | + const currentOfficeId = credentials?.officeId; |
| 176 | + |
| 177 | + if (currentOfficeId && this.offices.some((office) => office.id === currentOfficeId)) { |
| 178 | + return currentOfficeId; |
| 179 | + } |
| 180 | + |
| 181 | + return this.offices[0]?.id ?? null; |
| 182 | + } |
| 183 | + private updateVisibleWidgets(): void { |
| 184 | + this.visibleWidgets = (this.dashboard?.widgets || []).filter((widget) => |
| 185 | + this.analyticsVisibilityService.canView(widget.visibleTo) |
| 186 | + ); |
| 187 | + } |
| 188 | +} |
0 commit comments