UpdateProduct JSX
UpdateProduct JSX
import "./UpdateProduct.css";
import upload_area from "../Assets/upload_area.svg";
if (dataObj.success) {
product.image = dataObj.image_url;
}
}
await fetch(`http://localhost:4000/updateproduct/${selectedProduct}`, {
method: "PUT",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify(product),
})
.then((resp) => resp.json())
.then((data) =>
data.success ? alert("Product Updated") : alert("Failed to update product")
);
};
return (
<div className="updateproduct">
<div className="updateproduct-itemfield">
<p>Select Product to Update</p>
<select
value={selectedProduct}
onChange={(e) => setSelectedProduct(e.target.value)}
className="update-product-selector"
>
<option value="">Select a product</option>
{products.map((product) => (
<option key={product._id} value={product._id}>
{product.name}
</option>
))}
</select>
</div>
{selectedProduct && (
<>
<div className="updateproduct-itemfield">
<p>Product Title</p>
<input
type="text"
name="name"
value={productDetails.name}
onChange={(e) => changeHandler(e)}
placeholder="Type here"
/>
</div>
<div className="updateproduct-price">
<div className="updateproduct-itemfield">
<p>Price</p>
<input
type="text"
name="old_price"
value={productDetails.old_price}
onChange={(e) => changeHandler(e)}
placeholder="Type here"
/>
</div>
<div className="updateproduct-itemfield">
<p>Offer Price</p>
<input
type="text"
name="new_price"
value={productDetails.new_price}
onChange={(e) => changeHandler(e)}
placeholder="Type here"
/>
</div>
</div>
<div className="updateproduct-itemfield">
<p>Product Category</p>
<select
value={productDetails.category}
name="category"
className="update-product-selector"
onChange={changeHandler}
>
<option value="kitchenessentials">Kitchen Essentials</option>
<option value="breakfastandsnacks">Breakfast & Snacks</option>
<option value="combo">Combo</option>
</select>
</div>
<div className="updateproduct-itemfield">
<p>Product Image</p>
<label htmlFor="file-input">
<img
className="updateproduct-thumbnail-img"
src={!image ? productDetails.image || upload_area :
URL.createObjectURL(image)}
alt=""
/>
</label>
<input
onChange={(e) => imageHandler(e)}
type="file"
name="image"
id="file-input"
hidden
/>
</div>