import React, { useState } from "react"; import axios from "axios"; import swal from "sweetalert"; import { isAutheticated } from "src/auth.js"; import { useNavigate } from "react-router-dom"; // Import useNavigate import toast from "react-hot-toast"; import { Button } from "@mui/material"; const AddMultipleProducts = () => { const [file, setFile] = useState(null); const [loading, setLoading] = useState(false); const [errors, setErrors] = useState([]); const [updatedProducts, setupdatedProducts] = useState([]); const [newlyCreated, setnewlyCreated] = useState([]); const navigate = useNavigate(); // Initialize useNavigate const handleFileChange = (e) => { const selectedFile = e.target.files[0]; if ( selectedFile && selectedFile.type === "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ) { setFile(selectedFile); } else { swal("Error", "Please upload a valid Excel file", "error"); setFile(null); } }; const handleSubmit = async () => { if (!file) { swal("Error", "No file selected", "error"); return; } setLoading(true); setErrors([]); setupdatedProducts([]); setnewlyCreated([]); try { const formData = new FormData(); formData.append("file", file); const token = isAutheticated(); const response = await axios.post("/api/products/upload", formData, { headers: { "Content-Type": "multipart/form-data", Authorization: `Bearer ${token}`, }, }); const { data } = response; // console.log(data); if (data.errors && data.errors.length > 0) { setErrors(data.errors); } if (data.newlyCreated && data.newlyCreated.length > 0) { setnewlyCreated(data.newlyCreated); } if (data.updatedProducts && data.updatedProducts.length > 0) { setupdatedProducts(data.updatedProducts); } if (data.errors && data.errors.length > 0) { setErrors(data.errors); swal({ title: "SpreadSheet Upload Successful", text: "A few products have errors. Please fix them and upload again.", icon: "warning", button: "OK", }); } else if ( (data.updatedProducts && data.updatedProducts.length > 0) || (data.newlyCreated && data.newlyCreated.length > 0) ) { swal({ title: "SpreadSheet Upload Successful", text: "Products added successfully", icon: "success", button: "OK", }); } else { toast.success("Products added successfully"); navigate("/products"); } setFile(null); // Clear the file input document.querySelector('input[type="file"]').value = ""; // Reset file input value } catch (error) { console.error("Upload error:", error); swal( "Error", `Failed to upload products: ${ error.response?.data?.message || "An unexpected error occurred" }`, "error" ); } finally { setLoading(false); } }; return (
Add Multiple Products

Upload only .xlsx files*

{errors.length > 0 && (
Finding errors while adding the products.
{errors.map((error, index) => ( ))}
Product Name Category Brand GST HSN_Code price Message
{error.productName || "N/A"} {error.category || "N/A"} {error.brand || "N/A"} {error.GST || "N/A"} {error.HSN_Code || "N/A"} {error.price || "N/A"} {error.message}
)} {updatedProducts.length > 0 && (
Updated Product Details.
{updatedProducts.map((update, index) => ( ))}
Product Name Category Brand GST HSN_Code price Message
{update.name || "N/A"} {update.category || "N/A"} {update.brand || "N/A"} {update.GST || "N/A"} {update.HSN_Code || "N/A"} {update.price || "N/A"} {update.updatedFields}
)} {newlyCreated.length > 0 && (
Newly Created Product Details:
{newlyCreated.map((create, index) => ( ))}
Product Name Category Brand GST HSN_Code price
{create.name || "N/A"} {create.category || "N/A"} {create.brand || "N/A"} {create.GST || "N/A"} {create.HSN_Code || "N/A"} {create.price || "N/A"}
)}
); }; export default AddMultipleProducts;