import { useEffect, useState, type FormEvent } from 'react' import { useNavigate } from 'react-router-dom' import { catalog, inventory } from '@/api/client' import type { Item, Location } from '@/types/api' import { PageHeader } from '@/components/PageHeader' import { ErrorBox } from '@/components/ErrorBox' export function AdjustStockPage() { const navigate = useNavigate() const [items, setItems] = useState([]) const [locations, setLocations] = useState([]) const [itemCode, setItemCode] = useState('') const [locationId, setLocationId] = useState('') const [quantity, setQuantity] = useState('') const [submitting, setSubmitting] = useState(false) const [error, setError] = useState(null) const [result, setResult] = useState(null) useEffect(() => { Promise.all([catalog.listItems(), inventory.listLocations()]).then(([i, l]) => { setItems(i) setLocations(l.filter((x) => x.active)) if (i.length > 0) setItemCode(i[0].code) if (l.length > 0) setLocationId(l[0].id) }) }, []) const onSubmit = async (e: FormEvent) => { e.preventDefault() setError(null) setResult(null) setSubmitting(true) try { const bal = await inventory.adjustBalance({ itemCode, locationId, quantity: Number(quantity), }) setResult( `Balance set: ${bal.itemCode} @ location = ${bal.quantity}`, ) } catch (err: unknown) { setError(err instanceof Error ? err : new Error(String(err))) } finally { setSubmitting(false) } } return (
navigate('/balances')}>← Balances} />
setQuantity(e.target.value)} className="mt-1 w-full rounded-md border border-slate-300 px-3 py-2 text-sm text-right" />
{error && } {result && (
{result}
)}
) }