import { useEffect, useState } from 'react' import { Link } from 'react-router-dom' import { inventory } from '@/api/client' import type { Location, StockBalance } from '@/types/api' import { PageHeader } from '@/components/PageHeader' import { Loading } from '@/components/Loading' import { ErrorBox } from '@/components/ErrorBox' import { DataTable, type Column } from '@/components/DataTable' interface Row extends Record { id: string itemCode: string locationCode: string quantity: string | number } export function BalancesPage() { const [rows, setRows] = useState([]) const [error, setError] = useState(null) const [loading, setLoading] = useState(true) useEffect(() => { Promise.all([inventory.listBalances(), inventory.listLocations()]) .then(([balances, locs]: [StockBalance[], Location[]]) => { const byId = new Map(locs.map((l) => [l.id, l.code])) setRows( balances.map((b) => ({ id: b.id, itemCode: b.itemCode, locationCode: byId.get(b.locationId) ?? b.locationId, quantity: b.quantity, })), ) }) .catch((e: unknown) => setError(e instanceof Error ? e : new Error(String(e)))) .finally(() => setLoading(false)) }, []) const columns: Column[] = [ { header: 'Item', key: 'itemCode', render: (r) => {r.itemCode} }, { header: 'Location', key: 'locationCode', render: (r) => {r.locationCode}, }, { header: 'Quantity', key: 'quantity', render: (r) => {String(r.quantity)}, }, ] return (
Adjust Stock} /> {loading && } {error && } {!loading && !error && }
) }