import { update } from '@/actions/App/Http/Controllers/BudgetController'; import { AmountInput } from '@/components/ui/amount-input'; import { Button } from '@/components/ui/button'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; import { Budget, BUDGET_PERIOD_TYPES, BudgetPeriodType, getBudgetPeriodTypeLabel, getRolloverTypeLabel, ROLLOVER_TYPES, RolloverType, } from '@/types/budget'; import { __ } from '@/utils/i18n'; import { router } from '@inertiajs/react'; import { useEffect, useState } from 'react'; interface Props { budget: Budget; currentPeriod: { allocated_amount: number }; currencyCode?: string; open: boolean; onOpenChange: (open: boolean) => void; } export function EditBudgetDialog({ budget, currentPeriod, currencyCode = 'USD', open, onOpenChange, }: Props) { const [name, setName] = useState(budget.name); const [periodType, setPeriodType] = useState( budget.period_type as BudgetPeriodType, ); const [periodDuration, setPeriodDuration] = useState( budget.period_duration, ); const [periodStartDay, setPeriodStartDay] = useState( budget.period_start_day || 1, ); const [allocatedAmount, setAllocatedAmount] = useState( currentPeriod.allocated_amount, ); const [rolloverType, setRolloverType] = useState( budget.rollover_type as RolloverType, ); const [isSubmitting, setIsSubmitting] = useState(false); useEffect(() => { if (open && budget) { setName(budget.name); setPeriodType(budget.period_type as BudgetPeriodType); setPeriodDuration(budget.period_duration); setPeriodStartDay(budget.period_start_day || 1); setAllocatedAmount(currentPeriod.allocated_amount); setRolloverType(budget.rollover_type as RolloverType); } }, [open, budget, currentPeriod]); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); setIsSubmitting(true); router.patch( update({ budget: budget.id }).url, { name, period_type: periodType, period_duration: periodDuration, period_start_day: periodStartDay, allocated_amount: allocatedAmount, rollover_type: rolloverType, }, { onSuccess: () => { onOpenChange(false); }, onFinish: () => setIsSubmitting(false), }, ); }; return (
{__('Edit Budget')} {__( 'Update your budget settings. To change the allocated\n amount or tracking, use the budget page directly.', )}
setName(e.target.value)} placeholder={__('e.g., Monthly Budget')} required />
{periodType === 'custom' && (
setPeriodDuration( e.target.value ? parseInt(e.target.value) : null, ) } required={periodType === 'custom'} />
)}
setPeriodStartDay(parseInt(e.target.value)) } />

{periodType === 'monthly' ? 'Day of the month when the period starts (1-31)' : periodType === 'weekly' || periodType === 'biweekly' ? 'Day of week (0=Sunday, 6=Saturday)' : 'Starting day'}

{__( 'This will update the allocated amount for the\n current and future periods.', )}

{rolloverType === 'carry_over' ? 'Unused budget will carry over to the next period.' : 'Budget resets to zero at the start of each period.'}

); }