From d5cb00dc3d7b0d26d6965e7a5e563be654b5d778 Mon Sep 17 00:00:00 2001 From: Victor Turgeon <76506447+Medenos@users.noreply.github.com> Date: Sat, 5 Nov 2022 13:41:45 -0400 Subject: [PATCH] Fix de modification et delete d'image --- .../Controllers/ProductController.cs | 16 +++-- .../src/components/Modify.js | 65 +++++++++++++++---- .../src/pages/Inventaire.js | 14 +++- 3 files changed, 74 insertions(+), 21 deletions(-) diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs index 9917983..ab5db2d 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs @@ -86,11 +86,11 @@ public class ProductController : ControllerBase { [EnableCors("_myAllowSpecificOrigins"), HttpPatch()] public async Task> Patch([FromForm] ProductModel prod) { string? oldImage = ""; + if (prod.Price <= prod.PromoPrice) + prod.PromoPrice = prod.Price - 0.01M; try { - if (prod.ImageFile is not null) { - oldImage = _context.Products.FirstOrDefault(x => x.Id == prod.Id).ImageName; - if (oldImage == prod.ImageName) - oldImage = ""; + if (prod.ImageFile is not null) { + oldImage = prod.ImageName; prod.ImageName = await SaveImage(prod.ImageFile); } @@ -157,11 +157,15 @@ public class ProductController : ControllerBase { } private void DeleteImages(string imageName) { + + if (imageName == "default.jpg" || imageName == "default_thumbnail.jpg") + return; + var files = System.IO.Directory.GetFiles(_hostEnvironment.ContentRootPath + "/Images") - .Where(x => x.Contains(imageName)).ToArray(); + .Where(x => x.Contains(Path.GetFileNameWithoutExtension(imageName))).ToArray(); foreach (var file in files) - System.IO.File.Delete(_hostEnvironment.ContentRootPath + "/Images/" + file); + System.IO.File.Delete(file); } #endregion diff --git a/GrossesMitaines/grosses-mitaines-ui/src/components/Modify.js b/GrossesMitaines/grosses-mitaines-ui/src/components/Modify.js index ab83d44..b5f3ca4 100644 --- a/GrossesMitaines/grosses-mitaines-ui/src/components/Modify.js +++ b/GrossesMitaines/grosses-mitaines-ui/src/components/Modify.js @@ -1,6 +1,6 @@ -import { useState } from "react" +import { useState, useEffect } from "react" import { Dropdown } from "react-bootstrap"; -import {Button} from "react-bootstrap"; +import { Button } from "react-bootstrap"; function renderStatus(statusCode) { if (statusCode !== undefined) { @@ -32,21 +32,57 @@ const Modify = ({ morceau, onModify }) => { const [description, setDescription] = useState(morceau.description); const [category, setCategory] = useState(morceau.category); const [price, setPrice] = useState(morceau.price); - const [promoPrice, setPromoPrice] = useState(""); + const [promoPrice, setPromoPrice] = useState(morceau.promoPrice); const [quantity, setQuantity] = useState(morceau.quantity); - const [imageName, setImageName] = useState(morceau.imageName); - const [status, setStatusValue] = useState(morceau.status) + 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 handleSubmit = (e) => { e.preventDefault(); // EmpĂȘcher de reloader la page au submit. - // Appeler le comportement onCreation - onModify({ id, title, description, category, price, promoPrice, quantity, imageName, status }) + // 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, + }); } + const handleImageChange = (e) => { + if (e.target.files && e.target.files[0]) + setImage(e.target.files[0]); + else + setImage(null); + } + return (
@@ -94,11 +130,16 @@ const Modify = ({ morceau, onModify }) => { onChange={(e) => setQuantity(e.target.value)} />
- - setImageName(e.target.value)} /> + + handleImageChange(e)} /> + {imageUrl &&
+ +
} +
diff --git a/GrossesMitaines/grosses-mitaines-ui/src/pages/Inventaire.js b/GrossesMitaines/grosses-mitaines-ui/src/pages/Inventaire.js index 5376223..5f271ea 100644 --- a/GrossesMitaines/grosses-mitaines-ui/src/pages/Inventaire.js +++ b/GrossesMitaines/grosses-mitaines-ui/src/pages/Inventaire.js @@ -73,14 +73,22 @@ const Inventaire = () => { const handleModifyItem = async (morceau) => { + console.log(morceau); + + let formData = new FormData(); + Object.keys(morceau).map((k) => { + formData.set(k,morceau[k]); + }); + + const response = await fetch(`https://localhost:7292/api/Product`, { method: 'PATCH', credentials: 'include', + mode: 'cors', headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/json' + 'accept': 'application/json', }, - body: JSON.stringify(morceau) + body: formData }) const modifiedMorceau = await response.json();