import { useEffect, useState } from "react"; import { api } from "../api"; import Icon from "../components/Icon"; export default function Locations() { const [locations, setLocations] = useState([]); const [name, setName] = useState(""); const [parentId, setParentId] = useState(""); const [error, setError] = useState(null); async function load() { try { setLocations(await api.listLocations()); } catch (err) { setError(err.message); } } useEffect(() => { load(); }, []); async function add(e) { e.preventDefault(); setError(null); try { await api.createLocation({ name, parent_id: parentId === "" ? null : Number(parentId) }); setName(""); setParentId(""); load(); } catch (err) { setError(err.message); } } async function remove(id) { if (!confirm("Lagerort löschen? Untergeordnete Orte werden dann übergeordnet.")) return; try { await api.deleteLocation(id); load(); } catch (err) { setError(err.message); } } const nameById = Object.fromEntries(locations.map((l) => [l.id, l.name])); const roots = locations.filter((l) => !l.parent_id || !nameById[l.parent_id]); const childrenOf = (pid) => locations.filter((l) => l.parent_id === pid); const ordered = []; for (const r of roots) { ordered.push({ ...r, depth: 0 }); for (const c of childrenOf(r.id)) ordered.push({ ...c, depth: 1 }); } return (

Lagerorte

Orte lassen sich verschachteln (z.B. Keller → Regal 2 → Fach A)
{error &&
{error}
}
); }