From 6963e9d43cf005a86b184c7250929a805168d122 Mon Sep 17 00:00:00 2001 From: Sibunnayak Date: Fri, 9 Aug 2024 12:57:27 +0530 Subject: [PATCH] product and principal distributor error handling part done --- .../PrincipalDistributors/AddMultiplePD.js | 137 ++++++++++++++---- .../singlePrincipalDistributorAllDetails.js | 12 +- src/views/Products/AddMultipleProducts.js | 86 ++++++++++- 3 files changed, 198 insertions(+), 37 deletions(-) diff --git a/src/views/PrincipalDistributors/AddMultiplePD.js b/src/views/PrincipalDistributors/AddMultiplePD.js index 0b31083..02b1f73 100644 --- a/src/views/PrincipalDistributors/AddMultiplePD.js +++ b/src/views/PrincipalDistributors/AddMultiplePD.js @@ -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 = () => {
)} + {updatedDistributors.length > 0 && ( +
+
Updated Principal Distributors
+ + + + + + + + + + + {updatedDistributors.map((distributor, index) => ( + + + + + + + ))} + +
Principal Distributor NameEmailPhoneMessage
{distributor.name || "N/A"}{distributor.email || "N/A"}{distributor.phone || "N/A"}{distributor.updatedFields}
+
+ )} + {newlyCreated.length > 0 && ( +
+
Newly Created Principal Distributors:
+ + + + + + + + + + + + {newlyCreated.map((distributor, index) => ( + + + + + + + + ))} + +
Principal Distributor NameEmailPhonePANGST
{distributor.name || "N/A"}{distributor.email || "N/A"}{distributor.phone || "N/A"}{distributor.panNumber || "N/A"}{distributor.gstNumber || "N/A"}
+
+ )}
); }; diff --git a/src/views/PrincipalDistributors/singlePrincipalDistributorAllDetails.js b/src/views/PrincipalDistributors/singlePrincipalDistributorAllDetails.js index 2362db3..46fcb49 100644 --- a/src/views/PrincipalDistributors/singlePrincipalDistributorAllDetails.js +++ b/src/views/PrincipalDistributors/singlePrincipalDistributorAllDetails.js @@ -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 = () => { {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} diff --git a/src/views/Products/AddMultipleProducts.js b/src/views/Products/AddMultipleProducts.js index 1986e53..d25fcc8 100644 --- a/src/views/Products/AddMultipleProducts.js +++ b/src/views/Products/AddMultipleProducts.js @@ -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 = () => { Product Name Category GST + price Message @@ -122,6 +145,7 @@ const AddMultipleProducts = () => { {error.productName || "N/A"} {error.category || "N/A"} {error.GST || "N/A"} + {error.price || "N/A"} {error.message} ))} @@ -129,6 +153,58 @@ const AddMultipleProducts = () => { )} + {updatedProducts.length > 0 && ( +
+
Updated Product Details.
+ + + + + + + + + + + + {updatedProducts.map((update, index) => ( + + + + + + + + ))} + +
Product NameCategoryGSTpriceMessage
{update.name || "N/A"}{update.category || "N/A"}{update.GST || "N/A"}{update.price || "N/A"}{update.updatedFields}
+
+ )} + {newlyCreated.length > 0 && ( +
+
Newly Created Product Details:
+ + + + + + + + + + + {newlyCreated.map((create, index) => ( + + + + + + + ))} + +
Product NameCategoryGSTprice
{create.name || "N/A"}{create.category || "N/A"}{create.GST || "N/A"}{create.price || "N/A"}
+
+ )} ); };