Files
2024-DEV-BUT3/src/components/item/ItemBox.jsx

107 lines
3.6 KiB
React
Raw Normal View History

2024-05-09 18:39:06 +02:00
import React, { useEffect, useState } from 'react';
import { searchAndResizeImage } from '../../api/image-request'
import '../../assets/styles/modal.css'
import '../../assets/styles/itembox.css'
import FormUpdateItem from '../form/formUpdateItem';
2024-03-27 11:47:47 +01:00
2024-05-09 18:39:06 +02:00
// Composant Image
const Image = ({ src, alt, request, _id }) => {
const [cacheUrl, setCacheUrl] = useState(null);
useEffect(() => {
const fetchData = async () => {
let cachedUrl = localStorage.getItem(_id);
if (!cachedUrl) {
try {
cachedUrl = await searchAndResizeImage(request);
localStorage.setItem(_id, cachedUrl);
console.log("Mise en cache de l'image avec l'ID : " + _id);
} catch (error) {
console.error("Erreur lors de la récupération de l'image : ", error);
}
}
setCacheUrl(cachedUrl);
};
fetchData();
}, [request, _id]);
if (src) {
return <img src={src} alt={alt} style={{ display: 'block', margin: 'auto' }} width='150px' height='150px' />;
} else if (cacheUrl) {
return <img src={cacheUrl} alt={alt} style={{ display: 'block', margin: 'auto' }} width='150px' height='150px' />;
} else {
return <img src={"https://media.discordapp.net/attachments/1164176196930637956/1167746303820840990/IMG_20231028_104620.jpg?ex=663ddefe&is=663c8d7e&hm=0985ce123fd1751f65388f7fefde5db6ce817e514e30f1d3c81eb28b15e78453&=&"} alt={alt} style={{ display: 'block', margin: 'auto' }} width='150px' height='150px' />;
}
2024-03-27 11:47:47 +01:00
};
2024-05-09 18:39:06 +02:00
// Composant Description
2024-03-27 11:47:47 +01:00
const Description = ({ title, children }) => {
return (
<div className="description">
<h2 className="text-ellipsis">{title}</h2>
2024-03-27 11:47:47 +01:00
{children}
</div>
);
};
2024-05-09 18:39:06 +02:00
// Composant Caractéristique
2024-03-27 11:47:47 +01:00
const Characteristic = ({ label, value }) => {
2024-05-09 18:39:06 +02:00
2024-03-27 11:47:47 +01:00
return (
<div className="characteristic">
<strong>{label}:</strong> {value}
</div>
);
};
2024-05-09 18:39:06 +02:00
// Composant Détails du Produit
export const ItemBox = ({ model, brand, purchaseDate, price, _id }) => {
const [isModalOpen, setIsModalOpen] = useState(false);
// Fonction pour ouvrir la fenêtre modale
const openModal = () => {
setIsModalOpen(true);
};
// Fonction pour fermer la fenêtre modale
const closeModal = () => {
setIsModalOpen(false);
};
2024-05-09 18:39:06 +02:00
let productname = brand + " " + model;
let formatedPrice = price + "€";
let formatedDate = new Date(purchaseDate).toLocaleDateString('fr-FR');
let request = brand + " " + model
return (
<div className="product-details" >
<Description title={productname} >
<Image request={request} _id={_id} alt="Product" />
<Characteristic label="Model" value={model} />
<Characteristic label="Brand" value={brand} />
<Characteristic label="Purchase Date" value={formatedDate} />
<Characteristic label="Price" value={formatedPrice} />
{/* Bouton d'édition pour ouvrir la fenêtre modale */}
<button onClick={openModal}>Edit</button>
2024-05-09 18:39:06 +02:00
</Description>
{/* Fenêtre modale */}
{isModalOpen && (
<div className="modal">
<div className="modal-content">
<span className="close" onClick={closeModal}>&times;</span>
2024-05-12 12:46:12 +02:00
<FormUpdateItem itemId={_id}>{console.log("item ID :" + _id)}</FormUpdateItem>
</div>
</div>
)}
2024-05-09 18:39:06 +02:00
</div>
);
};