68 lines
2.1 KiB
TypeScript
68 lines
2.1 KiB
TypeScript
import { prisma } from "@/lib/prisma";
|
|
import { getServerSession } from "next-auth/next";
|
|
import { authOptions } from "@/lib/auth-config";
|
|
import { redirect } from "next/navigation";
|
|
import { Card, CardHeader, CardTitle, CardContent } from "@/components/ui/card";
|
|
import { Input } from "@/components/ui/input";
|
|
import { Button } from "@/components/ui/button";
|
|
import { z } from "zod";
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
const schema = z.object({
|
|
name: z.string().min(2),
|
|
level: z.string().min(1),
|
|
});
|
|
|
|
async function createClass(formData: FormData) {
|
|
"use server";
|
|
const session = await getServerSession(authOptions);
|
|
if (!session?.user) redirect("/login");
|
|
if (!session.user.establishmentId) redirect("/dashboard");
|
|
|
|
const parsed = schema.safeParse(Object.fromEntries(formData));
|
|
if (!parsed.success) return;
|
|
|
|
await prisma.class.create({
|
|
data: {
|
|
name: parsed.data.name,
|
|
level: parsed.data.level,
|
|
establishment: { connect: { id: session.user.establishmentId } },
|
|
},
|
|
});
|
|
|
|
redirect("/dashboard/classes");
|
|
}
|
|
|
|
export default async function NewClassPage() {
|
|
const session = await getServerSession(authOptions);
|
|
if (!session?.user) redirect("/login");
|
|
if (!session.user.establishmentId) redirect("/dashboard");
|
|
|
|
return (
|
|
<div className="space-y-6 max-w-xl">
|
|
<h1 className="text-3xl font-bold">Nouvelle classe</h1>
|
|
<Card>
|
|
<CardHeader>
|
|
<CardTitle>Créer une classe</CardTitle>
|
|
</CardHeader>
|
|
<CardContent>
|
|
<form action={createClass} className="space-y-4">
|
|
<div className="space-y-2">
|
|
<label htmlFor="name" className="text-sm font-medium">Nom</label>
|
|
<Input id="name" name="name" required minLength={2} />
|
|
</div>
|
|
<div className="space-y-2">
|
|
<label htmlFor="level" className="text-sm font-medium">Niveau</label>
|
|
<Input id="level" name="level" required minLength={1} />
|
|
</div>
|
|
<div className="flex justify-end">
|
|
<Button type="submit">Créer</Button>
|
|
</div>
|
|
</form>
|
|
</CardContent>
|
|
</Card>
|
|
</div>
|
|
);
|
|
}
|