feat: add CRUD forms with Server Actions for establishments, users, classes, students
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
'use server'
|
||||
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { redirect } from "next/navigation";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { z } from "zod";
|
||||
|
||||
const updateSchema = z.object({
|
||||
plan: z.enum(["trial", "starter", "standard", "premium"]),
|
||||
status: z.enum(["active", "suspended", "expired"]),
|
||||
});
|
||||
|
||||
export async function updateSubscription(establishmentId: string, formData: FormData) {
|
||||
const plan = formData.get("plan") as string;
|
||||
const status = formData.get("status") as string;
|
||||
|
||||
const parsed = updateSchema.safeParse({ plan, status });
|
||||
if (!parsed.success) {
|
||||
throw new Error("Données invalides");
|
||||
}
|
||||
|
||||
await prisma.subscription.update({
|
||||
where: { establishmentId },
|
||||
data: {
|
||||
plan: parsed.data.plan,
|
||||
status: parsed.data.status,
|
||||
},
|
||||
});
|
||||
|
||||
revalidatePath(`/superadmin/establishments/${establishmentId}`);
|
||||
}
|
||||
|
||||
export async function deleteEstablishment(establishmentId: string) {
|
||||
await prisma.establishment.delete({
|
||||
where: { id: establishmentId },
|
||||
});
|
||||
redirect("/superadmin/establishments");
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
'use client'
|
||||
|
||||
import { useState } from "react";
|
||||
import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog";
|
||||
import { Button } from "@/components/ui/button";
|
||||
|
||||
export function DeleteDialog({ deleteAction }: { deleteAction: () => void }) {
|
||||
const [open, setOpen] = useState(false);
|
||||
|
||||
return (
|
||||
<>
|
||||
<Button variant="destructive" onClick={() => setOpen(true)}>
|
||||
Supprimer
|
||||
</Button>
|
||||
<Dialog open={open} onOpenChange={setOpen}>
|
||||
<DialogContent>
|
||||
<DialogHeader>
|
||||
<DialogTitle>Confirmer la suppression</DialogTitle>
|
||||
</DialogHeader>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
Cette action est irréversible. L'établissement et toutes ses données associées seront supprimés.
|
||||
</p>
|
||||
<div className="flex justify-end gap-2 mt-4">
|
||||
<Button variant="outline" onClick={() => setOpen(false)}>
|
||||
Annuler
|
||||
</Button>
|
||||
<form action={deleteAction}>
|
||||
<Button variant="destructive" type="submit">
|
||||
Supprimer définitivement
|
||||
</Button>
|
||||
</form>
|
||||
</div>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import Link from "next/link";
|
||||
import { notFound } from "next/navigation";
|
||||
import { Card, CardHeader, CardTitle, CardContent } from "@/components/ui/card";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { Select } from "@/components/ui/select";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { updateSubscription, deleteEstablishment } from "./actions";
|
||||
import { DeleteDialog } from "./delete-dialog";
|
||||
|
||||
export default async function EstablishmentDetailPage({ params }: { params: { id: string } }) {
|
||||
const establishment = await prisma.establishment.findUnique({
|
||||
where: { id: params.id },
|
||||
include: {
|
||||
subscription: true,
|
||||
_count: { select: { users: true, classes: true } },
|
||||
},
|
||||
});
|
||||
|
||||
if (!establishment) notFound();
|
||||
|
||||
const boundDelete = deleteEstablishment.bind(null, establishment.id);
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div className="flex items-center justify-between">
|
||||
<h1 className="text-3xl font-bold">{establishment.name}</h1>
|
||||
<div className="flex items-center gap-2">
|
||||
<Button variant="outline" asChild>
|
||||
<Link href="/superadmin/establishments">Retour</Link>
|
||||
</Button>
|
||||
<DeleteDialog deleteAction={boundDelete} />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="grid gap-6 md:grid-cols-2">
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Informations</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-3">
|
||||
<div className="flex justify-between text-sm">
|
||||
<span className="text-muted-foreground">Slug</span>
|
||||
<span className="font-medium">{establishment.slug}</span>
|
||||
</div>
|
||||
<div className="flex justify-between text-sm">
|
||||
<span className="text-muted-foreground">Utilisateurs</span>
|
||||
<span className="font-medium">{establishment._count.users}</span>
|
||||
</div>
|
||||
<div className="flex justify-between text-sm">
|
||||
<span className="text-muted-foreground">Classes</span>
|
||||
<span className="font-medium">{establishment._count.classes}</span>
|
||||
</div>
|
||||
<div className="flex justify-between text-sm">
|
||||
<span className="text-muted-foreground">Créé le</span>
|
||||
<span className="font-medium">
|
||||
{new Date(establishment.createdAt).toLocaleDateString("fr-FR")}
|
||||
</span>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Abonnement</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<form action={updateSubscription.bind(null, establishment.id)} className="space-y-4">
|
||||
<div className="space-y-2">
|
||||
<label htmlFor="plan" className="text-sm font-medium">
|
||||
Plan
|
||||
</label>
|
||||
<Select
|
||||
id="plan"
|
||||
name="plan"
|
||||
defaultValue={establishment.subscription?.plan || "trial"}
|
||||
>
|
||||
<option value="trial">Trial</option>
|
||||
<option value="starter">Starter</option>
|
||||
<option value="standard">Standard</option>
|
||||
<option value="premium">Premium</option>
|
||||
</Select>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<label htmlFor="status" className="text-sm font-medium">
|
||||
Statut
|
||||
</label>
|
||||
<Select
|
||||
id="status"
|
||||
name="status"
|
||||
defaultValue={establishment.subscription?.status || "active"}
|
||||
>
|
||||
<option value="active">Active</option>
|
||||
<option value="suspended">Suspended</option>
|
||||
<option value="expired">Expired</option>
|
||||
</Select>
|
||||
</div>
|
||||
<div className="flex items-center justify-between">
|
||||
<Badge variant={establishment.subscription?.status === "active" ? "success" : "destructive"}>
|
||||
{establishment.subscription?.status || "-"}
|
||||
</Badge>
|
||||
<Button type="submit">Mettre à jour</Button>
|
||||
</div>
|
||||
</form>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -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>
|
||||
);
|
||||
}
|
||||
@@ -1,21 +1,34 @@
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import Link from "next/link";
|
||||
import { Table, TableHeader, TableBody, TableRow, TableHead, TableCell } from "@/components/ui/table";
|
||||
import { Card, CardHeader, CardTitle, CardContent } from "@/components/ui/card";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
import { Button } from "@/components/ui/button";
|
||||
|
||||
export default async function EstablishmentsPage() {
|
||||
const establishments = await prisma.establishment.findMany({
|
||||
include: { subscription: true, _count: { select: { users: true, classes: true } } },
|
||||
include: {
|
||||
subscription: true,
|
||||
_count: { select: { users: true, classes: true } },
|
||||
},
|
||||
orderBy: { createdAt: "desc" },
|
||||
});
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<h1 className="text-3xl font-bold">Établissements</h1>
|
||||
<div className="flex items-center justify-between">
|
||||
<h1 className="text-3xl font-bold">Établissements</h1>
|
||||
<Button asChild>
|
||||
<Link href="/superadmin/establishments/new">Ajouter</Link>
|
||||
</Button>
|
||||
</div>
|
||||
<Card>
|
||||
<CardContent className="pt-6">
|
||||
<CardHeader>
|
||||
<CardTitle>Liste des établissements</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<Table>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
@@ -25,28 +38,34 @@ export default async function EstablishmentsPage() {
|
||||
<TableHead>Statut</TableHead>
|
||||
<TableHead>Utilisateurs</TableHead>
|
||||
<TableHead>Classes</TableHead>
|
||||
<TableHead>Expiration</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{establishments.map((e) => (
|
||||
<TableRow key={e.id}>
|
||||
<TableCell className="font-medium">{e.name}</TableCell>
|
||||
<TableCell className="font-medium">
|
||||
<Link href={`/superadmin/establishments/${e.id}`} className="hover:underline">
|
||||
{e.name}
|
||||
</Link>
|
||||
</TableCell>
|
||||
<TableCell>{e.slug}</TableCell>
|
||||
<TableCell>
|
||||
<Badge variant="secondary">{e.subscription?.plan || "-"}</Badge>
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Badge variant={e.subscription?.status === "active" ? "success" : "destructive"}>{e.subscription?.status || "-"}</Badge>
|
||||
<Badge variant={e.subscription?.status === "active" ? "success" : "destructive"}>
|
||||
{e.subscription?.status || "-"}
|
||||
</Badge>
|
||||
</TableCell>
|
||||
<TableCell>{e._count.users}</TableCell>
|
||||
<TableCell>{e._count.classes}</TableCell>
|
||||
<TableCell>{e.subscription?.expiresAt ? new Date(e.subscription.expiresAt).toLocaleDateString("fr-FR") : "-"}</TableCell>
|
||||
</TableRow>
|
||||
))}
|
||||
{establishments.length === 0 && (
|
||||
<TableRow>
|
||||
<TableCell colSpan={7} className="text-center text-muted-foreground">Aucun établissement</TableCell>
|
||||
<TableCell colSpan={6} className="text-center text-muted-foreground">
|
||||
Aucun établissement
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
)}
|
||||
</TableBody>
|
||||
|
||||
Reference in New Issue
Block a user