import { useState, useEffect } from "react"; import { get, put } from "../../api/client"; import type { Item } from "../../api/types"; interface EditItemPaneProps { partNumber: string; onSaved: () => void; onCancel: () => void; } export function EditItemPane({ partNumber, onSaved, onCancel, }: EditItemPaneProps) { const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); const [error, setError] = useState(null); const [pn, setPN] = useState(""); const [itemType, setItemType] = useState(""); const [description, setDescription] = useState(""); const [sourcingType, setSourcingType] = useState(""); const [longDescription, setLongDescription] = useState(""); useEffect(() => { setLoading(true); get(`/api/items/${encodeURIComponent(partNumber)}`) .then((item) => { setPN(item.part_number); setItemType(item.item_type); setDescription(item.description); setSourcingType(item.sourcing_type ?? ""); setLongDescription(item.long_description ?? ""); }) .catch(() => setError("Failed to load item")) .finally(() => setLoading(false)); }, [partNumber]); const handleSave = async () => { setSaving(true); setError(null); try { await put(`/api/items/${encodeURIComponent(partNumber)}`, { part_number: pn !== partNumber ? pn : undefined, item_type: itemType || undefined, description: description || undefined, sourcing_type: sourcingType || undefined, long_description: longDescription || undefined, }); onSaved(); } catch (e) { setError(e instanceof Error ? e.message : "Failed to save item"); } finally { setSaving(false); } }; if (loading) return (
Loading...
); return (
Edit {partNumber}
{error && (
{error}
)} setPN(e.target.value)} style={inputStyle} /> setDescription(e.target.value)} style={inputStyle} />