import { useState, useEffect } from "react" import { Dropdown } from "react-bootstrap"; import { Button } from "react-bootstrap"; import { useForm } from "react-hook-form"; function renderStatus(statusCode) { if (statusCode !== undefined) { switch (statusCode) { case 0: return ("Disponible"); case 1: return ("En commande"); case 2: return ("Indisponible"); case 3: return ("Liquidation"); case 4: return ("Promotion"); case 5: return ("Discontinué"); default: return (<>); } } } const Modify = ({ morceau, onModify }) => { const { register, handleSubmit, formState: { errors } } = useForm(); const [currentStatus, setStatus] = useState(renderStatus(morceau.status)); const [title, setTitle] = useState(morceau.title); const [description, setDescription] = useState(morceau.description); const [category, setCategory] = useState(morceau.category); const [price, setPrice] = useState(morceau.price); const [promoPrice, setPromoPrice] = useState(morceau.promoPrice); const [quantity, setQuantity] = useState(morceau.quantity); const [status, setStatusValue] = useState(morceau.status); const [imageFile, setImage] = useState(null); const [imageUrl, setImageUrl] = useState(null); useEffect(() => { if (imageFile) setImageUrl(URL.createObjectURL(imageFile)); else { fetch(`https://localhost:7292/api/Image?id=${morceau.id}`) .then(response => response.blob()) .then(blob => { setImageUrl(URL.createObjectURL(blob)); }) } }, [imageFile]); const id = morceau.id; const onSubmit = (e) => { // Appeler le comportement onModify({ id: morceau.id, title: title, description: description, category: category, price: price, promoPrice: promoPrice, quantity: quantity, imageFile: imageFile, imageName: morceau.imageName, status: status, }, morceau.title ); } const handleImageChange = (e) => { if (e.target.files && e.target.files[0]) setImage(e.target.files[0]); else setImage(null); } return (

Modifier morceau: {morceau.title}

setTitle(e.target.value)} />
{errors.title && errors.title.type === 'required' && Veuillez entrer un titre de morceaux.} {errors.title && errors.title.type === 'maxLength' && Ne doit pas dépasser 255 caractères!}
setDescription(e.target.value)} />
{errors.description && errors.description.type === 'required' && Veuillez écrire une description.}
setCategory(e.target.value)} />
{errors.category && errors.category.type === 'required' && Veuillez inscrire une catégorie.}
setPrice(e.target.value)} />
{errors.price && errors.price.type === 'required' && Veuillez entrer un prix.} {errors.price && errors.price.type === 'min' && Minimum 0.01$.} {errors.price && errors.price.type === 'max' && Trop cher... voyons donc!}
setPromoPrice(e.target.value)} />
{errors.promoPrice && errors.promoPrice.type === 'required' && Veuillez entrer un prix promotionnel.} {errors.promoPrice && errors.promoPrice.type === 'min' && Minimum 0$.} {errors.promoPrice && errors.promoPrice.type === 'max' && Trop cher... voyons donc!}
setQuantity(e.target.value)} />
{errors.quantity && errors.quantity.type === 'min' && Minimum 0.}
handleImageChange(e)} /> {imageUrl &&
}
{currentStatus} { setStatus("Disponible"); // Mets le nom afficher quand le dropdown est fermé setStatusValue(0); }}> Disponible {/*Le nom de l'option*/} { setStatus("Indisponible"); setStatusValue(2); }}> Indisponible { setStatus("En Commande"); setStatusValue(1); }}> En Commande { setStatus("Liquidation"); setStatusValue(3); }}> Liquidation { setStatus("Promotion"); setStatusValue(4); }}> Promotion { setStatus("Discontinué"); setStatusValue(5); }}> Discontinué
) } export default Modify