160 lines
6.0 KiB
TypeScript
160 lines
6.0 KiB
TypeScript
import { useState } from 'react';
|
|
import { sumFinancialAmounts } from '@/lib/financial-calculators';
|
|
type Row = { label: string; amount: string };
|
|
const initialRows = (): Row[] =>
|
|
[
|
|
'Instalações',
|
|
'Equipamentos',
|
|
'Preparação e arranque',
|
|
'Fundo de maneio inicial',
|
|
].map((label) => ({ label, amount: '' }));
|
|
export function restoreInitialBudget(snapshot: string): Row[] {
|
|
try {
|
|
const parsed = JSON.parse(snapshot);
|
|
if (
|
|
parsed.version === 1 &&
|
|
Array.isArray(parsed.rows) &&
|
|
parsed.rows.length <= 30 &&
|
|
parsed.rows.every(
|
|
(row: Row) =>
|
|
typeof row?.label === 'string' &&
|
|
row.label.length <= 120 &&
|
|
typeof row.amount === 'string' &&
|
|
row.amount.length <= 40,
|
|
)
|
|
)
|
|
return parsed.rows;
|
|
} catch {
|
|
/* Preserve the saved original; no invented amounts. */
|
|
}
|
|
return initialRows();
|
|
}
|
|
export default function InvestmentInitialBudget({
|
|
snapshot,
|
|
currency,
|
|
onChange,
|
|
}: {
|
|
snapshot: string;
|
|
currency: string;
|
|
onChange: (key: string, value: string) => void;
|
|
}) {
|
|
const [rows, setRows] = useState(() => restoreInitialBudget(snapshot));
|
|
const [message, setMessage] = useState('');
|
|
const save = (next: Row[]) => {
|
|
setRows(next);
|
|
setMessage('');
|
|
onChange(
|
|
'initial_budget_snapshot',
|
|
JSON.stringify({ version: 1, rows: next }),
|
|
);
|
|
};
|
|
return (
|
|
<section
|
|
className="space-y-3 rounded border p-4"
|
|
aria-label="Construir orçamento inicial"
|
|
>
|
|
<h3 className="font-semibold">
|
|
Ajuda para construir o investimento inicial
|
|
</h3>
|
|
<p>
|
|
Liste apenas os custos iniciais aplicáveis. O fundo de maneio
|
|
cobre o arranque; não repita aqui custos já incluídos noutra
|
|
parcela. Um valor desconhecido fica em branco, não é zero.
|
|
</p>
|
|
{rows.map((row, index) => (
|
|
<div key={index} className="grid gap-2 sm:grid-cols-2">
|
|
<label>
|
|
Parcela
|
|
<input
|
|
className="block w-full rounded border p-2"
|
|
maxLength={120}
|
|
value={row.label}
|
|
onChange={(e) =>
|
|
save(
|
|
rows.map((item, i) =>
|
|
i === index
|
|
? { ...item, label: e.target.value }
|
|
: item,
|
|
),
|
|
)
|
|
}
|
|
/>
|
|
</label>
|
|
<label>
|
|
Montante {currency}
|
|
<input
|
|
className="block w-full rounded border p-2"
|
|
type="number"
|
|
min="0"
|
|
step="0.01"
|
|
value={row.amount}
|
|
onChange={(e) =>
|
|
save(
|
|
rows.map((item, i) =>
|
|
i === index
|
|
? {
|
|
...item,
|
|
amount: e.target.value,
|
|
}
|
|
: item,
|
|
),
|
|
)
|
|
}
|
|
/>
|
|
</label>
|
|
<button
|
|
type="button"
|
|
className="text-left underline"
|
|
onClick={() => save(rows.filter((_, i) => i !== index))}
|
|
>
|
|
Retirar parcela não aplicável
|
|
</button>
|
|
</div>
|
|
))}
|
|
<button
|
|
type="button"
|
|
className="mr-3 rounded border px-3 py-2"
|
|
disabled={rows.length >= 30}
|
|
onClick={() => save([...rows, { label: '', amount: '' }])}
|
|
>
|
|
Acrescentar parcela
|
|
</button>
|
|
<button
|
|
type="button"
|
|
className="rounded border px-3 py-2"
|
|
onClick={() => {
|
|
try {
|
|
if (
|
|
!currency ||
|
|
!rows.length ||
|
|
rows.some(
|
|
(row) =>
|
|
!row.label.trim() || !row.amount.trim(),
|
|
)
|
|
)
|
|
throw new Error(
|
|
'Indique a moeda e as parcelas conhecidas; complete os montantes em falta antes de usar a soma.',
|
|
);
|
|
const total = sumFinancialAmounts(
|
|
rows.map((row) => row.amount),
|
|
);
|
|
onChange('total_investment', total);
|
|
setMessage(
|
|
`Soma adoptada por sua escolha: ${total} ${currency}. O intervalo orçamental original permanece separado.`,
|
|
);
|
|
} catch (error) {
|
|
setMessage(
|
|
error instanceof Error
|
|
? error.message
|
|
: 'Confira as parcelas.',
|
|
);
|
|
}
|
|
}}
|
|
>
|
|
Usar a soma como investimento deste cenário
|
|
</button>
|
|
{message && <p role="status">{message}</p>}
|
|
</section>
|
|
);
|
|
}
|