🚧 boutton admin retrait
This commit is contained in:
1 parent
4e404bcb9d
commit
0a50ebe952
7 files changed
+157
-12
No files matched your search
+36
-6
@@ -1,6 +1,8 @@
|
||||
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';
|
||||
@@ -14,6 +16,8 @@ import { AiOutlineCheck } from 'react-icons/ai'
|
||||
|
||||
import Router from "next/router";
|
||||
|
||||
import { useRouter } from 'next/navigation';
|
||||
|
||||
|
||||
import style from '../styles/Connexion.module.css';
|
||||
|
||||
@@ -92,11 +96,14 @@ const Admin = (props: Props) => {
|
||||
const [response, setResponse] = useState(null)
|
||||
const [currentUser, setCurrentUser] = useState(null)
|
||||
const [users, setUsers] = useState(props.users)
|
||||
const [currentUserGain, setCurrentUserGain] = useState(null)
|
||||
const router = useRouter()
|
||||
|
||||
|
||||
const { register, setValue, handleSubmit, formState: { errors } } = useForm<FormData>({
|
||||
});
|
||||
|
||||
const fetcher = async (url, method, body = {}) => {
|
||||
const fetcher = async (url, method, body = {}, callback?: any) => {
|
||||
try {
|
||||
await fetch(url, {
|
||||
method,
|
||||
@@ -106,7 +113,7 @@ const Admin = (props: Props) => {
|
||||
.then((res) => res.json())
|
||||
.then(async (data) => {
|
||||
setResponse(data);
|
||||
setUsers(data.res)
|
||||
callback;
|
||||
if (data.err) {
|
||||
toast.error(data.err, {
|
||||
position: "top-center",
|
||||
@@ -178,8 +185,10 @@ const Admin = (props: Props) => {
|
||||
fetcher('/api/user/delete', 'DELETE', body)
|
||||
};
|
||||
|
||||
const selectUser = (user: User) => {
|
||||
const selectUser = (user: any) => {
|
||||
console.log('d', user.gains)
|
||||
setCurrentUser(user);
|
||||
setCurrentUserGain(user.gains)
|
||||
setValue("name", user.name)
|
||||
setValue("email", user.email)
|
||||
setValue("surname", user.surname)
|
||||
@@ -188,6 +197,26 @@ const Admin = (props: Props) => {
|
||||
setValue("phoneNumber", user.phoneNumber)
|
||||
}
|
||||
|
||||
const changeGiftStatus = async (gain: Gain) => {
|
||||
const body = { gain }
|
||||
fetcher('/api/user/update-gift', 'PUT', body)
|
||||
console.log('g', gain);
|
||||
|
||||
// setCurrentUserGain(gain)
|
||||
// router.refresh();
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return (
|
||||
<>
|
||||
<Head>
|
||||
@@ -217,6 +246,7 @@ const Admin = (props: Props) => {
|
||||
</thead>
|
||||
<tbody>
|
||||
{users.map((info: User, index: number) => {
|
||||
|
||||
return <tr key={index} className={`${style.cursor}`} onClick={() => selectUser(info)}>
|
||||
<th scope="row">{++index}</th>
|
||||
<td>{info.surname}</td>
|
||||
@@ -320,19 +350,19 @@ const Admin = (props: Props) => {
|
||||
<th scope="col">Codes joués</th>
|
||||
<th scope="col">Cadeaux</th>
|
||||
<th scope="col">Retiré en magasin</th>
|
||||
<th scope="col">Retrait</th>
|
||||
<th scope="col">Date Retrait</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{currentUser?.gains.map((gain: Gain, index: number) => {
|
||||
{currentUserGain.map((gain: Gain, index: number) => {
|
||||
return <tr key={index}>
|
||||
<th scope="row">{++index}</th>
|
||||
<td>{gain?.codeCadeau}</td>
|
||||
<td>{props.produits[gain.produitId - 1].nomProduit}</td>
|
||||
<td>{gain?.retrait ? '❌ déja récupéré' : '✔️ disponible'}</td>
|
||||
<td>{gain?.dateRetrait ? dateFormat(gain?.dateRetrait, "dd/mm/yyyy") : ''}</td>
|
||||
<td>
|
||||
<button className="btn btn-primary">OUI</button>
|
||||
<button className="btn btn-primary" disabled={gain?.retrait} onClick={() => changeGiftStatus(gain)}>OUI</button>
|
||||
</td>
|
||||
</tr>
|
||||
})}
|
||||
|
||||
@@ -6,7 +6,7 @@ import prisma from '../../../lib/prisma';
|
||||
|
||||
|
||||
//api qui va rendre un utilisateur admin (pour les developpeurs)
|
||||
//--> http://localhost:3000/api/superadmin/admin?pass=1234 (a effacer)
|
||||
//--> http://localhost:3000/api/superadmin/make-admin?pass=1234 (a effacer)
|
||||
|
||||
export default async function makeMeAdmin(req: NextApiRequest, res: NextApiResponse) {
|
||||
const { pass } = req.query;
|
||||
@@ -23,7 +23,7 @@ export default async function makeMeAdmin(req: NextApiRequest, res: NextApiRespo
|
||||
role: Role.SUPERADMIN,
|
||||
},
|
||||
})
|
||||
return res.status(200).send({ message: 'Profiled updated welcome SUPERADMIN !' });
|
||||
return res.status(200).send({ message: 'Profiled updated welcome ADMIN !' });
|
||||
} else {
|
||||
res.status(401).send({ message: 'Unauthorized' })
|
||||
}
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
import prisma from '../../../lib/prisma';
|
||||
import chance from 'chance'
|
||||
import cryptoRandomString from 'crypto-random-string';
|
||||
|
||||
|
||||
import { NextApiRequest, NextApiResponse } from 'next';
|
||||
import { getSession } from 'next-auth/react';
|
||||
import { Cadeaux } from '../../../enum/cadeaux.enum';
|
||||
import { Role } from '../../../enum/role.enum';
|
||||
import { Gain } from '.prisma/client';
|
||||
|
||||
|
||||
|
||||
//API gain qui va generer un code cadeau qui sera sur la facture du client
|
||||
//On part du principe qu'une 'API caisse'(qu'on ne gere pas) déclenche l'appel à l'API gain
|
||||
|
||||
export default async function updtateGift(req: NextApiRequest, res: NextApiResponse) {
|
||||
|
||||
|
||||
|
||||
const session = await getSession({ req })
|
||||
|
||||
const { gain } = req.body;
|
||||
|
||||
// est-ce la méthode est un POST ou PUT?
|
||||
// if (req.method !== ("POST" || 'PUT')) {
|
||||
// throw new Error(
|
||||
// `The HTTP ${req.method} method is not supported at this route.`
|
||||
// );
|
||||
// }
|
||||
|
||||
// est-ce que utilisateur est connecté ?
|
||||
if (!session) {
|
||||
res.status(401).json({ err: "Vous devez être connecté" });
|
||||
return;
|
||||
}
|
||||
|
||||
//récupération des gain de l'utilisateur et mise à jour
|
||||
const updatedGain: Gain = await prisma.gain.update({
|
||||
where: { id: gain.id },
|
||||
data: {
|
||||
retrait: true,
|
||||
dateRetrait: new Date()
|
||||
},
|
||||
})
|
||||
|
||||
const updatedCurrentUser = await prisma.user.findFirst({
|
||||
where: { id: updatedGain.userId },
|
||||
include: { gains: true }
|
||||
})
|
||||
|
||||
// est-ce que utilisateur est un client classique ? Donc interdit pour lui de faire des updates
|
||||
// A ACTIVER EN PRODUCTION
|
||||
// if (user.role === 'USER') {
|
||||
// res.status(401).json({ err: "Vous n'avez pas l'autorisation" });
|
||||
// return;
|
||||
// }
|
||||
|
||||
////////////////////////////////////////////////
|
||||
|
||||
|
||||
|
||||
|
||||
res.status(200).send({ message: `Le gain a ont été mis à jour !`, res: updatedCurrentUser });
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user