This commit is contained in:
eric committed 2022-12-12 00:27:56 +01:00
1 parent d0c86bfb19
commit 77ddf98216
5 files changed
+116 -23

No files matched your search

+77 -23
View File
@@ -1,9 +1,6 @@
import React, { useEffect, useState, useSyncExternalStore } from "react";
import prisma from '../lib/prisma'
import dateFormat, { masks } from "dateformat";
import Image from 'next/image';
import * as Yup from 'yup';
import Head from "next/head";
@@ -11,34 +8,24 @@ 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 { useRouter } from 'next/navigation';
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, Role, User } from "@prisma/client";
import { Gain, Produit, Role, TicketsRestants, User } from "@prisma/client";
import { Doughnut } 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;
@@ -74,13 +61,20 @@ export async function getServerSideProps(context) {
}
});
const tickets = await prisma.gain.findMany({})
const totalTirageRestant = await prisma.ticketsRestants.findFirst({})
const produits = await prisma.produit.findMany({})
return {
props: {
user: JSON.parse(JSON.stringify(user)),
produits: JSON.parse(JSON.stringify(produits)),
users: JSON.parse(JSON.stringify(users))
users: JSON.parse(JSON.stringify(users)),
tickets: JSON.parse(JSON.stringify(tickets)),
totalTirageRestant: JSON.parse(JSON.stringify(totalTirageRestant))
},
}
}
@@ -88,7 +82,9 @@ export async function getServerSideProps(context) {
type Props = {
user: User;
produits: Produit[];
users: any
users: any,
tickets: Gain[]
totalTirageRestant: TicketsRestants
}
const Admin = (props: Props) => {
@@ -103,6 +99,47 @@ const Admin = (props: Props) => {
const isSuperAdmin = session?.user['role'] === Role.SUPERADMIN
const bigWinner = users?.find((user) => user?.isBigWinner)
ChartJS.register(RadialLinearScale, ArcElement, Tooltip, Legend);
const TicketRestants = props.tickets?.filter(ticket => ticket.codeActif === true).length
const TicketUtilise = props.tickets?.filter(ticket => ticket.codeActif === false).length
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,
},
],
};
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>({
});
@@ -264,11 +301,28 @@ const Admin = (props: Props) => {
<div className="row">
<div className="col-md-12 col-sm-12">
<hr />
<h1 className="fw-light">Utilisateurs <TfiInfoAlt size={40} /></h1>
<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 📈 {props.totalTirageRestant?.total?.toString()}</p>
<Doughnut
data={ticketDataChart} />
</div>
</div>
</div>
{(isAdmin ||
isSuperAdmin) && <div>
<p className="fw-light">Grand jeu concours séléction l'utilisateur gagnant pour les 1 an de thé</p>
<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>}
@@ -292,7 +346,7 @@ const Admin = (props: Props) => {
<th scope="row">{++index}</th>
<td>{info.surname}</td>
<td>{info.name}</td>
<td>{info.isBigWinner && '🌟🌟🌟'}{info.email}</td>
<td>{info.isBigWinner && '🎁'}{info.email}</td>
<td>
<select className="custom-select" defaultValue={info.role} onChange={(e) => onChangeRole(e, info)}>
<option value="USER">Client</option>
+5
View File
@@ -7,6 +7,7 @@ import { NextApiRequest, NextApiResponse } from 'next';
import { getSession } from 'next-auth/react';
import { Cadeaux } from '../../../enum/cadeaux.enum';
import { Role } from '../../../enum/role.enum';
import process from 'process';
@@ -19,6 +20,10 @@ export default async function apiGain(req: NextApiRequest, res: NextApiResponse)
const { pass } = req.query;
const goodPassword = pass === 'P9RDK2b47x1XQV5s3fSaW6erGpzTNudk8jZwCycJYtMHAqhEnm';
if (!process.env.ACTION_API) {
return res.status(401).send({ message: 'Unauthorized' })
}
//récuperation des tickets disponible
const getTickets = await prisma.ticketsRestants.findFirst({
where: {