/* Copyright (C) 2024-2025 LibreWeddingPlanner contributors*/ 'use client'; import { AbstractApi } from '@/app/api/abstract-api'; import { Expense, ExpenseSerializer, PricingType, pricingTypes } from '@/app/lib/expense'; import { capitalize } from '@/app/lib/utils'; import { classNames } from '@/app/ui/components/button'; import { Dialog } from 'primereact/dialog'; import { Dropdown } from 'primereact/dropdown'; import { FloatLabel } from 'primereact/floatlabel'; import { InputText } from 'primereact/inputtext'; import { useState } from 'react'; export default function ExpenseFormDialog({ onCreate, onHide, expense, visible }: { onCreate?: () => void, onHide: () => void, expense?: Expense, visible: boolean, }) { const [name, setName] = useState(expense?.name || ''); const [amount, setAmount] = useState(expense?.amount || 0); const [pricingType, setPricingType] = useState(expense?.pricingType || 'fixed'); const api = new AbstractApi(); const serializer = new ExpenseSerializer(); function resetForm() { setName(''); setAmount(0); setPricingType('fixed'); } function submitGroup() { if (expense?.id !== undefined) { expense.name = name; expense.amount = amount; expense.pricingType = pricingType; api.update(serializer, expense, () => { resetForm(); onCreate && onCreate(); }); } else { api.create(serializer, new Expense(undefined, name, amount, pricingType), () => { resetForm(); onCreate && onCreate(); }); } } return ( <>
setName(e.target.value)} /> setAmount(parseFloat(e.target.value))} /> setPricingType(e.target.value)} options={ pricingTypes.map((type) => { return { label: capitalize(type), value: type }; }) } />
); }