feat: prisma fix

This commit is contained in:
Elie Baier 2023-12-04 10:23:31 +01:00
parent 91b0c04208
commit a637ab23e7
4 changed files with 24 additions and 6 deletions

View File

@ -1,8 +1,6 @@
import type { NextApiRequest, NextApiResponse } from 'next' import type { NextApiRequest, NextApiResponse } from 'next'
import { PrismaClient } from '@prisma/client' import prisma from '@lib/prisma';
const prisma = new PrismaClient()
interface IBody { interface IBody {
key: string key: string

View File

@ -7,17 +7,21 @@ export default function play() {
const [grade, setGrade] = useState<number>(0); const [grade, setGrade] = useState<number>(0);
const router = useRouter(); const router = useRouter();
let voted; let voted;
let password: string | null; const [password, setPassword] = useState<string | null>('');
useEffect(() => { useEffect(() => {
password = localStorage.getItem('@password'); const pass = localStorage.getItem('@password');
setPassword(pass);
voted = localStorage.getItem('@voted'); voted = localStorage.getItem('@voted');
if (!password) { if (!pass) {
router.push('/'); router.push('/');
} }
}, []); }, []);
async function handleSubmit(event: React.FormEvent<HTMLFormElement>) { async function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
event.preventDefault(); event.preventDefault();
console.log(grade, password);
const response = await fetch('/api/grade', { const response = await fetch('/api/grade', {
method: 'POST', method: 'POST',
headers: { headers: {

15
lib/prisma.ts Normal file
View File

@ -0,0 +1,15 @@
import { PrismaClient } from '@prisma/client'
const prismaClientSingleton = () => {
return new PrismaClient()
}
declare global {
var prisma: undefined | ReturnType<typeof prismaClientSingleton>
}
const prisma = globalThis.prisma ?? prismaClientSingleton()
export default prisma
if (process.env.NODE_ENV !== 'production') globalThis.prisma = prisma

View File

@ -21,6 +21,7 @@
"paths": { "paths": {
"@/*": ["./*"], "@/*": ["./*"],
"@components/*": ["./components/*"], "@components/*": ["./components/*"],
"@lib/*": ["./lib/*"],
} }
}, },
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"], "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],