product and principal distributor error handling part done
This commit is contained in:
parent
7149c7cf4f
commit
6963e9d43c
@ -1,15 +1,17 @@
|
||||
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 { isAutheticated } from "src/auth";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import { toast } from "react-hot-toast";
|
||||
const AddMultiplePd = () => {
|
||||
const [file, setFile] = useState(null);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [errors, setErrors] = useState([]);
|
||||
const navigate = useNavigate(); // Initialize useNavigate
|
||||
|
||||
const [newlyCreated, setNewlyCreated] = useState([]);
|
||||
const [updatedDistributors, setUpdatedDistributors] = useState([]);
|
||||
const navigate = useNavigate();
|
||||
const token = isAutheticated();
|
||||
const handleFileChange = (e) => {
|
||||
const selectedFile = e.target.files[0];
|
||||
if (
|
||||
@ -24,29 +26,45 @@ const AddMultiplePd = () => {
|
||||
}
|
||||
};
|
||||
|
||||
const handleSubmit = async () => {
|
||||
const handleSubmit = async (e) => {
|
||||
e.preventDefault();
|
||||
if (!file) {
|
||||
swal("Error", "No file selected", "error");
|
||||
toast.error("Please select a file to upload");
|
||||
return;
|
||||
}
|
||||
|
||||
// console.log(file);
|
||||
// console.log(token);
|
||||
setLoading(true);
|
||||
setErrors([]);
|
||||
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
|
||||
setNewlyCreated([]);
|
||||
setUpdatedDistributors([]);
|
||||
try {
|
||||
const token = isAutheticated();
|
||||
const response = await axios.post('/api/v1/principaldistributor/upload', formData, {
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data',
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
const formData = new FormData();
|
||||
formData.append("file", file);
|
||||
|
||||
const { data } = response;
|
||||
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({
|
||||
@ -55,16 +73,31 @@ const AddMultiplePd = () => {
|
||||
icon: "warning",
|
||||
button: "OK",
|
||||
});
|
||||
} else if (
|
||||
data.processedUsers.newlyCreated > 0 ||
|
||||
data.processedUsers.updatedDistributors > 0
|
||||
) {
|
||||
swal({
|
||||
title: "SpreadSheet Upload Successful",
|
||||
text: "Principal Distributors and Addresses added successfully.",
|
||||
icon: "success",
|
||||
buttons: "OK",
|
||||
});
|
||||
} else {
|
||||
toast.success("Principal Distributor and Address Added Successfully");
|
||||
navigate("/principal-distributor");
|
||||
toast.success("File processed successfully with no new entries.");
|
||||
navigate("/principal-distributors");
|
||||
}
|
||||
|
||||
setFile(null); // Clear the file input
|
||||
document.querySelector('input[type="file"]').value = ""; // Reset file input value
|
||||
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");
|
||||
swal(
|
||||
"Error",
|
||||
`Failed to upload Principal Distributor: ${
|
||||
error.response?.data?.message || "An unexpected error occurred"
|
||||
}`,
|
||||
"error"
|
||||
);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
@ -74,7 +107,7 @@ const AddMultiplePd = () => {
|
||||
<div className="container mt-4">
|
||||
<div className="mb-6">
|
||||
<button
|
||||
onClick={() => navigate('/principal-distributor')}
|
||||
onClick={() => navigate("/principal-distributor")}
|
||||
className="btn btn-secondary"
|
||||
>
|
||||
Back
|
||||
@ -114,7 +147,7 @@ const AddMultiplePd = () => {
|
||||
<th>Principal Distributor Name</th>
|
||||
<th>Email</th>
|
||||
<th>Phone</th>
|
||||
<th>Pan</th>
|
||||
<th>PAN</th>
|
||||
<th>GST</th>
|
||||
<th>Message</th>
|
||||
</tr>
|
||||
@ -134,6 +167,58 @@ const AddMultiplePd = () => {
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
{updatedDistributors.length > 0 && (
|
||||
<div className="my-4">
|
||||
<h6>Updated Principal Distributors</h6>
|
||||
<table className="table table-bordered">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Principal Distributor Name</th>
|
||||
<th>Email</th>
|
||||
<th>Phone</th>
|
||||
<th>Message</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{updatedDistributors.map((distributor, index) => (
|
||||
<tr key={index}>
|
||||
<td>{distributor.name || "N/A"}</td>
|
||||
<td>{distributor.email || "N/A"}</td>
|
||||
<td>{distributor.phone || "N/A"}</td>
|
||||
<td>{distributor.updatedFields}</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
{newlyCreated.length > 0 && (
|
||||
<div className="my-4">
|
||||
<h6>Newly Created Principal Distributors:</h6>
|
||||
<table className="table table-bordered">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Principal Distributor Name</th>
|
||||
<th>Email</th>
|
||||
<th>Phone</th>
|
||||
<th>PAN</th>
|
||||
<th>GST</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{newlyCreated.map((distributor, index) => (
|
||||
<tr key={index}>
|
||||
<td>{distributor.name || "N/A"}</td>
|
||||
<td>{distributor.email || "N/A"}</td>
|
||||
<td>{distributor.phone || "N/A"}</td>
|
||||
<td>{distributor.panNumber || "N/A"}</td>
|
||||
<td>{distributor.gstNumber || "N/A"}</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
@ -23,7 +23,7 @@ const SinglePrincipalDistributorAllDetails = () => {
|
||||
},
|
||||
})
|
||||
.then((res) => {
|
||||
console.log(res.data);
|
||||
// console.log(res.data);
|
||||
setUserAllAddress(res.data?.UserShippingAddress || []);
|
||||
// toast.success(res.data.message ? res.data.message : "Address fetch!");
|
||||
|
||||
@ -235,11 +235,11 @@ const SinglePrincipalDistributorAllDetails = () => {
|
||||
<td style={{ maxWidth: "400px" }}>
|
||||
<strong>
|
||||
{address?.first_Name} {address?.last_name}
|
||||
{address.company_name
|
||||
? `${address.company_name},`
|
||||
: "No Company_Name "}
|
||||
{address.gst_number ? `${address.gst_number},` : ""}
|
||||
{address?.phone_Number},{address?.street},
|
||||
{address.tradeName
|
||||
? `${address.tradeName},`
|
||||
: "No tradeName "}
|
||||
{address.gstNumber ? `${address.gstNumber},` : ""}
|
||||
{address?.street},
|
||||
{address?.city},{address?.state},{address?.country},
|
||||
{address?.postalCode}
|
||||
</strong>
|
||||
|
@ -3,11 +3,14 @@ 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";
|
||||
|
||||
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) => {
|
||||
@ -32,11 +35,12 @@ const AddMultipleProducts = () => {
|
||||
|
||||
setLoading(true);
|
||||
setErrors([]);
|
||||
|
||||
setupdatedProducts([]);
|
||||
setnewlyCreated([]);
|
||||
try{
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
|
||||
try {
|
||||
const token = isAutheticated();
|
||||
const response = await axios.post('/api/products/upload', formData, {
|
||||
headers: {
|
||||
@ -46,7 +50,16 @@ const AddMultipleProducts = () => {
|
||||
});
|
||||
|
||||
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({
|
||||
@ -55,8 +68,17 @@ const AddMultipleProducts = () => {
|
||||
icon: "warning",
|
||||
button: "OK",
|
||||
});
|
||||
} else {
|
||||
swal("Success", "Products added successfully", "success");
|
||||
} 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
|
||||
@ -113,6 +135,7 @@ const AddMultipleProducts = () => {
|
||||
<th>Product Name</th>
|
||||
<th>Category</th>
|
||||
<th>GST</th>
|
||||
<th>price</th>
|
||||
<th>Message</th>
|
||||
</tr>
|
||||
</thead>
|
||||
@ -122,6 +145,7 @@ const AddMultipleProducts = () => {
|
||||
<td>{error.productName || "N/A"}</td>
|
||||
<td>{error.category || "N/A"}</td>
|
||||
<td>{error.GST || "N/A"}</td>
|
||||
<td>{error.price || "N/A"}</td>
|
||||
<td>{error.message}</td>
|
||||
</tr>
|
||||
))}
|
||||
@ -129,6 +153,58 @@ const AddMultipleProducts = () => {
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
{updatedProducts.length > 0 && (
|
||||
<div className="my-4">
|
||||
<h6>Updated Product Details.</h6>
|
||||
<table className="table table-bordered">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Product Name</th>
|
||||
<th>Category</th>
|
||||
<th>GST</th>
|
||||
<th>price</th>
|
||||
<th>Message</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{updatedProducts.map((update, index) => (
|
||||
<tr key={index}>
|
||||
<td>{update.name || "N/A"}</td>
|
||||
<td>{update.category || "N/A"}</td>
|
||||
<td>{update.GST || "N/A"}</td>
|
||||
<td>{update.price || "N/A"}</td>
|
||||
<td>{update.updatedFields}</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
{newlyCreated.length > 0 && (
|
||||
<div className="my-4">
|
||||
<h6>Newly Created Product Details:</h6>
|
||||
<table className="table table-bordered">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Product Name</th>
|
||||
<th>Category</th>
|
||||
<th>GST</th>
|
||||
<th>price</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{newlyCreated.map((create, index) => (
|
||||
<tr key={index}>
|
||||
<td>{create.name || "N/A"}</td>
|
||||
<td>{create.category || "N/A"}</td>
|
||||
<td>{create.GST || "N/A"}</td>
|
||||
<td>{create.price || "N/A"}</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user