Conectar dialog by kind of source; plain-language canvas; login page and guard; loading indicators as the old app; survival curves of her clinical scenarios in the CaMa record
This commit is contained in:
@@ -0,0 +1,349 @@
|
||||
import { NgTemplateOutlet } from '@angular/common';
|
||||
import { HttpClient, HttpErrorResponse, HttpEventType } from '@angular/common/http';
|
||||
import { Component, computed, inject, resource, signal } from '@angular/core';
|
||||
import { FormsModule } from '@angular/forms';
|
||||
import { MatButtonModule } from '@angular/material/button';
|
||||
import { MAT_DIALOG_DATA, MatDialogModule, MatDialogRef } from '@angular/material/dialog';
|
||||
import { MatExpansionModule } from '@angular/material/expansion';
|
||||
import { MatFormFieldModule } from '@angular/material/form-field';
|
||||
import { MatIconModule } from '@angular/material/icon';
|
||||
import { MatInputModule } from '@angular/material/input';
|
||||
import { MatProgressBarModule } from '@angular/material/progress-bar';
|
||||
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
|
||||
import { MatSelectModule } from '@angular/material/select';
|
||||
import { MatSlideToggleModule } from '@angular/material/slide-toggle';
|
||||
import { MatTableModule } from '@angular/material/table';
|
||||
import { Api } from '../api/api';
|
||||
import { ApiConfiguration } from '../api/api-configuration';
|
||||
import { browse } from '../api/fn/ingest/browse';
|
||||
import { oauthStart } from '../api/fn/ingest/oauth-start';
|
||||
import { sourceTypes } from '../api/fn/ingest/source-types';
|
||||
import { testSource } from '../api/fn/ingest/test-source';
|
||||
import type { Browsed } from '../api/models/browsed';
|
||||
import type { FieldSpec } from '../api/models/field-spec';
|
||||
import type { Guided } from '../api/models/guided';
|
||||
import type { SourceType } from '../api/models/source-type';
|
||||
import type { Tested } from '../api/models/tested';
|
||||
import type { Uploaded } from '../api/models/uploaded';
|
||||
|
||||
/** What the dialog is opened with: the source being edited, if any. */
|
||||
export interface SourceDialogData {
|
||||
pipeline?: string;
|
||||
node?: string;
|
||||
guided?: Guided | null;
|
||||
label?: string;
|
||||
}
|
||||
/** What it gives back: the dialog's answers (the node's dlt fields are made from them). */
|
||||
export interface SourceDialogResult {
|
||||
guided: Guided;
|
||||
label: string;
|
||||
}
|
||||
|
||||
/** The catalog's generic icon words, as Material Symbols. */
|
||||
const ICONS: Record<string, string> = {
|
||||
database: 'database',
|
||||
upload: 'upload_file',
|
||||
folder: 'folder_open',
|
||||
link: 'link',
|
||||
'folder-network': 'lan',
|
||||
server: 'dns',
|
||||
cloud: 'cloud',
|
||||
onedrive: 'cloud',
|
||||
sharepoint: 'groups',
|
||||
'google-drive': 'add_to_drive',
|
||||
web: 'api',
|
||||
fhir: 'local_hospital',
|
||||
};
|
||||
const MASK = '********';
|
||||
|
||||
/** "Nueva fuente": the operator picks what kind of source the data is in, answers only what
|
||||
* that kind needs (in plain words, with pickers and sign-in buttons), tests the connection,
|
||||
* picks a table if there are several, sees the first rows, and saves. No dlt, no JSON. */
|
||||
@Component({
|
||||
selector: 'app-source-dialog',
|
||||
imports: [
|
||||
FormsModule,
|
||||
NgTemplateOutlet,
|
||||
MatButtonModule,
|
||||
MatDialogModule,
|
||||
MatExpansionModule,
|
||||
MatFormFieldModule,
|
||||
MatIconModule,
|
||||
MatInputModule,
|
||||
MatProgressBarModule,
|
||||
MatProgressSpinnerModule,
|
||||
MatSelectModule,
|
||||
MatSlideToggleModule,
|
||||
MatTableModule,
|
||||
],
|
||||
templateUrl: './source-dialog.html',
|
||||
styleUrl: './source-dialog.scss',
|
||||
})
|
||||
export class SourceDialog {
|
||||
private readonly api = inject(Api);
|
||||
private readonly http = inject(HttpClient);
|
||||
private readonly root = inject(ApiConfiguration).rootUrl;
|
||||
private readonly ref = inject(MatDialogRef<SourceDialog, SourceDialogResult>);
|
||||
protected readonly data = inject<SourceDialogData>(MAT_DIALOG_DATA, { optional: true }) ?? {};
|
||||
|
||||
protected readonly catalog = resource({ loader: () => this.api.invoke(sourceTypes) });
|
||||
protected readonly groups = computed(() => {
|
||||
const c = this.catalog.value();
|
||||
if (!c) return [];
|
||||
return Object.entries(c.groups).map(([id, g]) => ({
|
||||
id,
|
||||
...g,
|
||||
types: Object.entries(c.types)
|
||||
.filter(([, t]) => t.group === id)
|
||||
.map(([tid, t]) => ({ id: tid, ...t })),
|
||||
}));
|
||||
});
|
||||
|
||||
protected readonly typeId = signal<string | undefined>(this.data.guided?.type);
|
||||
protected readonly type = computed<SourceType | undefined>(() => {
|
||||
const id = this.typeId();
|
||||
return id ? this.catalog.value()?.types[id] : undefined;
|
||||
});
|
||||
protected readonly answers = signal<Record<string, unknown>>({
|
||||
...(this.data.guided?.answers ?? {}),
|
||||
});
|
||||
protected readonly label = signal(this.data.label ?? '');
|
||||
protected readonly fields = computed(() => entries(this.type()).filter(([, f]) => !f.advanced));
|
||||
protected readonly more = computed(() => entries(this.type()).filter(([, f]) => f.advanced));
|
||||
protected readonly showPassword = signal<Record<string, boolean>>({});
|
||||
|
||||
protected readonly testing = signal(false);
|
||||
/** The tables the last listing found: kept while one is picked and its rows shown. */
|
||||
protected readonly tableList = signal<string[]>([]);
|
||||
protected readonly tested = signal<Tested | undefined>(undefined);
|
||||
protected readonly columns = computed(() => Object.keys(this.tested()?.rows?.[0] ?? {}));
|
||||
|
||||
protected readonly browsing = signal<{ field: string; at: Browsed } | undefined>(undefined);
|
||||
protected readonly uploading = signal<{ field: string; percent: number } | undefined>(undefined);
|
||||
protected readonly uploaded = signal<Record<string, string[]>>({});
|
||||
protected readonly problem = signal('');
|
||||
|
||||
icon(name: string): string {
|
||||
return ICONS[name] ?? 'input';
|
||||
}
|
||||
|
||||
protected pick(id: string): void {
|
||||
const t = this.catalog.value()!.types[id];
|
||||
this.typeId.set(id);
|
||||
this.answers.set(
|
||||
Object.fromEntries(
|
||||
Object.entries(t.fields)
|
||||
.filter(([, f]) => f.default !== null && f.default !== undefined)
|
||||
.map(([k, f]) => [k, f.default]),
|
||||
),
|
||||
);
|
||||
if (!this.label()) this.label.set(t.title);
|
||||
this.tested.set(undefined);
|
||||
this.problem.set('');
|
||||
}
|
||||
|
||||
protected back(): void {
|
||||
this.typeId.set(undefined);
|
||||
this.tested.set(undefined);
|
||||
this.problem.set('');
|
||||
}
|
||||
|
||||
protected value(name: string): unknown {
|
||||
return this.answers()[name];
|
||||
}
|
||||
protected text(name: string): string {
|
||||
const v = this.answers()[name];
|
||||
return v === null || v === undefined ? '' : String(v);
|
||||
}
|
||||
protected set(name: string, value: unknown): void {
|
||||
this.answers.update((a) => ({ ...a, [name]: value }));
|
||||
if (this.type()?.fields[name]?.type === 'table') return;
|
||||
this.tested.set(undefined); // another connection: test again
|
||||
this.tableList.set([]);
|
||||
}
|
||||
protected setNumber(name: string, value: string): void {
|
||||
this.set(name, value === '' ? null : Number(value));
|
||||
}
|
||||
protected togglePassword(name: string): void {
|
||||
this.showPassword.update((s) => ({ ...s, [name]: !s[name] }));
|
||||
}
|
||||
protected isMasked(name: string): boolean {
|
||||
return this.answers()[name] === MASK;
|
||||
}
|
||||
protected formats() {
|
||||
return this.catalog.value()?.formats ?? [];
|
||||
}
|
||||
protected account(name: string): string | undefined {
|
||||
const a = this.answers()[name] as { user?: string } | undefined;
|
||||
return a ? (a.user ?? 'cuenta conectada') : undefined;
|
||||
}
|
||||
|
||||
/** "Probar conexión": what the source holds, in plain words. */
|
||||
protected async test(): Promise<void> {
|
||||
const type = this.typeId();
|
||||
if (!type) return;
|
||||
this.testing.set(true);
|
||||
this.problem.set('');
|
||||
try {
|
||||
const found = await this.api.invoke(testSource, {
|
||||
limit: 20,
|
||||
body: {
|
||||
type,
|
||||
answers: this.cleaned(),
|
||||
pipeline: this.data.pipeline,
|
||||
node: this.data.node,
|
||||
},
|
||||
});
|
||||
this.tested.set(found);
|
||||
if (found.tables?.length) this.tableList.set(found.tables);
|
||||
} catch (e) {
|
||||
this.problem.set(message(e));
|
||||
} finally {
|
||||
this.testing.set(false);
|
||||
}
|
||||
}
|
||||
|
||||
/** The tables a database listed, for its table field. */
|
||||
protected tables(): string[] {
|
||||
return this.tableList();
|
||||
}
|
||||
|
||||
/** Server folders, under the roots the operator mounted. */
|
||||
protected async openFolder(field: string, path?: string | null): Promise<void> {
|
||||
try {
|
||||
const at = await this.api.invoke(browse, path ? { path } : {});
|
||||
this.browsing.set({ field, at });
|
||||
} catch (e) {
|
||||
this.problem.set(message(e));
|
||||
}
|
||||
}
|
||||
protected chooseFolder(): void {
|
||||
const b = this.browsing();
|
||||
if (b?.at.path) this.set(b.field, b.at.path);
|
||||
this.browsing.set(undefined);
|
||||
}
|
||||
protected chooseFile(path: string): void {
|
||||
const b = this.browsing();
|
||||
if (b) this.set(b.field, path);
|
||||
this.browsing.set(undefined);
|
||||
}
|
||||
/** Whether the field being browsed takes a file (else a folder). */
|
||||
protected readonly browsingFiles = computed(() => {
|
||||
const b = this.browsing();
|
||||
return !!b && this.type()?.fields[b.field]?.type === 'server_file';
|
||||
});
|
||||
|
||||
/** Files or a whole folder from this computer, sent with progress; a folder keeps its
|
||||
* subfolders. More files can be added to the same set. */
|
||||
protected upload(field: string, list: FileList | null): void {
|
||||
if (!list?.length) return;
|
||||
const body = new FormData();
|
||||
for (const file of Array.from(list)) {
|
||||
body.append('files', file, file.webkitRelativePath || file.name);
|
||||
}
|
||||
const folder = this.answers()[field];
|
||||
if (typeof folder === 'string' && folder) body.append('folder', folder);
|
||||
this.uploading.set({ field, percent: 0 });
|
||||
this.problem.set('');
|
||||
this.http
|
||||
.post<Uploaded>(`${this.root}/ingest/uploads`, body, {
|
||||
reportProgress: true,
|
||||
observe: 'events',
|
||||
})
|
||||
.subscribe({
|
||||
next: (event) => {
|
||||
if (event.type === HttpEventType.UploadProgress && event.total) {
|
||||
this.uploading.set({ field, percent: Math.round((100 * event.loaded) / event.total) });
|
||||
} else if (event.type === HttpEventType.Response && event.body) {
|
||||
this.set(field, event.body.bucket_url);
|
||||
this.uploaded.update((u) => ({
|
||||
...u,
|
||||
[field]: [...(u[field] ?? []), ...event.body!.files],
|
||||
}));
|
||||
}
|
||||
},
|
||||
error: (e) => {
|
||||
this.uploading.set(undefined);
|
||||
this.problem.set(message(e));
|
||||
},
|
||||
complete: () => this.uploading.set(undefined),
|
||||
});
|
||||
}
|
||||
protected dropped(field: string, event: DragEvent): void {
|
||||
event.preventDefault();
|
||||
this.upload(field, event.dataTransfer?.files ?? null);
|
||||
}
|
||||
|
||||
/** "Iniciar sesión con …": the provider's page in a window; it hands the account back. */
|
||||
protected async signIn(field: string, provider: string): Promise<void> {
|
||||
this.problem.set('');
|
||||
let started;
|
||||
try {
|
||||
started = await this.api.invoke(oauthStart, { provider, origin: location.origin });
|
||||
} catch (e) {
|
||||
this.problem.set(message(e));
|
||||
return;
|
||||
}
|
||||
const popup = window.open(started.url, 'isssteleon-oauth', 'width=520,height=680');
|
||||
const listen = (event: MessageEvent) => {
|
||||
const m = event.data as { type?: string; state?: string; account?: unknown; error?: string };
|
||||
if (event.origin !== location.origin || m?.type !== 'isssteleon-oauth') return;
|
||||
if (m.state !== started.state) return;
|
||||
window.removeEventListener('message', listen);
|
||||
if (m.error) this.problem.set(m.error);
|
||||
else this.set(field, m.account);
|
||||
popup?.close();
|
||||
};
|
||||
window.addEventListener('message', listen);
|
||||
}
|
||||
|
||||
protected cell(v: unknown): string {
|
||||
return typeof v === 'object' && v !== null ? JSON.stringify(v) : String(v ?? '');
|
||||
}
|
||||
|
||||
/** Answers without empty ones (a default takes their place on the server). */
|
||||
private cleaned(): Record<string, unknown> {
|
||||
return Object.fromEntries(
|
||||
Object.entries(this.answers()).filter(([, v]) => v !== '' && v !== null && v !== undefined),
|
||||
);
|
||||
}
|
||||
|
||||
protected readonly missing = computed(() =>
|
||||
entries(this.type())
|
||||
.filter(([k, f]) => f.required && f.type !== 'table' && isEmpty(this.answers()[k]))
|
||||
.filter(([, f]) => f.default === null || f.default === undefined)
|
||||
.map(([, f]) => f.label),
|
||||
);
|
||||
protected readonly ready = computed(
|
||||
() =>
|
||||
!this.missing().length &&
|
||||
entries(this.type())
|
||||
.filter(([, f]) => f.type === 'table' && f.required)
|
||||
.every(([k]) => !isEmpty(this.answers()[k])),
|
||||
);
|
||||
|
||||
protected save(): void {
|
||||
const type = this.typeId();
|
||||
if (!type || !this.ready()) return;
|
||||
this.ref.close({
|
||||
guided: { type, answers: this.cleaned() },
|
||||
label: this.label() || this.type()!.title,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function entries(t: SourceType | undefined): [string, FieldSpec][] {
|
||||
return Object.entries(t?.fields ?? {});
|
||||
}
|
||||
function isEmpty(v: unknown): boolean {
|
||||
return v === '' || v === null || v === undefined;
|
||||
}
|
||||
function message(e: unknown): string {
|
||||
if (e instanceof HttpErrorResponse) {
|
||||
const d = e.error?.detail;
|
||||
if (typeof d === 'string') return d;
|
||||
if (e.status === 0) return 'No hay conexión con la plataforma. Intente de nuevo.';
|
||||
return JSON.stringify(d ?? e.message);
|
||||
}
|
||||
return String(e);
|
||||
}
|
||||
Reference in New Issue
Block a user