165 lines
5.1 KiB
TypeScript
165 lines
5.1 KiB
TypeScript
import { HttpErrorResponse } from '@angular/common/http';
|
|
import { Component, inject, input, output, signal } from '@angular/core';
|
|
import { FormsModule } from '@angular/forms';
|
|
import { MatButtonModule } from '@angular/material/button';
|
|
import { MatIconModule } from '@angular/material/icon';
|
|
import { Api } from '../api/api';
|
|
import { getConceptMap } from '../api/fn/ingest/get-concept-map';
|
|
import { saveConceptMap } from '../api/fn/ingest/save-concept-map';
|
|
|
|
/** One translation: a site's code onto a platform code of one target system. */
|
|
interface Row {
|
|
target: string;
|
|
code: string;
|
|
platform: string;
|
|
}
|
|
|
|
/** A site's codes in platform terms (a FHIR ConceptMap), edited as a table: a new
|
|
* laboratory spelling is one more row. The API refuses a code no platform field allows. */
|
|
@Component({
|
|
selector: 'app-concept-map-editor',
|
|
imports: [FormsModule, MatButtonModule, MatIconModule],
|
|
template: `
|
|
<h3>
|
|
Códigos del sitio · <span class="muted">{{ id() }}</span>
|
|
</h3>
|
|
<table class="plain">
|
|
<thead>
|
|
<tr>
|
|
<th>Sistema destino</th>
|
|
<th>Código del sitio</th>
|
|
<th>Código de la plataforma</th>
|
|
<th></th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
@for (row of rows(); track $index) {
|
|
<tr>
|
|
<td>
|
|
<select [(ngModel)]="row.target" [attr.aria-label]="'Sistema destino ' + $index">
|
|
@for (t of targets(); track t) {
|
|
<option [value]="t">{{ short(t) }}</option>
|
|
}
|
|
</select>
|
|
</td>
|
|
<td>
|
|
<input [(ngModel)]="row.code" [attr.aria-label]="'Código del sitio ' + $index" />
|
|
</td>
|
|
<td>
|
|
<input
|
|
[(ngModel)]="row.platform"
|
|
[attr.aria-label]="'Código de la plataforma ' + $index"
|
|
/>
|
|
</td>
|
|
<td>
|
|
<button mat-icon-button (click)="remove($index)" aria-label="Quitar fila">
|
|
<mat-icon>close</mat-icon>
|
|
</button>
|
|
</td>
|
|
</tr>
|
|
}
|
|
</tbody>
|
|
</table>
|
|
<div class="actions">
|
|
<button mat-stroked-button (click)="add()"><mat-icon>add</mat-icon> Fila</button>
|
|
<button mat-flat-button (click)="save()">Guardar códigos</button>
|
|
<span class="muted">{{ message() }}</span>
|
|
</div>
|
|
`,
|
|
styles: `
|
|
h3 {
|
|
font-size: 0.95rem;
|
|
font-weight: 500;
|
|
margin: 16px 0 8px;
|
|
}
|
|
input,
|
|
select {
|
|
width: 100%;
|
|
box-sizing: border-box;
|
|
font: inherit;
|
|
padding: 4px;
|
|
}
|
|
.actions {
|
|
display: flex;
|
|
flex-wrap: wrap;
|
|
align-items: center;
|
|
gap: 8px;
|
|
margin-top: 8px;
|
|
}
|
|
`,
|
|
})
|
|
export class ConceptMapEditor {
|
|
private readonly api = inject(Api);
|
|
readonly id = input.required<string>();
|
|
readonly saved = output<number>();
|
|
protected readonly rows = signal<Row[]>([]);
|
|
protected readonly targets = signal<string[]>([]);
|
|
protected readonly message = signal('');
|
|
private source = 'urn:isssteleon:site';
|
|
|
|
async ngOnInit(): Promise<void> {
|
|
try {
|
|
const doc = (await this.api.invoke(getConceptMap, { map_id: this.id() })) as Record<
|
|
string,
|
|
any
|
|
>;
|
|
const groups = (doc['group'] ?? []) as any[];
|
|
this.source = groups[0]?.source ?? this.source;
|
|
this.targets.set([...new Set(groups.map((g) => g.target))]);
|
|
this.rows.set(
|
|
groups.flatMap((g) =>
|
|
(g.element ?? []).map((e: any) => ({
|
|
target: g.target,
|
|
code: e.code,
|
|
platform: e.target?.[0]?.code ?? '',
|
|
})),
|
|
),
|
|
);
|
|
} catch (e) {
|
|
this.message.set(
|
|
e instanceof HttpErrorResponse && e.status === 404 ? 'Nuevo: agregue filas.' : String(e),
|
|
);
|
|
}
|
|
}
|
|
|
|
protected short(url: string): string {
|
|
return url.split('/').pop() ?? url;
|
|
}
|
|
|
|
protected add(): void {
|
|
this.rows.update((r) => [...r, { target: this.targets()[0] ?? '', code: '', platform: '' }]);
|
|
}
|
|
|
|
protected remove(i: number): void {
|
|
this.rows.update((r) => r.filter((_, j) => j !== i));
|
|
}
|
|
|
|
/** The rows as a ConceptMap: one group per target system. */
|
|
protected async save(): Promise<void> {
|
|
const groups = new Map<string, Row[]>();
|
|
for (const r of this.rows().filter((r) => r.target && r.code && r.platform)) {
|
|
groups.set(r.target, [...(groups.get(r.target) ?? []), r]);
|
|
}
|
|
const doc = {
|
|
resourceType: 'ConceptMap',
|
|
status: 'active',
|
|
group: [...groups].map(([target, rows]) => ({
|
|
source: this.source,
|
|
target,
|
|
element: rows.map((r) => ({
|
|
code: r.code,
|
|
target: [{ code: r.platform, equivalence: 'equivalent' }],
|
|
})),
|
|
})),
|
|
};
|
|
try {
|
|
const { version } = await this.api.invoke(saveConceptMap, { map_id: this.id(), body: doc });
|
|
this.message.set(`Versión ${version} guardada.`);
|
|
this.saved.emit(version);
|
|
} catch (e) {
|
|
const detail = e instanceof HttpErrorResponse ? e.error?.detail : e;
|
|
this.message.set(Array.isArray(detail) ? detail.join('; ') : String(detail));
|
|
}
|
|
}
|
|
}
|