🚧 boutton admin retrait

This commit is contained in:
eric committed 2022-12-08 12:52:03 +01:00
1 parent 4e404bcb9d
commit 0a50ebe952
7 files changed
+157 -12

No files matched your search

+36 -6
View File
@@ -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>
})}