@ -1,9 +1,6 @@
import React , { useEffect , useState , useSyncExternalStore } from "react" ;
import React , { useEffect , useState , useSyncExternalStore } from "react" ;
import prisma from '../lib/prisma'
import prisma from '../lib/prisma'
import dateFormat , { masks } from "dateformat" ;
import dateFormat , { masks } from "dateformat" ;
import Image from 'next/image' ;
import Image from 'next/image' ;
import * as Yup from 'yup' ;
import * as Yup from 'yup' ;
import Head from "next/head" ;
import Head from "next/head" ;
@ -11,34 +8,31 @@ import { getCsrfToken, getSession, signIn, useSession } from "next-auth/react";
import { yupResolver } from "@hookform/resolvers/yup" ;
import { yupResolver } from "@hookform/resolvers/yup" ;
import "yup-phone" ;
import "yup-phone" ;
import { SubmitHandler , useForm } from "react-hook-form" ;
import { SubmitHandler , useForm } from "react-hook-form" ;
import { AiOutlineCheck } from 'react-icons/ai'
import { AiOutlineCheck } from 'react-icons/ai'
import Router from "next/router" ;
import Router from "next/router" ;
import { useRouter } from 'next/navigation' ;
import { useRouter } from 'next/navigation' ;
import style from '../styles/Connexion.module.css' ;
import style from '../styles/Connexion.module.css' ;
import imageConnexion from '../public/images/connexion_image.png'
import imageConnexion from '../public/images/connexion_image.png'
import Link from "next/link" ;
import Link from "next/link" ;
import { TfiInfoAlt } from 'react-icons/tfi'
import { TfiInfoAlt } from 'react-icons/tfi'
import { TfiGift } from 'react-icons/tfi'
import { TfiGift } from 'react-icons/tfi'
import { toast } from "react-toastify" ;
import { toast } from "react-toastify" ;
import { Token } from "nodemailer/lib/xoauth2" ;
import { Token } from "nodemailer/lib/xoauth2" ;
import { isGeneratorFunction } from "util/types" ;
import { isGeneratorFunction } from "util/types" ;
import { UserProps } from "../types/user" ;
import { UserProps } from "../types/user" ;
import { UserGainProps } from "../types/userGain" ;
import { UserGainProps } from "../types/userGain" ;
import { Gain , Produit , Role , User } from "@prisma/client" ;
import { Gain , Produit , Role , User } from "@prisma/client" ;
import { Doughnut } from "react-chartjs-2" ;
import {
Chart as ChartJS ,
CategoryScale ,
LinearScale ,
BarElement ,
Title ,
Tooltip ,
Legend ,
} from 'chart.js' ;
import { Bar } from 'react-chartjs-2' ;
type FormData = {
type FormData = {
email : string ;
email : string ;
@ -74,13 +68,36 @@ export async function getServerSideProps(context) {
}
}
} ) ;
} ) ;
const ticketRestants = await prisma . gain . count ( {
where : {
dateAchat : {
not : null ,
}
}
} )
const ticketsSaisis = await prisma . gain . count ( {
where : {
codeActif : false ,
} ,
} )
const gainsRetires = await prisma . gain . count ( {
where : {
retrait : true
} ,
} )
const produits = await prisma . produit . findMany ( { } )
const produits = await prisma . produit . findMany ( { } )
return {
return {
props : {
props : {
user : JSON.parse ( JSON . stringify ( user ) ) ,
user : JSON.parse ( JSON . stringify ( user ) ) ,
produits : JSON.parse ( JSON . stringify ( produits ) ) ,
produits : JSON.parse ( JSON . stringify ( produits ) ) ,
users : JSON.parse ( JSON . stringify ( users ) )
users : JSON.parse ( JSON . stringify ( users ) ) ,
ticketRestants : JSON.parse ( JSON . stringify ( ticketRestants ) ) ,
ticketsSaisis : JSON.parse ( JSON . stringify ( ticketsSaisis ) ) ,
gainsRetires : JSON.parse ( JSON . stringify ( gainsRetires ) ) ,
} ,
} ,
}
}
}
}
@ -88,18 +105,72 @@ export async function getServerSideProps(context) {
type Props = {
type Props = {
user : User ;
user : User ;
produits : Produit [ ] ;
produits : Produit [ ] ;
users : User [ ]
users : any ,
ticketRestants : number ,
ticketsSaisis : number ,
gainsRetires : number ,
}
}
const Admin = ( props : Props ) = > {
const Admin = ( props : Props ) = > {
const { data : session , status } = useSession ( ) ;
const { data : session , status } = useSession ( ) ;
const [ response , setResponse ] = useState ( null )
const [ response , setResponse ] = useState ( null )
const [ currentUser , setCurrentUser ] = useState ( null )
const [ currentUser , setCurrentUser ] = useState ( null )
const [ users , setUsers ] = useState ( props . users )
const [ users , setUsers ] = useState ( props . users )
const [ currentUserGain , setCurrentUserGain ] = useState ( null )
const [ currentUserGain , setCurrentUserGain ] = useState ( null )
const [ query , setQuery ] = useState ( "" ) ;
const router = useRouter ( )
const router = useRouter ( )
const isAdmin = session ? . user [ 'role' ] === Role . ADMIN
const isSuperAdmin = session ? . user [ 'role' ] === Role . SUPERADMIN
const bigWinner = users ? . find ( ( user ) = > user ? . isBigWinner )
ChartJS . register (
CategoryScale ,
LinearScale ,
BarElement ,
Title ,
Tooltip ,
Legend
) ;
const labels = [ 'Tickets générés' , 'Codes saisis' , 'Cadeaux récupérés' ] ;
const data = {
labels : labels ,
datasets : [ {
data : [ props . ticketRestants , props . ticketsSaisis , props . gainsRetires ] ,
backgroundColor : [
'rgba(255, 99, 132, 0.2)' ,
'rgba(255, 159, 64, 0.2)' ,
'rgba(255, 205, 86, 0.2)' ,
] ,
borderColor : [
'rgb(255, 99, 132)' ,
'rgb(255, 159, 64)' ,
'rgb(255, 205, 86)' ,
] ,
borderWidth : 1
} ]
} ;
const options = {
responsive : true ,
plugins : {
legend : {
display : false
} ,
title : {
display : false ,
} ,
} ,
} ;
const numberWithSpaces = ( x : number ) : string = > {
return x . toString ( ) . replace ( /\B(?=(\d{3})+(?!\d))/g , " " ) ;
}
const { register , setValue , handleSubmit , formState : { errors } } = useForm < FormData > ( {
const { register , setValue , handleSubmit , formState : { errors } } = useForm < FormData > ( {
} ) ;
} ) ;
@ -115,28 +186,10 @@ const Admin = (props: Props) => {
setResponse ( data ) ;
setResponse ( data ) ;
callback ;
callback ;
if ( data . err ) {
if ( data . err ) {
toast . error ( data . err , {
toastError ( data . err ) ;
position : "top-center" ,
autoClose : 2000 ,
hideProgressBar : false ,
closeOnClick : true ,
pauseOnHover : true ,
draggable : true ,
progress : undefined ,
theme : "light" ,
} ) ;
return ;
return ;
}
}
toast . success ( data . message , {
toastSuccess ( data . message ) ;
position : "top-center" ,
autoClose : 5000 ,
hideProgressBar : false ,
closeOnClick : true ,
pauseOnHover : true ,
draggable : true ,
progress : undefined ,
theme : "light" ,
} ) ;
} )
} )
}
}
catch ( error ) {
catch ( error ) {
@ -144,6 +197,32 @@ const Admin = (props: Props) => {
}
}
}
}
const toastError = ( text : string ) = > {
toast . error ( text , {
position : "top-center" ,
autoClose : 2000 ,
hideProgressBar : false ,
closeOnClick : true ,
pauseOnHover : true ,
draggable : true ,
progress : undefined ,
theme : "light" ,
} ) ;
}
const toastSuccess = ( text : string ) = > {
toast . success ( text , {
position : "top-center" ,
autoClose : 5000 ,
hideProgressBar : false ,
closeOnClick : true ,
pauseOnHover : true ,
draggable : true ,
progress : undefined ,
theme : "light" ,
} ) ;
}
const onSubmit = async ( {
const onSubmit = async ( {
email ,
email ,
name ,
name ,
@ -165,28 +244,24 @@ const Admin = (props: Props) => {
const onChangeRole = async ( e : React.ChangeEvent < HTMLSelectElement > , user : User ) = > {
const onChangeRole = async ( e : React.ChangeEvent < HTMLSelectElement > , user : User ) = > {
const body = { selectedUserEmail : user.email , selectedUserRole : e.target.value }
const body = { selectedUserEmail : user.email , selectedUserRole : e.target.value }
fetcher ( '/api/user/update' , 'PUT' , body )
fetcher ( '/api/user/update' , 'PUT' , body )
setTimeout ( ( ) = > {
router . refresh ( ) ;
} , 2000 )
} ;
} ;
const onDeleteUser = async ( userEmail : string ) = > {
const onDeleteUser = async ( userEmail : string ) = > {
const body = { selectedUserEmail : userEmail }
const body = { selectedUserEmail : userEmail }
if ( props . user . email === userEmail ) {
if ( props . user . email === userEmail ) {
toast . error ( 'Vous devez être déconnecté pour supprimer votre compte' , {
toastError ( 'Vous devez être déconnecté pour supprimer votre compte' )
position : "top-center" ,
autoClose : 2000 ,
hideProgressBar : false ,
closeOnClick : true ,
pauseOnHover : true ,
draggable : true ,
progress : undefined ,
theme : "light" ,
} ) ;
return ;
return ;
}
}
fetcher ( '/api/user/delete' , 'DELETE' , body )
fetcher ( '/api/user/delete' , 'DELETE' , body )
setTimeout ( ( ) = > {
router . refresh ( ) ;
} , 2000 )
} ;
} ;
const selectUser = ( user : any ) = > {
const selectUser = ( user : any ) = > {
console . log ( 'd' , user . gains )
setCurrentUser ( user ) ;
setCurrentUser ( user ) ;
setCurrentUserGain ( user . gains )
setCurrentUserGain ( user . gains )
setValue ( "name" , user . name )
setValue ( "name" , user . name )
@ -200,22 +275,48 @@ const Admin = (props: Props) => {
const changeGiftStatus = async ( gain : Gain ) = > {
const changeGiftStatus = async ( gain : Gain ) = > {
const body = { gain }
const body = { gain }
fetcher ( '/api/user/update-gift' , 'PUT' , body )
fetcher ( '/api/user/update-gift' , 'PUT' , body )
console . log ( 'g' , gain ) ;
setTimeout ( ( ) = > {
router . refresh ( ) ;
setCurrentUserGain ( gain )
} , 2000 )
// router.refresh();
} ;
} ;
const selectWinUser = async ( ) = > {
const customers = users . filter ( ( user ) = > user . role === 'USER' )
if ( users . length <= 0 ) {
toastError ( "Il n'y a pas encore d'utilisateur" )
return ;
}
const customersWithTicket = customers . filter ( ( user ) = > user . gains . length >= 1 )
if ( customersWithTicket . length <= 0 ) {
toastError ( "Il n'y a pas encore d'utilisateurs ayant joué" ) ;
return ;
}
const winUser = customersWithTicket [ Math . floor ( Math . random ( ) * customersWithTicket . length ) ]
const body = { selectedUserEmail : winUser.email }
fetcher ( '/api/user/bigwinner' , 'PUT' , body )
setTimeout ( ( ) = > {
router . refresh ( ) ;
} , 2000 )
}
const searchFilter = ( array : any ) = > {
return array . filter ( ( el : User ) = > {
if ( query === "" ) return props . users
return el . email ? . toLowerCase ( ) . includes ( query )
}
)
}
const filtered = searchFilter ( props . users )
const handleChange = ( e : React.ChangeEvent < HTMLInputElement > ) = > {
setQuery ( e . target . value ) ;
}
return (
return (
< >
< >
@ -230,10 +331,34 @@ const Admin = (props: Props) => {
< div className = "row" >
< div className = "row" >
< div className = "col-md-12 col-sm-12" >
< div className = "col-md-12 col-sm-12" >
< hr / >
< hr / >
< h1 className = "fw-light" > Utilisateurs < TfiInfoAlt size = { 40 } / > < / h1 >
< h1 className = "fw-light" > Dashboard Administration < / h1 >
< hr / >
< hr / >
< div className = "container" style = { { width : '30vw' } } >
< div className = "row" >
< div className = "col-12 justify-content-center" >
< p > Total tirages restant 📈 { numberWithSpaces ( props . ticketRestants ) . toString ( ) } < / p >
< Bar options = { options } data = { data } / >
< / div >
< / div >
< / div >
{ ( isAdmin ||
isSuperAdmin ) && < div >
< p className = "fw-light" > Grand jeu concours : s é l é ctionner l ' utilisateur gagnant pour les 1 an de th é < / p >
< p className = "fw-light" > En cliquant sur le bouton < / p >
< p className = "fw-light" > 👇 < / p >
< button className = "btn btn-primary" disabled = { users ? . some ( ( user ) = > user ? . isBigWinner ) } onClick = { selectWinUser } > S é l é ctionner < / button >
< hr / >
< / div > }
{ bigWinner ? < p > Grand gagnant 📢 < / p > : < p > Pas encore de gagnant tir é au sort 🤫 < / p > }
< p > { bigWinner && bigWinner . email } < / p >
{ users . length <= 0 && < p > Pas encore d ' utilisateur < / p > }
{ users . length <= 0 && < p > Pas encore d ' utilisateur < / p > }
{ users . length >= 1 &&
{ users . length >= 1 && < >
< div className = "input-group mb-3" >
< span className = "input-group-text" id = "basic-addon1" > 🔎 < / span >
< input type = "text" className = "form-control" onChange = { handleChange } placeholder = 'Rechercher un utilisateur avec son email...' aria - label = "search" / >
< / div >
< table className = { ` table table-hover ` } >
< table className = { ` table table-hover ` } >
< thead >
< thead >
< tr >
< tr >
@ -241,30 +366,30 @@ const Admin = (props: Props) => {
< th scope = "col" > Nom < / th >
< th scope = "col" > Nom < / th >
< th scope = "col" > Pr é nom < / th >
< th scope = "col" > Pr é nom < / th >
< th scope = "col" > Email < / th >
< th scope = "col" > Email < / th >
<th scope = "col" > Types < / th >
{session ? . user [ 'role' ] !== Role . SELLER && <th scope = "col" > Types < / th > }
< / tr >
< / tr >
< / thead >
< / thead >
< tbody >
< tbody >
{ users . map ( ( info : User , index : number ) = > {
{ filtered . map ( ( info : User , index : number ) = > {
return < tr key = { index } className = { ` ${ style . cursor } ` } onClick = { ( ) = > selectUser ( info ) } >
return < tr key = { index } className = { ` ${ style . cursor } ` } onClick = { ( ) = > selectUser ( info ) } >
< th scope = "row" > { ++ index } < / th >
< th scope = "row" > { ++ index } < / th >
< td > { info . surname } < / td >
< td > { info . surname } < / td >
< td > { info . name } < / td >
< td > { info . name } < / td >
< td > { info . email} < / td >
< td > { info . isBigWinner && '🎁' } { info . email} < / td >
<td >
{session ? . user [ 'role' ] !== Role . SELLER && <td >
< select className = "custom-select" defaultValue = { info . role } onChange = { ( e ) = > onChangeRole ( e , info ) } >
< select className = "custom-select" defaultValue = { info . role } onChange = { ( e ) = > onChangeRole ( e , info ) } >
< option value = "USER" > Client < / option >
< option value = "USER" > Client < / option >
< option value = "SELLER" > Vendeur < / option >
< option value = "SELLER" > Vendeur < / option >
< option value = "ADMIN" > Administrateur < / option >
< option value = "ADMIN" > Administrateur < / option >
{ isSuperAdmin && < option value = "SUPERADMIN" > SuperAdmin < / option > }
< / select >
< / select >
< / td >
< / td > }
<td > < button className = "btn btn-primary" onClick = { ( ) = > onDeleteUser ( info . email ) } > Supprimer < / button > < / td >
{session ? . user [ 'role' ] !== Role . SELLER && <td > < button className = "btn btn-primary" onClick = { ( ) = > onDeleteUser ( info . email ) } > Supprimer < / button > < / td > }
< / tr >
< / tr > ;
} ) }
} ) }
< / tbody >
< / tbody >
< / table >
< / table >
}
</ > }
< / div >
< / div >
< / div >
< / div >
@ -272,19 +397,19 @@ const Admin = (props: Props) => {
< div className = "row" >
< div className = "row" >
< div className = "col-md-6 col-sm-12" >
< div className = "col-md-6 col-sm-12" >
< hr / >
< hr / >
< h1 className = "fw-light "> { currentUser ? . email || 'utilisateur' } < TfiInfoAlt size = { 40 } / > < / h1 >
< h1 className = "fw-light text-truncate "> { currentUser ? . email || 'utilisateur' } < TfiInfoAlt size = { 40 } / > < / h1 >
< hr / >
< hr / >
{ currentUser && < form onSubmit = { handleSubmit ( onSubmit ) } >
{ currentUser && < form onSubmit = { handleSubmit ( onSubmit ) } >
< div className = "form-group" >
< div className = "form-group" >
< div className = "form-group row ">
< div className = "form-group row my-1 ">
< label htmlFor = "staticEmail" className = "col-sm-3 col-form-label" > Email : < / label >
< label htmlFor = "staticEmail" className = "col-sm-3 col-form-label" > Email : < / label >
< div className = "col-sm-9" >
< div className = "col-sm-9" >
< input type = "text" readOnly className = "form-control-plaintext text-sm-center" id = "staticEmail" value = { currentUser ? . email || '' } / >
< input type = "text" readOnly className = "form-control-plaintext text-sm-center" id = "staticEmail" value = { currentUser ? . email || '' } / >
< / div >
< / div >
< / div >
< / div >
< div className = "form-group row ">
< div className = "form-group row my-1 ">
< label htmlFor = "inputName" className = "col-sm-3 col-form-label" > Nom : < / label >
< label htmlFor = "inputName" className = "col-sm-3 col-form-label" > Nom : < / label >
< div className = "col-sm-9" >
< div className = "col-sm-9" >
< input { ...register ( " name " ) } className = { ` form-control ${ errors . name ? 'is-invalid' : '' } ` } defaultValue = { currentUser ? . name || '' } id = "inputName" placeholder = "John..." / >
< input { ...register ( " name " ) } className = { ` form-control ${ errors . name ? 'is-invalid' : '' } ` } defaultValue = { currentUser ? . name || '' } id = "inputName" placeholder = "John..." / >
@ -292,7 +417,7 @@ const Admin = (props: Props) => {
< / div >
< / div >
< / div >
< / div >
< div className = "form-group row ">
< div className = "form-group row my-1 ">
< label htmlFor = "surname" className = "col-sm-3 col-form-label" > Prenom : < / label >
< label htmlFor = "surname" className = "col-sm-3 col-form-label" > Prenom : < / label >
< div className = "col-sm-9" >
< div className = "col-sm-9" >
< input { ...register ( " surname " ) } className = { ` form-control ${ errors . surname ? 'is-invalid' : '' } ` } defaultValue = { currentUser ? . surname || '' } id = "surname" placeholder = "Doe..." / >
< input { ...register ( " surname " ) } className = { ` form-control ${ errors . surname ? 'is-invalid' : '' } ` } defaultValue = { currentUser ? . surname || '' } id = "surname" placeholder = "Doe..." / >
@ -300,7 +425,7 @@ const Admin = (props: Props) => {
< / div >
< / div >
< / div >
< / div >
< div className = "form-group row ">
< div className = "form-group row my-1 ">
< label htmlFor = "address" className = "col-sm-3 col-form-label" > Adresse : < / label >
< label htmlFor = "address" className = "col-sm-3 col-form-label" > Adresse : < / label >
< div className = "col-sm-9" >
< div className = "col-sm-9" >
< input { ...register ( " address " ) } className = { ` form-control ${ errors . address ? 'is-invalid' : '' } ` } defaultValue = { currentUser ? . address || '' } id = "address" placeholder = "1 rue de la paix..." / >
< input { ...register ( " address " ) } className = { ` form-control ${ errors . address ? 'is-invalid' : '' } ` } defaultValue = { currentUser ? . address || '' } id = "address" placeholder = "1 rue de la paix..." / >
@ -308,7 +433,7 @@ const Admin = (props: Props) => {
< / div >
< / div >
< / div >
< / div >
< div className = "form-group row ">
< div className = "form-group row my-1 ">
< label htmlFor = "zipCode" className = "col-sm-3 col-form-label" > Code postale : < / label >
< label htmlFor = "zipCode" className = "col-sm-3 col-form-label" > Code postale : < / label >
< div className = "col-sm-9" >
< div className = "col-sm-9" >
< input { ...register ( " zipCode " ) } className = { ` form-control ${ errors . zipCode ? 'is-invalid' : '' } ` } defaultValue = { currentUser ? . zipCode || '' } id = "zipCode" placeholder = "95000..." / >
< input { ...register ( " zipCode " ) } className = { ` form-control ${ errors . zipCode ? 'is-invalid' : '' } ` } defaultValue = { currentUser ? . zipCode || '' } id = "zipCode" placeholder = "95000..." / >
@ -316,7 +441,7 @@ const Admin = (props: Props) => {
< / div >
< / div >
< / div >
< / div >
< div className = "form-group row ">
< div className = "form-group row my-1 ">
< label htmlFor = "phoneNumber" className = "col-sm-3 col-form-label" > T é l é phone : < / label >
< label htmlFor = "phoneNumber" className = "col-sm-3 col-form-label" > T é l é phone : < / label >
< div className = "col-sm-9" >
< div className = "col-sm-9" >
< input { ...register ( " phoneNumber " ) } className = { ` form-control ${ errors . phoneNumber ? 'is-invalid' : '' } ` } defaultValue = { currentUser ? . phoneNumber || '' } id = "phoneNumber" placeholder = "0123456789..." / >
< input { ...register ( " phoneNumber " ) } className = { ` form-control ${ errors . phoneNumber ? 'is-invalid' : '' } ` } defaultValue = { currentUser ? . phoneNumber || '' } id = "phoneNumber" placeholder = "0123456789..." / >
@ -339,7 +464,7 @@ const Admin = (props: Props) => {
< / div >
< / div >
< div className = "col-md-6 col-sm-12" >
< div className = "col-md-6 col-sm-12" >
< hr / >
< hr / >
< h1 className = "fw-light" > Cadeaux de { currentUser ? . email || "l'utilisateur" } < TfiGift size = { 40 } / > < / h1 >
< h1 className = "fw-light" > Cadeaux < TfiGift size = { 40 } / > < / h1 >
< hr / >
< hr / >
{ currentUser ? . gains ? . length <= 0 && < p > Pas encore de cadeaux gagn é < / p > }
{ currentUser ? . gains ? . length <= 0 && < p > Pas encore de cadeaux gagn é < / p > }
{ currentUser ? . gains ? . length >= 1 &&
{ currentUser ? . gains ? . length >= 1 &&
@ -375,7 +500,7 @@ const Admin = (props: Props) => {
< / div >
< / div >
< / d i v >
< / d i v >
< / div >
< / d i v >
< / > )
< / > )
} ;
} ;