53 lines
2.0 KiB
TypeScript
53 lines
2.0 KiB
TypeScript
"use client";
|
|
|
|
import { useState, useTransition } from "react";
|
|
import { decimalFromCents } from "@/domain/money";
|
|
import type { OpKind } from "@/domain/types";
|
|
import { formatDateFr } from "@/lib/dates";
|
|
import { confirmOperationAction, skipOperationAction } from "@/server/actions/operations";
|
|
import { Button } from "../ui/Button";
|
|
import { Input } from "../ui/Field";
|
|
|
|
export function ToConfirmRow({
|
|
op,
|
|
}: {
|
|
op: { id: string; label: string; emoji: string; amount: number; date: string; kind: OpKind; who: string };
|
|
}) {
|
|
const [amount, setAmount] = useState(decimalFromCents(op.amount).replace(".", ","));
|
|
const [date, setDate] = useState(op.date);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [pending, start] = useTransition();
|
|
|
|
return (
|
|
<div className="flex flex-col gap-2 border-b border-line-soft py-3 last:border-0 md:flex-row md:items-center">
|
|
<div className="min-w-0 flex-1">
|
|
<b>
|
|
{op.emoji} {op.label}
|
|
</b>
|
|
<div className="text-[13px] text-muted">
|
|
prévu le {formatDateFr(op.date)} · {op.kind === "EXPENSE" ? "payé par" : "reçu par"} {op.who}
|
|
</div>
|
|
{error && <div className="text-[13px] text-neg">{error}</div>}
|
|
</div>
|
|
<div className="flex flex-wrap items-center gap-2">
|
|
<Input aria-label="Montant" inputMode="decimal" className="w-28" value={amount} onChange={(e) => setAmount(e.target.value)} />
|
|
<Input aria-label="Date" type="date" className="w-40" value={date} onChange={(e) => setDate(e.target.value)} />
|
|
<Button
|
|
disabled={pending}
|
|
onClick={() =>
|
|
start(async () => {
|
|
const r = await confirmOperationAction(op.id, amount, date);
|
|
setError(r.ok ? null : r.error);
|
|
})
|
|
}
|
|
>
|
|
Confirmer
|
|
</Button>
|
|
<Button variant="ghost" disabled={pending} onClick={() => start(async () => void (await skipOperationAction(op.id)))}>
|
|
Ignorer
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|