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 AddMultipletm = () => {
const [file, setFile] = useState(null);
const [loading, setLoading] = useState(false);
const [errors, setErrors] = useState([]);
const [newlyCreated, setNewlyCreated] = useState([]);
const [updatedtrritoryManagers, setupdatedtrritoryManagers] = 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([]);
setupdatedtrritoryManagers([]);
try {
const formData = new FormData();
formData.append("file", file);
const { data } = await axios.post(
"https://cheminova-api1.onrender.com/api/territorymanager/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.updatedtrritoryManagers &&
data.updatedtrritoryManagers.length > 0
) {
setupdatedtrritoryManagers(data.updatedtrritoryManagers);
// console.log(data.updatedtrritoryManagers);
}
// Redirect or display success message
if (data.errors && data.errors.length > 0) {
setErrors(data.errors);
swal({
title: "SpreadSheet Upload Successful",
text: "A few Territory Manager have errors. Please fix them and upload again.",
icon: "warning",
button: "OK",
});
} else if (
data.newlyCreated.length > 0 ||
data.updatedtrritoryManagers.length > 0
) {
swal({
title: "SpreadSheet Upload Successful",
text: "Territory Managers added successfully.",
icon: "success",
buttons: "OK",
});
} else {
toast.success("File processed successfully with no new entries.");
navigate("/territorymanagers");
}
setFile(null); // Clear the file input
document.querySelector('input[type="file"]').value = "";
} catch (error) {
console.error("Upload error:", error);
swal(
"Error",
`Failed to upload Territory Managers: ${error.response?.data?.message || "An unexpected error occurred"
}`,
"error"
);
} finally {
setLoading(false);
}
};
return (
Add Multiple Territory Managers
{errors.length > 0 && (
Finding errors while adding the Territory Manager.
Employee Code |
Territory Manager Name |
Email |
Phone |
Message |
{errors.map((error, index) => (
{error.uniqueId || "N/A"} |
{error.name || "N/A"} |
{error.email || "N/A"} |
{error.phone || "N/A"} |
{error.message} |
))}
)}
{updatedtrritoryManagers.length > 0 && (
Updated Territory Managers
Employee Code |
Territory Manager Name |
Email |
Phone |
Message |
{updatedtrritoryManagers.map((TM, index) => (
{TM.uniqueId || "N/A"} |
{TM.name || "N/A"} |
{TM.email || "N/A"} |
{TM.mobileNumber || "N/A"} |
{TM.updatedFields} |
))}
)}
{newlyCreated.length > 0 && (
Newly Created Territory Managers:
Employee Code |
Territory Manager Name |
Email |
Phone |
{newlyCreated.map((TM, index) => (
{TM?.territoryManager?.uniqueId || "N/A"} |
{TM?.territoryManager?.name || "N/A"} |
{TM?.territoryManager?.email || "N/A"} |
{TM?.territoryManager?.mobileNumber || "N/A"} |
))}
)}
);
};
export default AddMultipletm;