97 lines
3.4 KiB
TypeScript
97 lines
3.4 KiB
TypeScript
import { DecimalPipe } from '@angular/common';
|
|
import { Component, OnInit, computed, inject, signal } from '@angular/core';
|
|
import { forkJoin, of } from 'rxjs';
|
|
import { catchError, map } from 'rxjs/operators';
|
|
import { MatButtonModule } from '@angular/material/button';
|
|
import { MAT_DIALOG_DATA, MatDialogModule } from '@angular/material/dialog';
|
|
import { MatIconModule } from '@angular/material/icon';
|
|
import { MatListModule } from '@angular/material/list';
|
|
import { MatPaginatorModule, PageEvent } from '@angular/material/paginator';
|
|
import { MatProgressBarModule } from '@angular/material/progress-bar';
|
|
import { MatTableModule } from '@angular/material/table';
|
|
import { DataPage, TargetMeta, apiErrorMessage } from '../../api/api.types';
|
|
import { DataApi } from '../../api/data.api';
|
|
|
|
export interface DatabaseExplorerData {
|
|
targets: TargetMeta[];
|
|
initial?: string;
|
|
}
|
|
|
|
/**
|
|
* Read-only view of what's stored in the destination database, table by
|
|
* table, paged server-side through the backend's Repository. The "Vista de
|
|
* datos" to confirm a load landed as expected.
|
|
*/
|
|
@Component({
|
|
selector: 'app-database-explorer-dialog',
|
|
imports: [DecimalPipe, MatButtonModule, MatDialogModule, MatIconModule, MatListModule, MatPaginatorModule, MatProgressBarModule, MatTableModule],
|
|
templateUrl: './database-explorer-dialog.html',
|
|
styleUrl: './database-explorer-dialog.scss',
|
|
})
|
|
export class DatabaseExplorerDialog implements OnInit {
|
|
readonly data = inject<DatabaseExplorerData>(MAT_DIALOG_DATA);
|
|
private readonly api = inject(DataApi);
|
|
|
|
readonly totals = signal<Map<string, number | null>>(new Map());
|
|
readonly selected = signal<TargetMeta | null>(null);
|
|
readonly page = signal<DataPage | null>(null);
|
|
readonly pageIndex = signal(0);
|
|
readonly pageSize = signal(25);
|
|
readonly loading = signal(false);
|
|
readonly error = signal<string | null>(null);
|
|
|
|
/** Table order from the backend (keys and FKs first), not the alphabetical order JSON objects arrive in. */
|
|
readonly allColumns = computed(() => this.page()?.columns ?? []);
|
|
|
|
ngOnInit(): void {
|
|
forkJoin(
|
|
this.data.targets.map((target) =>
|
|
this.api.page(target.key, 1, 0).pipe(
|
|
map((page) => [target.key, page.total] as const),
|
|
catchError(() => of([target.key, null] as const)),
|
|
),
|
|
),
|
|
).subscribe((entries) => this.totals.set(new Map(entries)));
|
|
const initial = this.data.targets.find((target) => target.key === this.data.initial) ?? this.data.targets[0];
|
|
if (initial) this.open(initial);
|
|
}
|
|
|
|
open(target: TargetMeta): void {
|
|
this.selected.set(target);
|
|
this.pageIndex.set(0);
|
|
this.fetch();
|
|
}
|
|
|
|
onPage(event: PageEvent): void {
|
|
this.pageIndex.set(event.pageIndex);
|
|
this.pageSize.set(event.pageSize);
|
|
this.fetch();
|
|
}
|
|
|
|
refresh(): void {
|
|
this.ngOnInit();
|
|
}
|
|
|
|
display(value: unknown): string {
|
|
if (value === null || value === undefined) return 'null';
|
|
return String(value);
|
|
}
|
|
|
|
private fetch(): void {
|
|
const target = this.selected();
|
|
if (!target) return;
|
|
this.loading.set(true);
|
|
this.error.set(null);
|
|
this.api.page(target.key, this.pageSize(), this.pageIndex() * this.pageSize()).subscribe({
|
|
next: (page) => {
|
|
this.loading.set(false);
|
|
this.page.set(page);
|
|
},
|
|
error: (error) => {
|
|
this.loading.set(false);
|
|
this.error.set(apiErrorMessage(error, 'No se pudo leer la tabla.'));
|
|
},
|
|
});
|
|
}
|
|
}
|