import React, { useState } from "react"; import axios from "axios"; import swal from "sweetalert"; import { isAutheticated } from "src/auth"; import { useNavigate } from "react-router-dom"; import { toast } from "react-hot-toast"; const AddMultiplerd = () => { const [file, setFile] = useState(null); const [loading, setLoading] = useState(false); const [errors, setErrors] = useState([]); const [newlyCreated, setNewlyCreated] = useState([]); const [updatedDistributors, setUpdatedDistributors] = useState([]); const navigate = useNavigate(); const token = isAutheticated(); 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 (e) => { e.preventDefault(); if (!file) { toast.error("Please select a file to upload"); return; } // console.log(file); // console.log(token); setLoading(true); setErrors([]); setNewlyCreated([]); setUpdatedDistributors([]); try { const formData = new FormData(); formData.append("file", file); const { data } = await axios.post( "/api/retaildistributor/upload", formData, { headers: { "Content-Type": "multipart/form-data", Authorization: `Bearer ${token}`, }, } ); // console.log(data); if (data.errors && data.errors.length > 0) { setErrors(data.errors); } if (data.newlyCreated && data.newlyCreated.length > 0) { setNewlyCreated(data.newlyCreated); // console.log(data.newlyCreated); } if (data.updatedDistributors && data.updatedDistributors.length > 0) { setUpdatedDistributors(data.updatedDistributors); // console.log(data.updatedDistributors); } // Redirect or display success message if (data.errors && data.errors.length > 0) { setErrors(data.errors); swal({ title: "SpreadSheet Upload Successful", text: "A few Retail Distributor have errors. Please fix them and upload again.", icon: "warning", button: "OK", }); } else if ( data.newlyCreated.length > 0 || data.updatedDistributors.length > 0 ) { swal({ title: "SpreadSheet Upload Successful", text: "Retail Distributors added successfully.", icon: "success", buttons: "OK", }); } else { toast.success("File processed successfully with no new entries."); navigate("/retail-distributor"); } setFile(null); // Clear the file input document.querySelector('input[type="file"]').value = ""; } catch (error) { console.error("Upload error:", error); swal( "Error", `Failed to upload Retail Distributor: ${ error.response?.data?.message || "An unexpected error occurred" }`, "error" ); } finally { setLoading(false); } }; return (
Add Multiple Retail Distributor

Upload only .xlsx files*

{errors.length > 0 && (
Finding errors while adding the Retail Distributor.
{errors.map((error, index) => ( ))}
Retail Distributor Name Email Phone PAN GST AAdhar Message
{error.name || "N/A"} {error.email || "N/A"} {error.phone || "N/A"} {error.panNumber || "N/A"} {error.gstNumber || "N/A"} {error.AadharNumber || "N/A"} {error.message}
)} {updatedDistributors.length > 0 && (
Updated Retail Distributors
{updatedDistributors.map((distributor, index) => ( ))}
Retail Distributor Name Email Phone PAN GST Aadhar Message
{distributor.name || "N/A"} {distributor.email || "N/A"} {distributor.mobile_number || "N/A"} {distributor.pan_number || "N/A"} {distributor.gst_number || "N/A"} {distributor.aadhar_number || "N/A"} {distributor.updatedFields}
)} {newlyCreated.length > 0 && (
Newly Created Retail Distributors:
{newlyCreated.map((distributor, index) => ( ))}
Retail Distributor Name Email Phone PAN GST Aadhar
{distributor?.Kyc?.name || "N/A"} {distributor?.Kyc?.email || "N/A"} {distributor?.Kyc?.mobile_number || "N/A"} {distributor?.Kyc?.pan_number || "N/A"} {distributor?.Kyc?.gst_number || "N/A"} {distributor?.Kyc?.aadhar_number || "N/A"}
)}
); }; export default AddMultiplerd;