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