Files
WebApp/src/app/features/indicadores/indicador-metadata.ts
T
Roberto Treviño Cervantes e6e6fb7210 preliminary release
2026-10-29 12:12:15 -06:00

71 lines
3.7 KiB
TypeScript

import { ChangeDetectorRef, Component, DestroyRef, inject, Input, OnChanges } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { catchError, of, Subject, switchMap } from 'rxjs';
import { Dominio, IndicadorMetadatos } from './indicadores.models';
import { IndicadoresService } from './indicadores.service';
@Component({
selector: 'app-indicador-metadata',
styleUrl: './indicador-metadata.scss',
template: `
@if (loading) { <p role="status">Cargando información metodológica…</p> }
@if (description; as text) {
<section aria-labelledby="metadata-description-title">
<h4 id="metadata-description-title">Descripción</h4>
<p class="metadata-copy">{{ text }}</p>
</section>
}
@if (fichaFields.length) {
<section aria-labelledby="metadata-ficha-title">
<h4 id="metadata-ficha-title">Ficha técnica</h4>
<div class="table-scroll">
<table class="ficha-table">
<tbody>@for (field of fichaFields; track field.key) {
<tr><th scope="row">{{ field.label }}</th><td class="metadata-copy">{{ field.value }}</td></tr>
}</tbody>
</table>
</div>
</section>
}
@if (references.length) {
<section aria-labelledby="metadata-references-title">
<h4 id="metadata-references-title">Referencias</h4>
<ul>@for (ref of references; track $index) { <li>{{ ref.etiqueta }}@if (hasText(ref.ref)) {, {{ ref.ref }} }</li> }</ul>
</section>
}
@if (populations.length) {
<section aria-labelledby="metadata-populations-title">
<h4 id="metadata-populations-title">Poblaciones del estudio</h4>
<ul>@for (population of populations; track $index) { <li>{{ population.descripcion }}</li> }</ul>
</section>
}
@if (!loading && !hasUsableMetadata) { <p>La información metodológica ampliada no está disponible para este indicador.</p> }
`,
})
export class IndicadorMetadata implements OnChanges {
@Input({ required: true }) dominio!: Dominio;
@Input({ required: true }) indicador!: string;
private readonly service = inject(IndicadoresService);
private readonly requests = new Subject<void>();
metadata?: IndicadorMetadatos;
loading = false;
private readonly changeDetector = inject(ChangeDetectorRef);
private readonly fichaLabels = [
['codigo', 'Código'], ['para', 'Para quién'], ['unidad', 'Unidad'],
['pregunta', 'Pregunta'], ['definicion', 'Definición'], ['nota', 'Nota'],
] as const;
hasText(value: unknown): value is string { return typeof value === 'string' && value.trim().length > 0; }
get description() { return this.hasText(this.metadata?.descripcion) ? this.metadata!.descripcion!.trim() : undefined; }
get fichaFields() {
const ficha = this.metadata?.ficha;
return ficha ? this.fichaLabels.flatMap(([key, label]) => this.hasText(ficha[key]) ? [{ key, label, value: ficha[key]!.trim() }] : []) : [];
}
get references() { return (this.metadata?.referencias ?? []).filter(ref => this.hasText(ref.etiqueta)); }
get populations() { return (this.metadata?.poblaciones ?? []).filter(population => this.hasText(population.descripcion)); }
get hasUsableMetadata() { return !!(this.description || this.fichaFields.length || this.references.length || this.populations.length); }
constructor() {
this.requests.pipe(switchMap(() => this.service.metadatos(this.dominio, this.indicador).pipe(catchError(() => of(undefined)))), takeUntilDestroyed(inject(DestroyRef))).subscribe(value => { this.metadata = value; this.loading = false; this.changeDetector.markForCheck(); });
}
ngOnChanges() { this.metadata = undefined; this.loading = true; this.requests.next(); }
}