import { ChangeDetectionStrategy, Component, computed, inject, input, resource } from '@angular/core'; import { apiErrorMessage } from '../../core/api'; import { SapDbApi } from '../../core/sl/sapdb-api'; import type { DetailColumn } from './grid.types'; import { RowsTable } from './rows-table'; /** Tabla de solo lectura con el resultado de una consulta registrada a la base de SAP (el stock de un lote por almacén). */ @Component({ selector: 'sbo-query-table', changeDetection: ChangeDetectionStrategy.OnPush, imports: [RowsTable], template: ` @if (result.error()) { } @else if (result.isLoading()) {

Consultando…

} @else { @if (result.value()?.truncated) {

Hay más filas de las que se muestran.

} } `, styles: ` :host { display: block; } .hint { margin: 6px 0; color: var(--quiet); } .err { margin: 6px 0; color: var(--err); } `, }) export class QueryTable { private readonly api = inject(SapDbApi); readonly query = input.required(); readonly params = input.required>(); readonly columns = input.required(); readonly hideEmptyBy = input(); readonly hideEmptyLabel = input('Ocultar las filas en 0'); /** La consulta y sus parámetros como texto: quien nos usa puede armar un objeto nuevo en cada ciclo y no por eso se vuelve a consultar. */ private readonly request = computed(() => JSON.stringify({ query: this.query(), params: this.params() })); protected readonly result = resource({ params: () => this.request(), loader: ({ params }) => { const r = JSON.parse(params) as { query: string; params: Record }; return this.api.run(r.query, r.params); }, }); protected readonly rows = computed(() => (this.result.hasValue() ? this.result.value().rows : [])); protected readonly error = computed(() => apiErrorMessage(this.result.error())); }