Files
openmonetis/src/features/invoices/components/adjust-invoice-dialog.tsx
Felipe Coutinho 94bf93194f chore: ajustes de componentes, estilos, dependências e métricas do dashboard
- dashboard: melhorias em métricas, filtros de transações e overview de período
- transactions: colunas, tabela e página com novos campos e ajustes de exibição
- ui: card, table, navigation-menu, navbar, month-picker, logo-picker, theme-toggler
- calculator: ajustes de display, keypad e estado
- calendar: melhorias de grid e day-cell
- insights: atualização de constantes
- settings: pequenos ajustes
- pnpm-lock: atualização de dependências
- pdf.worker: atualização do worker

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-02 22:08:53 +00:00

128 lines
3.4 KiB
TypeScript

"use client";
import { useRouter } from "next/navigation";
import { useEffect, useState, useTransition } from "react";
import { toast } from "sonner";
import { adjustInvoiceAction } from "@/features/invoices/actions";
import { Button } from "@/shared/components/ui/button";
import { CurrencyInput } from "@/shared/components/ui/currency-input";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/shared/components/ui/dialog";
import { Label } from "@/shared/components/ui/label";
import { formatCurrency } from "@/shared/utils/currency";
type AdjustInvoiceDialogProps = {
trigger: React.ReactNode;
cardId: string;
period: string;
currentTotal: number;
};
export function AdjustInvoiceDialog({
trigger,
cardId,
period,
currentTotal,
}: AdjustInvoiceDialogProps) {
const router = useRouter();
const [open, setOpen] = useState(false);
const [isPending, startTransition] = useTransition();
const currentAbs = Math.abs(currentTotal);
const [amount, setAmount] = useState<string>(currentAbs.toFixed(2));
useEffect(() => {
if (open) {
setAmount(currentAbs.toFixed(2));
}
}, [open, currentAbs]);
const targetAmount = Number(amount);
const diff = Number.isFinite(targetAmount)
? Math.round((targetAmount - currentAbs) * 100) / 100
: 0;
const diffLabel =
diff > 0
? `Será criado um lançamento de despesa de ${formatCurrency(diff)}.`
: diff < 0
? `Será criado um lançamento de receita de ${formatCurrency(Math.abs(diff))}.`
: "Nenhum ajuste será criado — o valor já está correto.";
const handleSave = () => {
if (!Number.isFinite(targetAmount) || targetAmount < 0) {
toast.error("Informe um valor válido.");
return;
}
startTransition(async () => {
const result = await adjustInvoiceAction({
cardId,
period,
currentTotal,
targetAmount,
});
if (result.success) {
toast.success(result.message);
setOpen(false);
router.refresh();
return;
}
toast.error(result.error);
});
};
return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>{trigger}</DialogTrigger>
<DialogContent className="sm:max-w-md">
<DialogHeader>
<DialogTitle>Ajustar fatura</DialogTitle>
<DialogDescription>
Informe o valor real da fatura. A diferença em relação ao total
atual será lançada como um ajuste no período.
</DialogDescription>
</DialogHeader>
<div className="space-y-4">
<div className="rounded-md border bg-muted/30 px-3 py-2 text-sm">
<p className="text-muted-foreground">Total atual no sistema</p>
<p className="font-medium text-foreground">
{formatCurrency(currentAbs)}
</p>
</div>
<div className="space-y-2">
<Label htmlFor="adjust-target">Valor correto da fatura</Label>
<CurrencyInput
id="adjust-target"
value={amount}
onValueChange={setAmount}
autoFocus
/>
<p className="text-xs text-muted-foreground">{diffLabel}</p>
</div>
</div>
<DialogFooter>
<Button
type="button"
variant="outline"
onClick={() => setOpen(false)}
disabled={isPending}
>
Cancelar
</Button>
<Button type="button" onClick={handleSave} disabled={isPending}>
{isPending ? "Salvando..." : "Salvar"}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
}