38 lines
1.5 KiB
TypeScript
38 lines
1.5 KiB
TypeScript
import { notFound, redirect } from "next/navigation";
|
|
import { FormPage } from "@/components/FormPage";
|
|
import { RecurringForm } from "@/components/recurrents/RecurringForm";
|
|
import { decimalFromCents } from "@/domain/money";
|
|
import { todayParis } from "@/lib/dates";
|
|
import { requireUser } from "@/server/auth";
|
|
import { prisma } from "@/server/db";
|
|
import { getFormData, getRecurringDefs } from "@/server/repo";
|
|
|
|
export default async function EditRecurringPage({ params }: { params: Promise<{ id: string }> }) {
|
|
const user = await requireUser();
|
|
const { id } = await params;
|
|
const today = todayParis();
|
|
const [data, defs, row] = await Promise.all([
|
|
getFormData(today),
|
|
getRecurringDefs(),
|
|
prisma.recurring.findUnique({ where: { id }, select: { loanId: true } }),
|
|
]);
|
|
const def = defs.find((d) => d.id === id);
|
|
if (!def || !row) notFound();
|
|
if (row.loanId) redirect(`/credits/${row.loanId}`);
|
|
const targets = defs.filter((d) => d.kind === "EXPENSE").map((d) => ({ id: d.id, label: d.label, split: d.split }));
|
|
return (
|
|
<FormPage title="Modifier le récurrent" back="/recurrents">
|
|
<p className="mb-4 rounded-[10px] bg-accent-soft px-3 py-2 text-[13px] text-accent">
|
|
Les changements s'appliquent aux prochaines échéances. Les opérations déjà passées ne bougent pas.
|
|
</p>
|
|
<RecurringForm
|
|
{...data}
|
|
targets={targets}
|
|
currentUserId={user.id}
|
|
today={today}
|
|
initial={{ ...def, amount: decimalFromCents(def.amount).replace(".", ",") }}
|
|
/>
|
|
</FormPage>
|
|
);
|
|
}
|