feat(indicadores): implementar filtros hibridos y memoria UX-04

This commit is contained in:
Angel Capetillo Zavala
2026-10-13 00:29:46 -06:00
parent ae5e4c5db5
commit ea91b31b17
14 changed files with 803 additions and 93 deletions
@@ -6,14 +6,15 @@ import { FormsModule } from '@angular/forms';
import { MatButtonModule } from '@angular/material/button';
import { MatCardModule } from '@angular/material/card';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { MatSelectModule } from '@angular/material/select';
import { MatProgressBarModule } from '@angular/material/progress-bar';
import { catchError, map, of, startWith, Subject, switchMap } from 'rxjs';
import { Dominio, DOMINIOS, isDominio, TipoPeriodo, FigureSelection, Indicador, parsePeriodos, Tema } from './indicadores.models';
import { catchError, defer, map, of, startWith, Subject, switchMap } from 'rxjs';
import { Dominio, DOMINIOS, isDominio, FigureSelection, Indicador, Tema } from './indicadores.models';
import { IndicadoresService } from './indicadores.service';
import { presentationFigure } from './indicadores.presentation';
import { FigureState, IndicadorFigureCard } from './indicador-figure-card';
import { FilterState, IndicadoresMemory, validDraft } from './indicadores-memory';
import { IndicadorPeriodFilters } from './indicador-period-filters';
export function httpMessage(error: unknown, catalog = false): string {
if (!(error instanceof HttpErrorResponse)) return 'La respuesta recibida no tiene el formato esperado. Intenta nuevamente.';
@@ -25,18 +26,18 @@ export function httpMessage(error: unknown, catalog = false): string {
}
@Component({
selector: 'app-indicadores-page',
imports: [RouterLink, FormsModule, MatButtonModule, MatCardModule, MatFormFieldModule, MatInputModule, MatSelectModule, MatProgressBarModule, IndicadorFigureCard],
imports: [RouterLink, FormsModule, MatButtonModule, MatCardModule, MatFormFieldModule, MatSelectModule, MatProgressBarModule, IndicadorFigureCard, IndicadorPeriodFilters],
templateUrl: './indicadores-page.html',
styleUrl: './indicadores-page.scss',
})
export class IndicadoresPage {
private readonly service = inject(IndicadoresService);
private readonly memory = inject(IndicadoresMemory);
private readonly destroyRef = inject(DestroyRef);
private readonly route = inject(ActivatedRoute, { optional: true });
readonly dominios = DOMINIOS;
dominio: Dominio = 'tea';
readonly domainLabels: Record<Dominio, string> = { tea: 'TEA', cacu: 'CaCu', cama: 'CaMa' };
tipoPeriodo: TipoPeriodo = 'semestre';
private readonly catalogRequests = new Subject<void>();
private readonly figureRequests = new Subject<FigureSelection | null>();
readonly catalog = signal<Indicador[]>([]);
@@ -45,19 +46,22 @@ export class IndicadoresPage {
readonly result = signal<FigureState>({ status: 'idle' });
indicadorId = '';
figuraId = '';
periodMode: 'todos' | 'manual' = 'todos';
periodText = '';
periodError = '';
tema: Tema = 'claro';
filters?: FilterState;
get selected(): Indicador | undefined { return this.catalog().find(item => item.id === this.indicadorId); }
constructor() {
const initialDomain: unknown = this.route?.snapshot.data['dominio'];
if (isDominio(initialDomain)) this.dominio = initialDomain;
this.figureRequests.pipe(switchMap(selection => selection
? this.service.figura(selection).pipe(
? defer(() => this.service.figura(selection)).pipe(
map(figure => ({ status: 'ready', figure, presentationFigure: presentationFigure(figure, selection) }) as FigureState),
catchError(error => of({ status: 'error', message: httpMessage(error) } as FigureState)),
catchError(error => of({
status: 'error',
message: httpMessage(error) + (error instanceof HttpErrorResponse && error.status === 404
&& (selection.periodos?.length ?? 0) > 1
? ' La consulta corresponde al conjunto de periodos; esta respuesta no identifica un periodo concreto.' : ''),
} as FigureState)),
startWith({ status: 'loading' } as FigureState))
: of({ status: 'idle' } as FigureState)), takeUntilDestroyed(this.destroyRef))
.subscribe(state => this.result.set(state));
@@ -66,7 +70,7 @@ export class IndicadoresPage {
this.catalogError.set('');
this.catalog.set([]);
this.indicadorId = this.figuraId = '';
this.resetPeriodos('semestre');
this.filters = undefined;
this.figureRequests.next(null);
return this.service.catalogo(this.dominio).pipe(map(items => ({ items, error: '' })),
catchError(error => of({ items: [] as Indicador[], error: httpMessage(error, true) })));
@@ -74,53 +78,68 @@ export class IndicadoresPage {
this.catalogLoading.set(false);
this.catalog.set(items);
this.catalogError.set(error);
if (items.length) this.selectIndicador(items[0].id);
const navigation = this.memory.navigation(this.dominio);
this.tema = navigation?.tema ?? 'claro';
if (items.length) this.selectIndicador(items.some(i => i.id === navigation?.indicador) ? navigation!.indicador : items[0].id);
});
}
filtersEdited() {
if (!this.filters || !this.selected) return;
this.memory.save(this.dominio, this.indicadorId, this.figuraId, this.filters);
this.memory.remember(this.dominio, this.indicadorId, this.figuraId, this.tema);
}
changeDominio(dominio: Dominio) {
if (!isDominio(dominio) || dominio === this.dominio) return;
this.filtersEdited();
this.dominio = dominio;
this.catalogRequests.next();
}
private resetPeriodos(tipo: TipoPeriodo) {
this.tipoPeriodo = tipo;
this.periodText = this.periodError = '';
this.periodMode = 'todos';
}
changeTipoPeriodo(tipo: TipoPeriodo) {
if (!this.selected?.tipos_periodo.includes(tipo)) return;
this.resetPeriodos(tipo);
if (tipo !== this.selected.tipos_periodo[0]) this.periodMode = 'manual';
this.requestFigure();
}
get periodLabel(): string { return this.tipoPeriodo === 'mes' ? 'Meses (AAAA-MM)' : 'Semestres (AAAA-S1,AAAA-S2)'; }
retryCatalog() { this.catalogRequests.next(); }
retryCatalog() { this.filtersEdited(); this.catalogRequests.next(); }
selectIndicador(id: string) {
if (!this.catalog().some(i => i.id === id)) return;
this.filtersEdited();
this.indicadorId = id;
this.figuraId = this.selected?.figuras[0] ?? '';
if (!this.selected?.tipos_periodo.includes(this.tipoPeriodo)
|| (this.periodMode === 'todos' && this.tipoPeriodo !== this.selected?.tipos_periodo[0])) {
this.resetPeriodos(this.selected?.tipos_periodo[0] ?? 'semestre');
}
const remembered = this.memory.selectedFigure(this.dominio, id);
this.figuraId = this.selected!.figuras.includes(remembered ?? '') ? remembered! : this.selected!.figuras[0] ?? '';
this.restoreFilters();
}
selectFigura(id: string) {
if (!this.selected?.figuras.includes(id)) return;
this.filtersEdited();
this.figuraId = id;
this.restoreFilters();
}
private restoreFilters() {
this.filters = this.memory.restore(this.dominio, this.selected!, this.figuraId);
this.filtersEdited();
this.requestFigure();
}
selectFigura(id: string) { this.figuraId = id; this.requestFigure(); }
changePeriodMode(mode: 'todos' | 'manual') { this.periodMode = mode; this.requestFigure(); }
changePeriodText(text: string) { this.periodText = text; this.requestFigure(); }
changeTema(tema: Tema) { this.tema = tema; this.requestFigure(); }
applyFilters() {
if (!this.filters || !this.selected) return;
const draft = this.filters.editors[this.filters.tipo]!.draft;
if (!validDraft(draft, this.filters.tipo, this.selected.tipos_periodo)) return;
this.filters.applied = { tipo: this.filters.tipo, selection: structuredClone(draft) };
this.filtersEdited();
this.requestFigure();
}
changeTema(tema: Tema) {
if (tema !== 'claro' && tema !== 'oscuro') return;
this.tema = tema;
this.filtersEdited();
this.requestFigure();
}
// Navigation, retry and theme all use the last application, never the pending editor.
requestFigure() {
this.periodError = '';
const periodos = this.periodMode === 'manual' ? parsePeriodos(this.periodText, this.tipoPeriodo) : undefined;
if (periodos === null) this.periodError = this.tipoPeriodo === 'mes'
? 'Introduce meses AAAA-MM del 01 al 12 separados por comas.'
: 'Introduce semestres AAAA-S1 o AAAA-S2 separados por comas.';
if (this.periodMode === 'todos' && this.tipoPeriodo !== this.selected?.tipos_periodo[0]) {
this.periodError = 'Selecciona periodos explícitos para este tipo; el servicio usa solo el primer tipo publicado al omitir periodos.';
}
if (!this.selected || !this.selected.figuras.includes(this.figuraId) || !this.selected.tipos_periodo.includes(this.tipoPeriodo) || periodos === null || !!this.periodError) {
const applied = this.filters?.applied;
if (!this.selected?.figuras.includes(this.figuraId) || !applied
|| !validDraft(applied.selection, applied.tipo, this.selected.tipos_periodo)) {
this.figureRequests.next(null);
return;
}
this.figureRequests.next({ dominio: this.dominio, tipoPeriodo: this.tipoPeriodo, indicador: this.indicadorId, figura: this.figuraId, tema: this.tema, periodos });
this.figureRequests.next({
dominio: this.dominio, tipoPeriodo: applied.tipo, indicador: this.indicadorId,
figura: this.figuraId, tema: this.tema,
periodos: applied.selection.mode === 'explicit' ? [...applied.selection.periodos] : undefined,
});
}
}