import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import InputError from '@/components/InputError'; import { useForm } from '@inertiajs/react'; import { todayISO } from '@/lib/utils'; import { LoaderCircle } from 'lucide-react'; import { FormEventHandler, useEffect, useState } from 'react'; import ComponentTitle from '@/components/ui/ComponentTitle'; interface EntryFormData { date: string; quantity: string; [key: string]: string; } interface AddEntryFormProps { unit?: string; onSuccess?: () => void; onCancel?: () => void; } interface EntrySummary { total_quantity: number; } export default function AddEntryForm({ unit = 'units', onSuccess, onCancel }: AddEntryFormProps) { const { data, setData, post, processing, errors, reset } = useForm({ date: todayISO(), quantity: '', }); const [currentHoldings, setCurrentHoldings] = useState(null); useEffect(() => { const fetchSummary = async () => { try { const response = await fetch('/entries/summary'); if (response.ok) { const summary = await response.json(); setCurrentHoldings(summary); } } catch (error) { console.error('Failed to fetch entry summary:', error); } }; fetchSummary(); }, []); const submit: FormEventHandler = (e) => { e.preventDefault(); post(route('entries.store'), { onSuccess: () => { reset(); setData('date', todayISO()); if (onSuccess) onSuccess(); }, }); }; return (
ADD ENTRY {currentHoldings && currentHoldings.total_quantity > 0 && (

[CURRENT] {currentHoldings.total_quantity.toFixed(6)} {unit}

)}
setData('date', e.target.value)} max={todayISO()} className="bg-black border-red-500 text-red-400 focus:border-red-300 font-mono text-sm rounded-none border-2 focus:ring-0 focus:outline-none transition-all glow-red" />
setData('quantity', e.target.value)} className="bg-black border-red-500 text-red-400 focus:border-red-300 font-mono text-sm rounded-none border-2 focus:ring-0 focus:outline-none placeholder:text-red-400/40 transition-all glow-red" />
{onCancel && ( )}
); }