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";
import { Button } from "@mui/material";
const AddMultiplePd = () => {
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/v1/principaldistributor/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);
}
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 Principal 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: "Principal Distributors and Addresses added successfully.",
icon: "success",
buttons: "OK",
});
} else {
toast.success("File processed successfully with no new entries.");
navigate("/principal-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 Principal Distributor: ${
error.response?.data?.message || "An unexpected error occurred"
}`,
"error"
);
} finally {
setLoading(false);
}
};
return (
Add Multiple Principal Distributor
{errors.length > 0 && (
Finding errors while adding the Principal Distributor.
Principal Distributor Name |
Email |
Phone |
PAN |
GST |
Message |
{errors.map((error, index) => (
{error.name || "N/A"} |
{error.email || "N/A"} |
{error.phone || "N/A"} |
{error.panNumber || "N/A"} |
{error.gstNumber || "N/A"} |
{error.message} |
))}
)}
{updatedDistributors.length > 0 && (
Updated Principal Distributors
Principal Distributor Name |
Email |
Phone |
Message |
{updatedDistributors.map((distributor, index) => (
{distributor.name || "N/A"} |
{distributor.email || "N/A"} |
{distributor.phone || "N/A"} |
{distributor.updatedFields} |
))}
)}
{newlyCreated.length > 0 && (
Newly Created Principal Distributors:
uniqueId |
Principal Distributor Name |
Email |
Phone |
PAN |
GST |
{newlyCreated.map((distributor, index) => (
{distributor?.distributor?.uniqueId || "N/A"} |
{distributor?.distributor?.name || "N/A"} |
{distributor?.distributor?.email || "N/A"} |
{distributor?.distributor?.phone || "N/A"} |
{distributor?.address?.panNumber || "N/A"} |
{distributor?.address?.gstNumber || "N/A"} |
))}
)}
);
};
export default AddMultiplePd;