dca39203d2
Added a "Periodos a mostrar" selector to the Trazabilidad filters, as in the reference history tab. - period-selection.ts: pure functions for the initial window, the periods shown for a choice, the select options, the year grouping and the labels. - periods-dialog.ts: Material dialog with the periods of the interval by year, checkboxes, choose all and clear all, and a count of what is chosen. - tab-trazabilidad.ts: the select offers the latest periods in steps of five, all, and a custom choice that opens the dialog; the choice is kept per interval. Every panel draws the same chosen periods. Choices: the periods offered are those with data in any panel. The default is the domain's declared window (the last 5 months for CaCu, months only) when there are more periods than it, otherwise all. Cancelling the dialog leaves the selection untouched. period-selection.spec.ts covers the selection functions.
244 lines
7.9 KiB
TypeScript
244 lines
7.9 KiB
TypeScript
import { Component, computed, inject, input, resource, signal } from '@angular/core';
|
|
import { MatButtonModule } from '@angular/material/button';
|
|
import { MatButtonToggleModule } from '@angular/material/button-toggle';
|
|
import { MatDialog } from '@angular/material/dialog';
|
|
import { MatFormFieldModule } from '@angular/material/form-field';
|
|
import { MatSelectModule } from '@angular/material/select';
|
|
import type { Measure, MeasureReport } from 'fhir/r4';
|
|
import { firstValueFrom } from 'rxjs';
|
|
import { Fhir, resources } from '../fhir';
|
|
import { Loading } from '../loading';
|
|
import { Figure, Plot } from '../plot';
|
|
import { historyFigure } from './figures';
|
|
import { periodLabel, periods, PeriodType, Reading, reading } from './measures';
|
|
import { HISTORY, HistoryPanel } from './pages';
|
|
import {
|
|
chronological,
|
|
CUSTOM_PERIODS,
|
|
initialWindow,
|
|
PeriodChoice,
|
|
visiblePeriods,
|
|
windowOptions,
|
|
} from './period-selection';
|
|
import { PeriodsDialog, PeriodsDialogData } from './periods-dialog';
|
|
|
|
interface Point {
|
|
period: string;
|
|
r: Reading | undefined;
|
|
}
|
|
|
|
const hasPopulation = (p: Point) => (p.r?.populations['initial-population'] ?? 0) > 0;
|
|
|
|
interface Drawn {
|
|
panel: HistoryPanel;
|
|
heading?: string;
|
|
figure?: Figure;
|
|
}
|
|
|
|
@Component({
|
|
selector: 'app-tab-trazabilidad',
|
|
imports: [
|
|
Loading,
|
|
MatButtonModule,
|
|
MatButtonToggleModule,
|
|
MatFormFieldModule,
|
|
MatSelectModule,
|
|
Plot,
|
|
],
|
|
template: `
|
|
<section class="card" aria-labelledby="history-filters">
|
|
<h2 id="history-filters" class="filters-title">Filtros</h2>
|
|
<div class="filters">
|
|
<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>
|
|
<mat-form-field appearance="outline" subscriptSizing="dynamic">
|
|
<mat-label>Periodos a mostrar</mat-label>
|
|
<mat-select [value]="selected()" (selectionChange)="choose($event.value)">
|
|
@for (option of options(); track option.value) {
|
|
<mat-option [value]="option.value">{{ option.label }}</mat-option>
|
|
}
|
|
</mat-select>
|
|
</mat-form-field>
|
|
@if (choice().window === customPeriods) {
|
|
<button type="button" mat-stroked-button (click)="pick()">Cambiar periodos</button>
|
|
}
|
|
</div>
|
|
</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 {
|
|
display: flex;
|
|
flex-wrap: wrap;
|
|
align-items: center;
|
|
gap: 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[]>();
|
|
private readonly dialog = inject(MatDialog);
|
|
protected readonly type = signal<PeriodType>('month');
|
|
protected readonly customPeriods = CUSTOM_PERIODS;
|
|
private readonly choices = signal<Partial<Record<PeriodType, PeriodChoice>>>({});
|
|
private readonly picking = signal(false);
|
|
|
|
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(','),
|
|
});
|
|
const found = resources<MeasureReport>(pages, 'MeasureReport');
|
|
return { panel, points: asked.map((period, i) => ({ period, r: reading(found[i]) })) };
|
|
}),
|
|
);
|
|
},
|
|
});
|
|
|
|
private readonly available = computed(() => {
|
|
if (!this.series.hasValue()) return [];
|
|
const withData = this.series
|
|
.value()
|
|
.flatMap(({ points }) => points.filter(hasPopulation).map((p) => p.period));
|
|
return chronological(withData);
|
|
});
|
|
|
|
protected readonly choice = computed<PeriodChoice>(() => {
|
|
const type = this.type();
|
|
const initial = type === 'month' ? (HISTORY[this.domain()]?.months ?? 0) : 0;
|
|
return (
|
|
this.choices()[type] ?? {
|
|
window: initialWindow(initial, this.available().length),
|
|
custom: [],
|
|
}
|
|
);
|
|
});
|
|
|
|
protected readonly options = computed(() =>
|
|
windowOptions(this.type(), this.available().length, this.choice()),
|
|
);
|
|
|
|
protected readonly selected = computed(() =>
|
|
this.picking() ? CUSTOM_PERIODS : this.choice().window,
|
|
);
|
|
|
|
protected choose(window: number): void {
|
|
if (window === CUSTOM_PERIODS) {
|
|
void this.pick();
|
|
return;
|
|
}
|
|
this.choices.update((all) => ({
|
|
...all,
|
|
[this.type()]: { ...this.choice(), window },
|
|
}));
|
|
}
|
|
|
|
protected async pick(): Promise<void> {
|
|
const type = this.type();
|
|
const periods = this.available();
|
|
const data: PeriodsDialogData = {
|
|
type,
|
|
periods,
|
|
chosen: visiblePeriods(periods, this.choice()),
|
|
};
|
|
this.picking.set(true);
|
|
const ref = this.dialog.open<PeriodsDialog, PeriodsDialogData, string[]>(PeriodsDialog, {
|
|
data,
|
|
width: '640px',
|
|
maxWidth: '92vw',
|
|
maxHeight: '85vh',
|
|
});
|
|
const custom = await firstValueFrom(ref.afterClosed());
|
|
this.picking.set(false);
|
|
if (custom?.length) {
|
|
this.choices.update((all) => ({ ...all, [type]: { window: CUSTOM_PERIODS, custom } }));
|
|
}
|
|
}
|
|
|
|
protected readonly drawn = computed<Drawn[]>(() => {
|
|
const history = HISTORY[this.domain()];
|
|
const xTitle = this.type() === 'month' ? 'Mes y año' : 'Semestre';
|
|
const visible = new Set(visiblePeriods(this.available(), this.choice()));
|
|
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 shown = points.filter((p) => visible.has(p.period));
|
|
if (!history || !shown.some(hasPopulation)) return { panel, heading };
|
|
const series = shown.map((p) => ({ label: periodLabel(p.period), r: p.r }));
|
|
return { panel, heading, figure: historyFigure(panel, series, history.line, xTitle) };
|
|
});
|
|
});
|
|
}
|