import React, { useEffect, useState, useSyncExternalStore } from "react"; import prisma from '../lib/prisma' import Image from 'next/image'; import * as Yup from 'yup'; import Head from "next/head"; import { getCsrfToken, getSession, signIn, useSession } from "next-auth/react"; import { yupResolver } from "@hookform/resolvers/yup"; import "yup-phone"; import { SubmitHandler, useForm } from "react-hook-form"; import { AiOutlineCheck } from 'react-icons/ai' import Router from "next/router"; import style from '../styles/Connexion.module.css'; import imageConnexion from '../public/images/connexion_image.png' import Link from "next/link"; import { TfiInfoAlt } from 'react-icons/tfi' import { TfiGift } from 'react-icons/tfi' import { toast } from "react-toastify"; import { Token } from "nodemailer/lib/xoauth2"; import { isGeneratorFunction } from "util/types"; import { UserProps } from "../types/user"; import { UserGainProps } from "../types/userGain"; import { Gain, Produit, User } from "@prisma/client"; type FormData = { email: string; name: string; surname: string; address: string; zipCode: number; phoneNumber: number; }; export async function getServerSideProps(context) { const session = await getSession(context) if (!session) { return { redirect: { destination: '/', permanent: false } } } const user = await prisma.user.findUnique({ where: { email: session?.user.email } }); const userGains = await prisma.gain.findMany({ where: { userId: user.id }, include: { produit: true } }) return { props: { user: JSON.parse(JSON.stringify(user)), userGains, }, } } type Props = { user: User; userGains: any; } const MonCompte = (props: Props) => { const { data: session, status } = useSession(); const [response, setResponse] = useState(null) const validationSchema = Yup.object().shape({ name: Yup.string() .trim() .min(2, "le nom doit comporter au moins 2 caractères") .max(20, "le nom ne doit pas dépasser 20 caractères"), surname: Yup.string() .trim() .min(2, "le prenom doit comporter au moins 2 caractères") .max(20, "le prenom ne doit pas dépasser 20 caractères"), address: Yup.string() .trim() .max(50, "l'adresse ne doit pas dépasser 50 caractères"), zipCode: Yup.number() .typeError("le code postal est invalide"), phoneNumber: Yup.string() .phone("FR", true, "le numéro de téléphone est invalide") }); const { register, setValue, handleSubmit, formState: { errors } } = useForm({ resolver: yupResolver(validationSchema) }); const onSubmit = async ({ email, name, surname, address, zipCode, phoneNumber }: FormData) => { try { const body = { email, name, surname, address, zipCode: Number(zipCode), phoneNumber: Number(phoneNumber), }; await fetch(`/api/user/`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), }) .then((res) => res.json()) .then(async (data) => { setResponse(data); if (data.err) { toast.error(data.err, { position: "top-center", autoClose: 2000, hideProgressBar: false, closeOnClick: true, pauseOnHover: true, draggable: true, progress: undefined, theme: "light", }); return; } toast.success(data.message, { position: "top-center", autoClose: 5000, hideProgressBar: false, closeOnClick: true, pauseOnHover: true, draggable: true, progress: undefined, theme: "light", }); }) } catch (error) { console.error('ERROR :', error); } }; return ( <> Mon Compte

Mes informations


{errors.name?.message}
{errors.surname?.message}
{errors.address?.message}
{errors.zipCode?.message}
{errors.phoneNumber?.message}

Mes cadeaux


{props.userGains.length <= 0 &&

Pas encore de cadeau gagné

} {props.userGains.length >= 1 && {props.userGains.map((info: any) => { return })}
# Mes codes joué Mes cadeaux
{info.codeCadeau} {info.produit.nomProduit}
}
) }; export default MonCompte;