add: dashboard with server actions

This commit is contained in:
Elie Baier 2023-12-15 01:32:27 +01:00
parent 5b8038126b
commit 564edb70f8
3 changed files with 59 additions and 0 deletions

12
actions/mangeTest.ts Normal file
View File

@ -0,0 +1,12 @@
"use server";
import prisma from "@/lib/prisma";
export async function setTestActive(id: number, active: boolean) {
const users = await prisma.users.findFirst();
console.log(users);
return true;
}

View File

@ -0,0 +1,22 @@
'use client';
import { setTestActive } from '@/actions/mangeTest';
import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger } from '@/components/ui/alert-dialog';
export default function ActiveCard() {
return (
<AlertDialog>
<AlertDialogTrigger>Open</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Etes-vous sur de vouloir terminer ce test?</AlertDialogTitle>
<AlertDialogDescription>Les votations ne seront plus ouverte pour ce test. Vous pouvez cependant le réactiver dans le dashboard.</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<AlertDialogAction onClick={() => setTestActive(1, true)}>Continue</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
);
}

25
app/dashboard/page.tsx Normal file
View File

@ -0,0 +1,25 @@
import prisma from '@/lib/prisma';
import ActiveCard from './ActiveCard';
export default async function Dashboard() {
const tests = await prisma.test.findMany({ select: { isActive: true, isPassed: true, id: true, testOf: { select: { id: true, firstName: true, lastName: true, isTeacher: true } } } });
const activeTests = tests.filter((test) => test.isActive);
const passedTests = tests.filter((test) => test.isPassed);
return (
<div>
<h1>Dashboard</h1>
<div className="border-2 border-white rounded-2xl bg-red-500 p-2">
<h2>Test(s) Actif(s) :</h2>
<ul>
{activeTests.map((test) => (
<li key={test.id}>
{test.testOf.firstName + ' ' + test.testOf.lastName} <ActiveCard />
</li>
))}
</ul>
</div>
</div>
);
}