@ -21,18 +21,11 @@ import { Token } from "nodemailer/lib/xoauth2";
import { isGeneratorFunction } from "util/types" ;
import { UserProps } from "../types/user" ;
import { UserGainProps } from "../types/userGain" ;
import { Gain , Produit , Role , User } from "@prisma/client" ;
import { Gain , Produit , Role , TicketsRestants, 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' ;
import { Chart as ChartJS , ArcElement , Tooltip , Legend , RadialLinearScale } from 'chart.js' ;
import { PolarArea } from 'react-chartjs-2' ;
type FormData = {
email : string ;
@ -68,26 +61,8 @@ 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 tickets = await prisma . gain . findMany ( { } )
const totalTirageRestant = await prisma . ticketsRestants . findFirst ( { } )
const produits = await prisma . produit . findMany ( { } )
return {
@ -95,9 +70,8 @@ export async function getServerSideProps(context) {
user : JSON.parse ( JSON . stringify ( user ) ) ,
produits : JSON.parse ( JSON . stringify ( produits ) ) ,
users : JSON.parse ( JSON . stringify ( users ) ) ,
ticketRestants : JSON.parse ( JSON . stringify ( ticketRestants ) ) ,
ticketsSaisis : JSON.parse ( JSON . stringify ( ticketsSaisis ) ) ,
gainsRetires : JSON.parse ( JSON . stringify ( gainsRetires ) ) ,
tickets : JSON.parse ( JSON . stringify ( tickets ) ) ,
totalTirageRestant : JSON.parse ( JSON . stringify ( totalTirageRestant ) )
} ,
}
}
@ -106,12 +80,10 @@ type Props = {
user : User ;
produits : Produit [ ] ;
users : any ,
ticketRestants : number ,
ticketsSaisis : number ,
gainsRetires : number ,
tickets : Gain [ ]
totalTirageRestant : TicketsRestants
}
const Admin = ( props : Props ) = > {
const { data : session , status } = useSession ( ) ;
const [ response , setResponse ] = useState ( null )
@ -121,56 +93,58 @@ const Admin = (props: Props) => {
const [ query , setQuery ] = useState ( "" ) ;
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
ChartJS . register ( RadialLinearScale , ArcElement , Tooltip , Legend ) ;
const TicketRestants = props . tickets ? . filter ( ticket = > ticket . codeActif === true ) . length
console . log ( "🚀 ~ file: admin.tsx:103 ~ Admin ~ TicketRestants" , TicketRestants )
const TicketUtilise = props . tickets ? . filter ( ticket = > ticket . codeActif === false ) . length
console . log ( "🚀 ~ file: admin.tsx:104 ~ Admin ~ TicketUtilise" , TicketUtilise )
const TicketTotal = props . totalTirageRestant . total ;
console . log ( "🚀 ~ file: admin.tsx:107 ~ Admin ~ TicketTotal" , TicketTotal )
const TotalTirageRestant = props . totalTirageRestant ? . total ;
const ticketDataChart = {
labels : [ 'Tickets généré utlisés' , 'Tickets généré restants' ] ,
datasets : [
{
label : 'Tickets restants généré' ,
data : [ TicketUtilise , TicketRestants ] ,
backgroundColor : [
'rgba(255, 99, 132, 0.2)' ,
'rgba(54, 162, 235, 0.2)' ,
] ,
borderColor : [
'rgba(255, 99, 132, 1)' ,
'rgba(54, 162, 235, 1)' ,
] ,
borderWidth : 1 ,
} ,
title : {
display : false ,
} ,
} ,
] ,
} ;
const numberWithSpaces = ( x : number ) : string = > {
return x . toString ( ) . replace ( /\B(?=(\d{3})+(?!\d))/g , " " ) ;
const TotalTirageRestantData = {
labels : [ 'Total tirage restant' ] ,
datasets : [
{
label : 'Tirage de ticket' ,
data : [ TotalTirageRestant ] ,
backgroundColor : [
'rgba(54, 162, 235, 0.5)' ,
] ,
borderWidth : 1 ,
} ,
] ,
}
const { register , setValue , handleSubmit , formState : { errors } } = useForm < FormData > ( {
} ) ;
@ -186,10 +160,28 @@ const Admin = (props: Props) => {
setResponse ( data ) ;
callback ;
if ( data . err ) {
toastError ( data . err ) ;
toast . error ( data . err , {
position : "top-center" ,
autoClose : 2000 ,
hideProgressBar : false ,
closeOnClick : true ,
pauseOnHover : true ,
draggable : true ,
progress : undefined ,
theme : "light" ,
} ) ;
return ;
}
toastSuccess ( data . message ) ;
toast . success ( data . message , {
position : "top-center" ,
autoClose : 5000 ,
hideProgressBar : false ,
closeOnClick : true ,
pauseOnHover : true ,
draggable : true ,
progress : undefined ,
theme : "light" ,
} ) ;
} )
}
catch ( error ) {
@ -197,32 +189,6 @@ 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 ( {
email ,
name ,
@ -252,13 +218,19 @@ const Admin = (props: Props) => {
const onDeleteUser = async ( userEmail : string ) = > {
const body = { selectedUserEmail : userEmail }
if ( props . user . email === userEmail ) {
toastError ( 'Vous devez être déconnecté pour supprimer votre compte' )
toast . error ( '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 ;
}
fetcher ( '/api/user/delete' , 'DELETE' , body )
setTimeout ( ( ) = > {
router . refresh ( ) ;
} , 2000 )
} ;
const selectUser = ( user : any ) = > {
@ -284,19 +256,38 @@ const Admin = (props: Props) => {
const customers = users . filter ( ( user ) = > user . role === 'USER' )
if ( users . length <= 0 ) {
toastError ( "Il n'y a pas encore d'utilisateur" )
toast . error ( "Il n'y a pas encore d'utilisateur" , {
position : "top-center" ,
autoClose : 2000 ,
hideProgressBar : false ,
closeOnClick : true ,
pauseOnHover : true ,
draggable : true ,
progress : undefined ,
theme : "light" ,
} ) ;
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é" ) ;
toast . error ( "Il n'y a pas encore d'utilisateurs ayant joué" , {
position : "top-center" ,
autoClose : 2000 ,
hideProgressBar : false ,
closeOnClick : true ,
pauseOnHover : true ,
draggable : true ,
progress : undefined ,
theme : "light" ,
} ) ;
return ;
}
const winUser = customersWithTicket [ Math . floor ( Math . random ( ) * customersWithTicket . length ) ]
const body = { selectedUserEmail : winUser.email }
console . log ( body )
fetcher ( '/api/user/bigwinner' , 'PUT' , body )
setTimeout ( ( ) = > {
router . refresh ( ) ;
@ -315,7 +306,7 @@ const Admin = (props: Props) => {
const filtered = searchFilter ( props . users )
const handleChange = ( e : React.ChangeEvent < HTMLInputElement > ) = > {
setQuery ( e . target . value ) ;
setQuery ( e . target . value )
}
return (
@ -333,13 +324,17 @@ const Admin = (props: Props) => {
< hr / >
< h1 className = "fw-light" > Dashboard Administration < / h1 >
< 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 } / >
< p > Total tirages restant 📈 { props . totalTirageRestant ? . total ? . toString ( ) } < / p >
< Doughnut
data = { ticketDataChart } / >
< / div >
< / div >
< / div >
@ -366,7 +361,7 @@ const Admin = (props: Props) => {
< th scope = "col" > Nom < / th >
< th scope = "col" > Pr é nom < / th >
< th scope = "col" > Email < / th >
{session ? . user [ 'role' ] !== Role . SELLER && <th scope = "col" > Types < / th > }
<th scope = "col" > Types < / th >
< / tr >
< / thead >
< tbody >
@ -376,7 +371,7 @@ const Admin = (props: Props) => {
< td > { info . surname } < / td >
< td > { info . name } < / td >
< td > { info . isBigWinner && '🎁' } { info . email } < / td >
{ session? . user [ 'role' ] !== Role . SELLER && < td >
{ info. role !== Role . SELLER && < td >
< select className = "custom-select" defaultValue = { info . role } onChange = { ( e ) = > onChangeRole ( e , info ) } >
< option value = "USER" > Client < / option >
< option value = "SELLER" > Vendeur < / option >
@ -384,7 +379,7 @@ const Admin = (props: Props) => {
{ isSuperAdmin && < option value = "SUPERADMIN" > SuperAdmin < / option > }
< / select >
< / td > }
{ session? . user [ 'role' ] !== Role . SELLER && < td > < button className = "btn btn-primary" onClick = { ( ) = > onDeleteUser ( info . email ) } > Supprimer < / button > < / td > }
{ info. role !== Role . SELLER && < td > < button className = "btn btn-primary" onClick = { ( ) = > onDeleteUser ( info . email ) } > Supprimer < / button > < / td > }
< / tr > ;
} ) }
< / tbody >
@ -402,14 +397,14 @@ const Admin = (props: Props) => {
{ currentUser && < form onSubmit = { handleSubmit ( onSubmit ) } >
< div className = "form-group" >
< div className = "form-group row my-1 ">
< 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 = { currentUser ? . email || '' } / >
< / div >
< / div >
< div className = "form-group row my-1 ">
< 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 = { currentUser ? . name || '' } id = "inputName" placeholder = "John..." / >
@ -417,7 +412,7 @@ const Admin = (props: Props) => {
< / div >
< / div >
< div className = "form-group row my-1 ">
< 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 = { currentUser ? . surname || '' } id = "surname" placeholder = "Doe..." / >
@ -425,7 +420,7 @@ const Admin = (props: Props) => {
< / div >
< / div >
< div className = "form-group row my-1 ">
< 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 = { currentUser ? . address || '' } id = "address" placeholder = "1 rue de la paix..." / >
@ -433,7 +428,7 @@ const Admin = (props: Props) => {
< / div >
< / div >
< div className = "form-group row my-1 ">
< 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 = { currentUser ? . zipCode || '' } id = "zipCode" placeholder = "95000..." / >
@ -441,7 +436,7 @@ const Admin = (props: Props) => {
< / div >
< / div >
< div className = "form-group row my-1 ">
< 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 = { currentUser ? . phoneNumber || '' } id = "phoneNumber" placeholder = "0123456789..." / >