import "@dh-software/baukasten-config-ui";
|
import type { BaukastenConfigurationChangeDetail } from "@dh-software/baukasten-config-ui";
|
import type { ConfigurationEntry, ConfigurationReference } from "@dh-software/baukasten-types";
|
|
const picker = document.getElementById("picker") as HTMLElement & {
|
available: ConfigurationEntry[];
|
selected: ConfigurationReference[];
|
};
|
const customerInput = document.getElementById("customer") as HTMLInputElement;
|
const reloadButton = document.getElementById("reload") as HTMLButtonElement;
|
const loadButton = document.getElementById("load") as HTMLButtonElement;
|
const statusEl = document.getElementById("status") as HTMLElement;
|
const resultsEl = document.getElementById("results") as HTMLElement;
|
|
let selected: ConfigurationReference[] = [];
|
|
async function loadCatalog(): Promise<void> {
|
const customerNo = customerInput.value.trim();
|
statusEl.textContent = "Lade Katalog…";
|
const response = await fetch(`/api/catalog?customerNo=${encodeURIComponent(customerNo)}`);
|
const entries = (await response.json()) as ConfigurationEntry[];
|
picker.available = entries;
|
selected = [];
|
picker.selected = selected;
|
resultsEl.replaceChildren();
|
statusEl.textContent = `${entries.length} Konfiguration(en) verfügbar.`;
|
}
|
|
picker.addEventListener("configurations-change", (event) => {
|
selected = (event as CustomEvent<BaukastenConfigurationChangeDetail>).detail.selected;
|
});
|
|
async function loadSelected(): Promise<void> {
|
if (selected.length === 0) {
|
statusEl.textContent = "Nichts ausgewählt.";
|
resultsEl.replaceChildren();
|
return;
|
}
|
statusEl.textContent = "Lese Konfigurationen…";
|
const response = await fetch("/api/read", {
|
method: "POST",
|
headers: { "Content-Type": "application/json" },
|
body: JSON.stringify(selected),
|
});
|
if (!response.ok) {
|
const error = (await response.json().catch(() => ({}))) as { error?: string };
|
statusEl.textContent = `Fehler: ${error.error ?? response.status}`;
|
return;
|
}
|
const result = (await response.json()) as Array<{
|
reference: ConfigurationReference;
|
content: unknown;
|
}>;
|
resultsEl.replaceChildren(
|
...result.map((item, index) => {
|
const wrapper = document.createElement("div");
|
wrapper.className = "result";
|
const title = document.createElement("h4");
|
title.textContent = `${index + 1}. ${item.reference.customerNumber} / ${item.reference.key}`;
|
const pre = document.createElement("pre");
|
pre.textContent = JSON.stringify(item.content, null, 2);
|
wrapper.append(title, pre);
|
return wrapper;
|
}),
|
);
|
statusEl.textContent = `${result.length} Konfiguration(en) gelesen (Reihenfolge = Merge-Reihenfolge).`;
|
}
|
|
reloadButton.addEventListener("click", () => {
|
void loadCatalog();
|
});
|
loadButton.addEventListener("click", () => {
|
void loadSelected();
|
});
|
|
void loadCatalog();
|