F3: Trazabilidad chooses which periods to show
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.
This commit is contained in:
@@ -1,12 +1,33 @@
|
||||
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 } from './measures';
|
||||
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;
|
||||
@@ -16,18 +37,38 @@ interface Drawn {
|
||||
|
||||
@Component({
|
||||
selector: 'app-tab-trazabilidad',
|
||||
imports: [Loading, MatButtonToggleModule, Plot],
|
||||
imports: [
|
||||
Loading,
|
||||
MatButtonModule,
|
||||
MatButtonToggleModule,
|
||||
MatFormFieldModule,
|
||||
MatSelectModule,
|
||||
Plot,
|
||||
],
|
||||
template: `
|
||||
<section class="card" 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>
|
||||
<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…" />
|
||||
@@ -68,6 +109,12 @@ interface Drawn {
|
||||
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;
|
||||
@@ -91,7 +138,11 @@ 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() }),
|
||||
@@ -114,22 +165,77 @@ export class TabTrazabilidad {
|
||||
},
|
||||
});
|
||||
|
||||
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 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 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) };
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user