feat: add CRUD forms with Server Actions for establishments, users, classes, students

This commit is contained in:
root
2026-06-06 20:08:17 +00:00
parent 0a73a70820
commit a1883080d3
26 changed files with 1206 additions and 16 deletions
@@ -0,0 +1,40 @@
'use server'
import { prisma } from "@/lib/prisma";
import { redirect } from "next/navigation";
import { z } from "zod";
const schema = z.object({
name: z.string().min(2, "Le nom doit contenir au moins 2 caractères"),
slug: z.string().regex(/^[a-z0-9-]+$/, "Le slug ne peut contenir que des minuscules, chiffres et tirets"),
});
export async function createEstablishment(formData: FormData) {
const name = formData.get("name") as string;
const slug = formData.get("slug") as string;
const parsed = schema.safeParse({ name, slug });
if (!parsed.success) {
const message = parsed.error.issues.map((e) => e.message).join(", ");
redirect(`/superadmin/establishments/new?error=${encodeURIComponent(message)}`);
}
try {
await prisma.establishment.create({
data: {
name: parsed.data.name,
slug: parsed.data.slug,
subscription: {
create: {
plan: "trial",
status: "active",
},
},
},
});
} catch {
redirect(`/superadmin/establishments/new?error=${encodeURIComponent("Ce slug existe déjà ou une erreur est survenue")}`);
}
redirect("/superadmin/establishments");
}
@@ -0,0 +1,55 @@
export const dynamic = "force-dynamic";
import Link from "next/link";
import { createEstablishment } from "./actions";
import { Card, CardHeader, CardTitle, CardContent } from "@/components/ui/card";
import { Input } from "@/components/ui/input";
import { Button } from "@/components/ui/button";
export default function NewEstablishmentPage({
searchParams,
}: {
searchParams?: { error?: string };
}) {
const error = searchParams?.error;
return (
<div className="space-y-6">
<div className="flex items-center justify-between">
<h1 className="text-3xl font-bold">Nouvel établissement</h1>
<Button variant="outline" asChild>
<Link href="/superadmin/establishments">Retour</Link>
</Button>
</div>
<Card>
<CardHeader>
<CardTitle>Créer un établissement</CardTitle>
</CardHeader>
<CardContent>
<form action={createEstablishment} className="space-y-4">
{error && (
<div className="rounded-md border border-destructive bg-destructive/10 p-3 text-sm text-destructive">
{error}
</div>
)}
<div className="space-y-2">
<label htmlFor="name" className="text-sm font-medium">
Nom
</label>
<Input id="name" name="name" placeholder="Nom de l'établissement" required />
</div>
<div className="space-y-2">
<label htmlFor="slug" className="text-sm font-medium">
Slug
</label>
<Input id="slug" name="slug" placeholder="mon-etablissement" required />
</div>
<div className="flex justify-end">
<Button type="submit">Créer</Button>
</div>
</form>
</CardContent>
</Card>
</div>
);
}