139 lines
6.9 KiB
TypeScript
139 lines
6.9 KiB
TypeScript
import { ChangeDetectorRef, Component, inject, Input, OnChanges, OnDestroy, signal, SimpleChanges } from '@angular/core';
|
|
import { HttpErrorResponse } from '@angular/common/http';
|
|
import { TitleCasePipe } from '@angular/common';
|
|
import { combineLatest, Subscription, timeout, TimeoutError } from 'rxjs';
|
|
import { MatButtonModule } from '@angular/material/button';
|
|
import { MatProgressBarModule } from '@angular/material/progress-bar';
|
|
import { PlotlyChart } from '../../shared/plotly/plotly-chart';
|
|
import { Dominio, Indicador, PlotlyFigure, Tema } from './indicadores.models';
|
|
import { filterCompatibility, GlobalApplied } from './indicadores-memory';
|
|
import { IndicadoresSnapshots, snapshotKey } from './indicadores-snapshots';
|
|
import { httpMessage } from './indicadores-http';
|
|
import { ConteosIncompatibles, donaConteos, donaPacientes, ModeloConteos, modelosConteos, ParConteos, totalExamenes, totalPacientes } from './indicadores-conteos-pareados';
|
|
|
|
@Component({
|
|
selector: 'app-indicador-conteos-pareados',
|
|
imports: [MatButtonModule, MatProgressBarModule, PlotlyChart, TitleCasePipe],
|
|
templateUrl: './indicador-conteos-pareados.html',
|
|
styleUrl: './indicador-conteos-pareados.scss',
|
|
})
|
|
export class IndicadorConteosPareados implements OnChanges, OnDestroy {
|
|
@Input({ required: true }) dominio!: Dominio;
|
|
@Input({ required: true }) par!: ParConteos;
|
|
// The card's indicator (the exams); its partner (the patients) has no card and is requested by id.
|
|
@Input({ required: true }) indicador!: Indicador;
|
|
@Input({ required: true }) tema!: Tema;
|
|
@Input({ required: true }) applied!: GlobalApplied;
|
|
@Input() visible = true;
|
|
readonly status = signal<'deferred' | 'loading' | 'ready' | 'empty' | 'error' | 'incompatible'>('deferred');
|
|
message = '';
|
|
periodIndex = 0;
|
|
pages: ModeloConteos[] = [];
|
|
donutFigure?: PlotlyFigure;
|
|
patientsFigure?: PlotlyFigure;
|
|
private readonly snapshots = inject(IndicadoresSnapshots);
|
|
private readonly changeDetector = inject(ChangeDetectorRef);
|
|
private subscription?: Subscription;
|
|
private key = '';
|
|
private loaded = false;
|
|
private anchoDona = 390;
|
|
private anchoPacientes = 390;
|
|
|
|
get model(): ModeloConteos | undefined { return this.pages[this.periodIndex]; }
|
|
get total() { return this.model ? totalExamenes(this.model) : null; }
|
|
get totalPacientes() { return this.model ? totalPacientes(this.model) : null; }
|
|
texto(n: number | null) { return n === null ? 'Sin dato' : n.toLocaleString('es-MX'); }
|
|
textoPacientes(n: number | null) { return n === null && this.model && !this.model.pacientesVerificados ? 'Pendiente' : this.texto(n); }
|
|
helpWhich: 'examenes' | 'pacientes' | null = null;
|
|
private helpPinned: 'examenes' | 'pacientes' | null = null;
|
|
toggleHelp(which: 'examenes' | 'pacientes') {
|
|
if (this.helpPinned === which) { this.helpPinned = null; this.helpWhich = null; return; }
|
|
this.helpPinned = which;
|
|
this.openHelp(which);
|
|
}
|
|
openHelp(which: 'examenes' | 'pacientes') {
|
|
this.helpWhich = which;
|
|
}
|
|
hideHelp(which: 'examenes' | 'pacientes') {
|
|
if (this.helpPinned === which) return;
|
|
if (this.helpWhich === which) this.helpWhich = null;
|
|
}
|
|
onHelpFocusOut(which: 'examenes' | 'pacientes', event: FocusEvent) {
|
|
const next = event.relatedTarget;
|
|
if (next instanceof Node && (event.currentTarget as Node).contains(next)) return;
|
|
if (this.helpPinned === which) this.helpPinned = null;
|
|
if (this.helpWhich === which) this.helpWhich = null;
|
|
}
|
|
|
|
ngOnChanges(changes: SimpleChanges) {
|
|
const key = snapshotKey(this.dominio, this.indicador, this.applied) + JSON.stringify([this.par.examenes, this.par.pacientes, this.indicador.tipos_periodo]);
|
|
if (key !== this.key) {
|
|
this.subscription?.unsubscribe(); this.key = key; this.loaded = false; this.clear();
|
|
if (!this.visible) { this.status.set('deferred'); return; }
|
|
this.load();
|
|
} else if (this.visible && !this.loaded) this.load();
|
|
else if (this.model && changes['tema']) this.draw();
|
|
}
|
|
retry() { if (this.visible) this.load(); }
|
|
private clear() { this.pages = []; this.periodIndex = 0; this.donutFigure = undefined; this.patientsFigure = undefined; }
|
|
private load() {
|
|
this.subscription?.unsubscribe();
|
|
this.loaded = true; this.clear();
|
|
const compatibility = filterCompatibility(this.applied, this.indicador.tipos_periodo);
|
|
if (this.indicador.id !== this.par.examenes || this.indicador.contrato !== 'C2' || !compatibility.compatible) {
|
|
this.message = !compatibility.compatible ? compatibility.reason : 'El catálogo no publica el conteo necesario para esta vista.';
|
|
this.status.set('incompatible'); return;
|
|
}
|
|
const key = this.key;
|
|
const periods = this.applied.selection.mode === 'explicit' ? [...this.applied.selection.periodos] : undefined;
|
|
this.status.set('loading');
|
|
this.subscription = combineLatest([
|
|
this.snapshots.datos(this.dominio, this.par.examenes, this.applied.tipo, periods, this.indicador.version),
|
|
this.snapshots.datos(this.dominio, this.par.pacientes, this.applied.tipo, periods),
|
|
]).pipe(timeout({ first: 40000 })).subscribe({
|
|
next: ([examenes, pacientes]) => {
|
|
if (key !== this.key) return;
|
|
try {
|
|
this.pages = modelosConteos(this.par, examenes, pacientes, this.applied.tipo);
|
|
// The most recent period first; navigation goes back.
|
|
this.periodIndex = Math.max(0, this.pages.length - 1);
|
|
this.draw(); this.status.set(this.model ? 'ready' : 'empty');
|
|
} catch (error) { this.failure(error); }
|
|
},
|
|
error: error => { if (key === this.key) this.failure(error); },
|
|
});
|
|
}
|
|
private failure(error: unknown) {
|
|
this.clear();
|
|
this.message = error instanceof TimeoutError ? 'El servicio tardó demasiado. Vuelve a intentar.'
|
|
: error instanceof ConteosIncompatibles ? error.message : httpMessage(error);
|
|
this.status.set(error instanceof ConteosIncompatibles || error instanceof Error && !(error instanceof HttpErrorResponse) && !(error instanceof TimeoutError)
|
|
? 'incompatible' : 'error');
|
|
this.changeDetector.markForCheck();
|
|
}
|
|
changePeriod(step: number) {
|
|
if (![1, -1].includes(step) || !this.pages[this.periodIndex + step]) return;
|
|
this.periodIndex += step; this.draw();
|
|
}
|
|
ajustarAnchoDona(ancho: number) {
|
|
if (ancho === this.anchoDona) return;
|
|
this.anchoDona = ancho;
|
|
const m = this.model;
|
|
if (m) this.donutFigure = donaConteos(m, this.par, this.tema, ancho);
|
|
}
|
|
ajustarAnchoPacientes(ancho: number) {
|
|
if (ancho === this.anchoPacientes) return;
|
|
this.anchoPacientes = ancho;
|
|
const m = this.model;
|
|
if (m) this.patientsFigure = donaPacientes(m, this.par, this.tema, ancho);
|
|
}
|
|
private draw() {
|
|
const m = this.model;
|
|
if (!m) return;
|
|
this.donutFigure = donaConteos(m, this.par, this.tema, this.anchoDona);
|
|
this.patientsFigure = donaPacientes(m, this.par, this.tema, this.anchoPacientes);
|
|
this.changeDetector.markForCheck();
|
|
}
|
|
ngOnDestroy() { this.subscription?.unsubscribe(); }
|
|
}
|