import { Component, computed, DestroyRef, effect, inject, input, signal, untracked } from '@angular/core'; import { HttpErrorResponse } from '@angular/common/http'; import { forkJoin, Subscription, timeout, TimeoutError } from 'rxjs'; import { MatButtonModule } from '@angular/material/button'; import { MatButtonToggleModule } from '@angular/material/button-toggle'; import { MatFormFieldModule } from '@angular/material/form-field'; import { MatProgressBarModule } from '@angular/material/progress-bar'; import { MatSelectModule } from '@angular/material/select'; import { PlotlyChart } from '../../shared/plotly/plotly-chart'; import { Dominio, IndicadorDatos, Tema, TipoPeriodo } from './indicadores.models'; import { etiquetaPeriodo } from './indicadores-historico-figures'; import { httpMessage } from './indicadores-http'; import { IndicadoresSnapshots } from './indicadores-snapshots'; import { ROUTE_IDS, RouteIncompatible, RouteView, routeCellText, routeFigure, routeHasData, routeModel } from './screening-route'; /** Days along the screening route, from the sample to each event, as horizontal bars: cumulative or step by step. */ @Component({ selector: 'app-indicador-screening-route', imports: [MatButtonModule, MatButtonToggleModule, MatFormFieldModule, MatProgressBarModule, MatSelectModule, PlotlyChart], templateUrl: './indicador-screening-route.html', styleUrl: './indicador-screening-route.scss', }) export class IndicadorScreeningRoute { readonly dominio = input.required(); readonly tema = input.required(); readonly tipo = input('semestre'); /** The measures load once the region is visible. */ readonly visible = input(true); readonly status = signal<'deferred' | 'loading' | 'ready' | 'empty' | 'error' | 'incompatible'>('deferred'); readonly message = signal(''); readonly view = signal('cumulative'); readonly requestedPeriod = signal(undefined); private readonly payloads = signal | undefined>(undefined); readonly model = computed(() => { const data = this.payloads(); return data ? routeModel(data, this.tipo(), this.requestedPeriod()) : undefined; }); readonly figure = computed(() => { const model = this.model(); return model && routeHasData(model) ? routeFigure(model, this.view(), this.tema()) : undefined; }); readonly periodLabel = etiquetaPeriodo; readonly cellText = routeCellText; private readonly snapshots = inject(IndicadoresSnapshots); private subscription?: Subscription; constructor() { effect(() => { const dominio = this.dominio(), tipo = this.tipo(); if (!this.visible()) { this.status.set('deferred'); return; } untracked(() => this.load(dominio, tipo)); }); inject(DestroyRef).onDestroy(() => this.subscription?.unsubscribe()); } retry() { this.load(this.dominio(), this.tipo()); } selectPeriod(period: string) { this.requestedPeriod.set(period); } selectView(view: RouteView) { this.view.set(view); } private load(dominio: Dominio, tipo: TipoPeriodo) { this.subscription?.unsubscribe(); this.payloads.set(undefined); this.status.set('loading'); const requests = Object.fromEntries(ROUTE_IDS.map(id => [id, this.snapshots.datos(dominio, id, tipo)])); this.subscription = forkJoin(requests).pipe(timeout({ first: 40000 })).subscribe({ next: data => { try { const model = routeModel(data, tipo, this.requestedPeriod()); this.payloads.set(data); this.status.set(routeHasData(model) ? 'ready' : 'empty'); } catch (error) { this.failure(error); } }, error: error => this.failure(error), }); } private failure(error: unknown) { this.payloads.set(undefined); this.message.set(error instanceof TimeoutError ? 'El servicio tardó demasiado. Vuelve a intentar.' : error instanceof RouteIncompatible ? error.message : httpMessage(error)); this.status.set(error instanceof RouteIncompatible || (error instanceof Error && !(error instanceof HttpErrorResponse) && !(error instanceof TimeoutError)) ? 'incompatible' : 'error'); } }