142 lines
5.1 KiB
TypeScript
142 lines
5.1 KiB
TypeScript
import { Component, computed, inject, input, resource, signal } from '@angular/core';
|
|
import { MatButtonToggleModule } from '@angular/material/button-toggle';
|
|
import type { Measure, MeasureReport } from 'fhir/r4';
|
|
import { Fhir, resources } from '../fhir';
|
|
import { Loading } from '../loading';
|
|
import { Figure, Plot } from '../plot';
|
|
import { historyFigure } from './figures';
|
|
import { periodLabel, periods, PeriodType, reading } from './measures';
|
|
import { HISTORY, HistoryPanel } from './pages';
|
|
|
|
interface Drawn {
|
|
panel: HistoryPanel;
|
|
/** the group's heading, on the first panel of its run */
|
|
heading?: string;
|
|
figure?: Figure;
|
|
}
|
|
|
|
/** Trazabilidad as the old tab: the domain's declared panels (pages.HISTORY), in their order,
|
|
* each measure period by period. */
|
|
@Component({
|
|
selector: 'app-tab-trazabilidad',
|
|
imports: [Loading, MatButtonToggleModule, Plot],
|
|
template: `
|
|
<section class="card filters" aria-labelledby="history-filters">
|
|
<h2 id="history-filters" class="filters-title">Filtros</h2>
|
|
<mat-button-toggle-group
|
|
[value]="type()"
|
|
(change)="type.set($event.value)"
|
|
aria-label="Intervalo de análisis"
|
|
>
|
|
<mat-button-toggle value="month">Mensual</mat-button-toggle>
|
|
<mat-button-toggle value="semester">Semestral</mat-button-toggle>
|
|
</mat-button-toggle-group>
|
|
</section>
|
|
@if (series.isLoading()) {
|
|
<app-loading what="la trazabilidad" text="Cargando trazabilidad…" />
|
|
} @else if (series.error()) {
|
|
<p class="muted" role="alert">
|
|
No se pudo cargar la trazabilidad.
|
|
<button type="button" class="retry" (click)="series.reload()">Reintentar</button>
|
|
</p>
|
|
} @else {
|
|
@for (d of drawn(); track d.panel.measure) {
|
|
@if (d.heading) {
|
|
<h2 class="group-title">{{ d.heading }}</h2>
|
|
}
|
|
<section class="card panel" [attr.data-measure]="d.panel.measure">
|
|
@if (d.panel.group) {
|
|
<h3 class="panel-title">{{ d.panel.title }}</h3>
|
|
} @else {
|
|
<h2 class="panel-title">{{ d.panel.title }}</h2>
|
|
}
|
|
@if (d.figure) {
|
|
<app-plot [figure]="d.figure" [height]="320" />
|
|
} @else {
|
|
<p class="muted">Sin datos para el periodo.</p>
|
|
}
|
|
</section>
|
|
}
|
|
}
|
|
`,
|
|
styles: `
|
|
:host {
|
|
display: block;
|
|
padding-top: 16px;
|
|
}
|
|
.card {
|
|
background: var(--mat-sys-surface);
|
|
border: 1px solid var(--mat-sys-outline-variant);
|
|
border-radius: 12px;
|
|
padding: 16px 20px;
|
|
margin-bottom: 16px;
|
|
}
|
|
.filters-title {
|
|
font-size: 16px;
|
|
font-weight: 500;
|
|
margin: 0 0 12px;
|
|
}
|
|
.panel-title,
|
|
.group-title {
|
|
font-size: 20px;
|
|
font-weight: 700;
|
|
margin: 0 0 8px;
|
|
}
|
|
h3.panel-title {
|
|
font-size: 16px;
|
|
}
|
|
.group-title {
|
|
margin: 24px 0 12px;
|
|
}
|
|
`,
|
|
})
|
|
export class TabTrazabilidad {
|
|
private readonly fhir = inject(Fhir);
|
|
readonly domain = input.required<string>();
|
|
readonly measures = input.required<Measure[]>();
|
|
protected readonly type = signal<PeriodType>('month');
|
|
|
|
protected readonly series = resource({
|
|
params: () => ({ domain: this.domain(), type: this.type(), measures: this.measures() }),
|
|
loader: async ({ params }) => {
|
|
const history = HISTORY[params.domain];
|
|
if (!history) return [];
|
|
const asked = periods(params.type).reverse();
|
|
return Promise.all(
|
|
history.panels.map(async (panel) => {
|
|
const measure = params.measures.find((m) => m.name === panel.measure);
|
|
if (!measure) return { panel, points: [] };
|
|
const pages = await this.fhir.all(params.domain, 'MeasureReport', {
|
|
measure: measure.id!,
|
|
'reporting-period': asked.join(','),
|
|
});
|
|
// One report per period asked, in the order asked.
|
|
const found = resources<MeasureReport>(pages, 'MeasureReport');
|
|
return { panel, points: asked.map((period, i) => ({ period, r: reading(found[i]) })) };
|
|
}),
|
|
);
|
|
},
|
|
});
|
|
|
|
protected readonly drawn = computed<Drawn[]>(() => {
|
|
const history = HISTORY[this.domain()];
|
|
const xTitle = this.type() === 'month' ? 'Mes y año' : 'Semestre';
|
|
let group: string | undefined;
|
|
if (!this.series.hasValue()) return [];
|
|
return this.series.value().map(({ panel, points }) => {
|
|
const heading = panel.group && panel.group !== group ? panel.group : undefined;
|
|
group = panel.group;
|
|
const has = (p: (typeof points)[number]) => (p.r?.populations['initial-population'] ?? 0) > 0;
|
|
const first = points.findIndex(has);
|
|
if (first < 0 || !history) return { panel, heading };
|
|
const last = points.length - 1 - [...points].reverse().findIndex(has);
|
|
const shown =
|
|
history.months && this.type() === 'month'
|
|
? points.slice(Math.max(first, last + 1 - history.months), last + 1)
|
|
: points.slice(first, last + 1);
|
|
const series = shown.map((p) => ({ label: periodLabel(p.period), r: p.r }));
|
|
return { panel, heading, figure: historyFigure(panel, series, history.line, xTitle) };
|
|
});
|
|
});
|
|
}
|