You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
260 lines
9.7 KiB
260 lines
9.7 KiB
import React, { useState } from "react";
|
|
import prisma from '../lib/prisma'
|
|
import * as Yup from 'yup';
|
|
import Head from "next/head";
|
|
import Image from 'next/image';
|
|
import { getSession, useSession } from "next-auth/react";
|
|
import { yupResolver } from "@hookform/resolvers/yup";
|
|
import "yup-phone";
|
|
import { useForm } from "react-hook-form";
|
|
import style from '../styles/Connexion.module.css';
|
|
import { TfiInfoAlt } from 'react-icons/tfi'
|
|
import { TfiGift } from 'react-icons/tfi'
|
|
import { toast } from "react-toastify";
|
|
import { Gain, User } from "@prisma/client";
|
|
import unandethe from '../public/images/unandethe.webp'
|
|
|
|
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: JSON.parse(JSON.stringify(userGains)),
|
|
},
|
|
}
|
|
}
|
|
|
|
type Props = {
|
|
user: User;
|
|
userGains: Gain[];
|
|
}
|
|
|
|
const MonCompte = (props: Props) => {
|
|
const { data: session } = useSession();
|
|
const [, 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, handleSubmit, formState: { errors } } = useForm<FormData>({
|
|
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 (
|
|
<>
|
|
<Head>
|
|
<title>Mon Compte</title>
|
|
<link rel="preconnect" href="https://fonts.googleapis.com" />
|
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossOrigin="" />
|
|
</Head>
|
|
<div className={`${style['masthead']} d-flex justify-content-center align-items-center text-center`}>
|
|
<div className="container align-items-center d-flex flex-column">
|
|
<div className={`${style['bloc-content']} col-12 col-md-12`}>
|
|
<div className="row">
|
|
{props.user.isBigWinner &&
|
|
<div>
|
|
<h3>!!! Vous avez été séléctionné pour gagner 1 an de thé !!!</h3>
|
|
<Image
|
|
src={unandethe}
|
|
alt="logo un an cadeau"
|
|
width={190}
|
|
height={180 - 40}
|
|
className={`${style['rotatingSlow']}`} />
|
|
|
|
</div>}
|
|
<div className="col-md-6 col-sm-12">
|
|
<hr />
|
|
<h1 className="fw-light">Mes informations <TfiInfoAlt size={40} /></h1>
|
|
<hr />
|
|
<form onSubmit={handleSubmit(onSubmit)}>
|
|
<div className="form-group">
|
|
<div className="form-group row">
|
|
<label htmlFor="staticEmail" className="col-sm-3 col-form-label">Email :</label>
|
|
<div className="col-sm-9">
|
|
<input type="text" readOnly className="form-control-plaintext text-sm-center" id="staticEmail" value={session?.user.email || ''} />
|
|
</div>
|
|
</div>
|
|
<div className="form-group row">
|
|
<label htmlFor="inputName" className="col-sm-3 col-form-label">Nom :</label>
|
|
<div className="col-sm-9">
|
|
<input {...register("name")} className={`form-control ${errors.name ? 'is-invalid' : ''}`} defaultValue={props.user?.name || ''} id="inputName" placeholder="John..." />
|
|
<div className="invalid-feedback alert alert-danger mt-2">{errors.name?.message}</div>
|
|
</div>
|
|
</div>
|
|
<div className="form-group row">
|
|
<label htmlFor="surname" className="col-sm-3 col-form-label">Prenom :</label>
|
|
<div className="col-sm-9">
|
|
<input {...register("surname")} className={`form-control ${errors.surname ? 'is-invalid' : ''}`} defaultValue={props.user.surname || ''} id="surname" placeholder="Doe..." />
|
|
<div className="invalid-feedback alert alert-danger mt-2">{errors.surname?.message}</div>
|
|
</div>
|
|
</div>
|
|
<div className="form-group row">
|
|
<label htmlFor="address" className="col-sm-3 col-form-label">Adresse :</label>
|
|
<div className="col-sm-9">
|
|
<input {...register("address")} className={`form-control ${errors.address ? 'is-invalid' : ''}`} defaultValue={props.user.address} id="address" placeholder="1 rue de la paix..." />
|
|
<div className="invalid-feedback alert alert-danger mt-2">{errors.address?.message}</div>
|
|
</div>
|
|
</div>
|
|
<div className="form-group row">
|
|
<label htmlFor="zipCode" className="col-sm-3 col-form-label">Code postale :</label>
|
|
<div className="col-sm-9">
|
|
<input {...register("zipCode")} className={`form-control ${errors.zipCode ? 'is-invalid' : ''}`} defaultValue={props.user.zipCode} id="zipCode" placeholder="95000..." />
|
|
<div className="invalid-feedback alert alert-danger mt-2">{errors.zipCode?.message}</div>
|
|
</div>
|
|
</div>
|
|
<div className="form-group row">
|
|
<label htmlFor="phoneNumber" className="col-sm-3 col-form-label">Téléphone :</label>
|
|
<div className="col-sm-9">
|
|
<input {...register("phoneNumber")} className={`form-control ${errors.phoneNumber ? 'is-invalid' : ''}`} defaultValue={props.user.phoneNumber} id="phoneNumber" placeholder="0123456789..." />
|
|
<div className="invalid-feedback alert alert-danger mt-2">{errors.phoneNumber?.message}</div>
|
|
</div>
|
|
</div>
|
|
<div className="form-group">
|
|
<button
|
|
type="submit"
|
|
className="btn btn-primary mt-2"
|
|
>
|
|
Sauvegarder mes changements
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
<div className="col-md-6 col-sm-12">
|
|
<hr />
|
|
<h1 className="fw-light">Mes cadeaux <TfiGift size={40} /></h1>
|
|
<hr />
|
|
{props.userGains.length <= 0 && <p>Pas encore de cadeau gagné</p>}
|
|
{props.userGains.length >= 1 &&
|
|
<table className="table">
|
|
<thead>
|
|
<tr>
|
|
<th scope="col">#</th>
|
|
<th scope="col">Mes codes joué</th>
|
|
<th scope="col">Mes cadeaux</th>
|
|
<th scope="col">Statut</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{props.userGains.map((info: any, index: number) => {
|
|
return <tr key={index}>
|
|
<th scope="row">{++index}</th>
|
|
<td>{info.codeCadeau}</td>
|
|
<td>{info.produit.nomProduit}</td>
|
|
<td>{info.retrait ? '❌ déja récupéré' : '✔️ venir me chercher !'}</td>
|
|
</tr>
|
|
})}
|
|
</tbody>
|
|
</table>
|
|
}
|
|
</div>
|
|
</div>
|
|
</div >
|
|
</div>
|
|
</div >
|
|
</>)
|
|
};
|
|
|
|
export default MonCompte;
|
|
|
|
|