import { useEffect, useState } from 'react' import { Link } from 'react-router-dom' import { Shell } from '../layouts/Shell' import { apiClient } from '../services/apiClient' import { formatCents } from '../utils/format' interface InventoryItem { product_id: string seller_id: string name: string batch: string expires_at: string quantity: number price_cents: number } export function InventoryPage() { const [inventory, setInventory] = useState([]) const [loading, setLoading] = useState(true) const [error, setError] = useState(null) const [expiringDays, setExpiringDays] = useState('') useEffect(() => { loadInventory() }, [expiringDays]) const loadInventory = async () => { try { setLoading(true) const params = expiringDays ? `?expires_in_days=${expiringDays}` : '' const response = await apiClient.get<{ items: InventoryItem[]; total: number }>(`/v1/inventory${params}`) setInventory(response?.items || []) setError(null) } catch (err) { setError('Erro ao carregar estoque') console.error(err) } finally { setLoading(false) } } const adjustStock = async (productId: string, delta: number, reason: string) => { try { await apiClient.post('/v1/inventory/adjust', { product_id: productId, delta, reason }) await loadInventory() } catch (err) { console.error('Erro ao ajustar estoque:', err) } } const isExpiringSoon = (date: string) => { const expires = new Date(date) const now = new Date() const diffDays = Math.ceil((expires.getTime() - now.getTime()) / (1000 * 60 * 60 * 24)) return diffDays <= 30 } return (

Meus Produtos

Controle de inventário e validades

+ Cadastrar Produto
{loading && (
)} {error && (
{error}
)} {!loading && inventory.length === 0 && (
Nenhum item no estoque
)}
{inventory.map((item) => ( ))}
Produto Lote Validade Quantidade Preço Ações
{item.name} {item.batch} {new Date(item.expires_at).toLocaleDateString('pt-BR')} {isExpiringSoon(item.expires_at) && ⚠️} {item.quantity} {formatCents(item.price_cents)}
) }