add: auto-refresh on the results pages

This commit is contained in:
Elie Baier 2023-12-18 16:17:29 +01:00
parent 8ca5133527
commit 25ec9c2e50
3 changed files with 81 additions and 49 deletions

33
actions/results.ts Normal file
View File

@ -0,0 +1,33 @@
"use server";
import prisma from "@/lib/prisma";
export interface Results {
name: string;
total: number;
}
export async function getResults(testId: number) {
const grades = await prisma.grade.findMany({ where: { testId: testId, user: { isTeacher: false } } });
const allGrades = ['1', '1.5', '2', '2.5', '3', '3.5', '4', '4.5', '5', '5.5', '6'];
let gradeOccurences = new Array(allGrades.length).fill(0);
const gradeList = grades.map((grade) => grade.grade);
for (let i = 0; i < gradeList.length; i++) {
gradeOccurences[allGrades.indexOf(gradeList[i].toString())]++;
}
let data: Results[] = [];
for (let i = 0; i < gradeOccurences.length; i++) {
data.push({
name: allGrades[i],
total: gradeOccurences[i],
});
}
return data;
}
export async function getTeacherResult(testId: number) {
return await prisma.grade.findFirst({ where: { testId: testId, user: { isTeacher: true } } });
}

View File

@ -1,35 +1,34 @@
'use server'; 'use client';
import { Chart } from '@components/custom/chart'; import { Chart } from '@components/custom/chart';
import logo from '@images/logo.svg'; import logo from '@images/logo.svg';
import Image from 'next/image'; import Image from 'next/image';
import Prisma from '@lib/prisma';
import ginger from '@images/ginger.png'; import ginger from '@images/ginger.png';
import { getResults, getTeacherResult, Results } from '@/actions/results';
import { useEffect, useState } from 'react';
interface data { export default function Page({ params }: { params: { id: string } }) {
name: string; const testId = parseInt(params.id);
total: number;
}
export default async function Page({ params }: { params: { id: string } }) { const [teacherGrade, setTeacherGrade] = useState<number>(0);
const teacherGrade = await Prisma.grade.findFirst({ where: { testId: parseInt(params.id), user: { isTeacher: true } } }); const [data, setData] = useState<Results[]>([]);
const grades = await Prisma.grade.findMany({ where: { testId: parseInt(params.id), user: { isTeacher: false } } }); function featchResults(testId: number) {
getTeacherResult(testId)
.then((res) => setTeacherGrade(res?.grade || 0))
.catch((err) => console.error(err));
const allGrades = ['1', '1.5', '2', '2.5', '3', '3.5', '4', '4.5', '5', '5.5', '6']; getResults(testId)
let gradeOccurences = new Array(allGrades.length).fill(0); .then((res) => setData(res))
const gradeList = grades.map((grade) => grade.grade); .catch((err) => console.error(err));
for (let i = 0; i < gradeList.length; i++) {
gradeOccurences[allGrades.indexOf(gradeList[i].toString())]++;
} }
let data: data[] = []; useEffect(() => {
for (let i = 0; i < gradeOccurences.length; i++) { featchResults(testId);
data.push({ }, [testId]);
name: allGrades[i],
total: gradeOccurences[i], setTimeout(() => {
}); featchResults(testId);
} }, 5000);
return ( return (
<div className={'p-4 md:p-12 w-full h-screen bg-black flex flex-col justify-between gap-y-4'}> <div className={'p-4 md:p-12 w-full h-screen bg-black flex flex-col justify-between gap-y-4'}>
@ -38,7 +37,7 @@ export default async function Page({ params }: { params: { id: string } }) {
<div className={'flex justify-center relative w-full'}> <div className={'flex justify-center relative w-full'}>
<div className={'flex items-center text-center'}> <div className={'flex items-center text-center'}>
<Image src={ginger} alt={'Logo'} width={120} height={120} className={'inline-flex'} /> <Image src={ginger} alt={'Logo'} width={120} height={120} className={'inline-flex'} />
<h2 className={'text-4xl font-bold text-white absolute top-[50%] left-[50%] translate-x-[-50%] translate-y-[-50%]'}>{teacherGrade.grade}</h2> <h2 className={'text-4xl font-bold text-white absolute top-[50%] left-[50%] translate-x-[-50%] translate-y-[-50%]'}>{teacherGrade}</h2>
</div> </div>
</div> </div>
)} )}

View File

@ -1,32 +1,32 @@
"use client" 'use client';
import {Santa} from "@components/custom/santa"; import { Santa } from '@components/custom/santa';
interface ChartProps { interface ChartProps {
data: { data: {
name: string name: string;
total: number total: number;
}[] }[];
} }
export function Chart({...props}: ChartProps) { export function Chart({ ...props }: ChartProps) {
return ( return (
<div className={"text-[#F0F0F0] flex-1 flex items-end"}> <div className={'text-[#F0F0F0] flex-1 flex items-end'}>
<div className={"w-full"}> <div className={'w-full'}>
<div className={"flex flex-row justify-between w-full items-end"}> <div className={'flex flex-row justify-between w-full items-end'}>
{props.data.map((item, index) => ( {props.data.map((item, index) => (
<Santa height={item.total} key={index}/> <Santa height={item.total} key={index} />
))} ))}
</div> </div>
<div className={"flex flex-row justify-between w-full items-end mt-2"}> <div className={'flex flex-row justify-between w-full items-end mt-2'}>
{props.data.map((item, index) => ( {props.data.map((item, index) => (
<div className={"w-[100px] h-fit flex flex-col items-center"} key={index}> <div className={'w-[100px] h-fit flex flex-col items-center'} key={index}>
<div className={"h-[14px] w-[3pt] bg-white"}/> <div className={'h-[14px] w-[3pt] bg-white'} />
<span className={""}>{item.name}</span> <span className={''}>{item.name}</span>
</div> </div>
))} ))}
</div> </div>
</div> </div>
</div> </div>
) );
} }