import React, { useState, useEffect, useRef, useCallback } from "react"; import { Link } from "react-router-dom"; import axios from "axios"; import { isAutheticated } from "src/auth"; import swal from "sweetalert"; import debounce from "lodash.debounce"; const Inventory = () => { const token = isAutheticated(); const [loading, setLoading] = useState(false); const [inventoryData, setInventoryData] = useState([]); const nameRef = useRef(); const startDateRef = useRef(); const endDateRef = useRef(); const [currentPage, setCurrentPage] = useState(1); const [itemPerPage, setItemPerPage] = useState(10); const [totalData, setTotalData] = useState(0); const getInventoryData = async () => { setLoading(true); try { const response = await axios.get(`api/inventory/all`, { headers: { Authorization: `Bearer ${token}`, }, params: { page: currentPage, show: itemPerPage, startDate: startDateRef.current?.value || "", endDate: endDateRef.current?.value || "", name: nameRef.current?.value || "", }, }); const transformedData = response.data?.inventories?.map((entry) => ({ id: entry._id, uniqueId: entry.uniqueId, tradeName: entry.addedForData?.shippingAddress?.tradeName || entry.addedForData?.trade_name || "N/A", designation: entry.addedFor === "PrincipalDistributor" ? "PD" : "RD", products: entry.products.map((product) => ({ SKU: product.SKU, ProductName: product.ProductName, Sale: product.Sale, Inventory: product.Inventory, })), createdAt: entry.createdAt, updatedAt: entry.updatedAt, })) || []; setInventoryData(transformedData); setTotalData(response.data?.total_data || 0); } catch (err) { const msg = err?.response?.data?.msg || "Something went wrong!"; swal({ title: "Error", text: msg, icon: "error", button: "Retry", dangerMode: true, }); } finally { setLoading(false); } }; const debouncedSearch = useCallback( debounce(() => { setCurrentPage(1); getInventoryData(); }, 500), [] ); const handleSearchChange = () => { debouncedSearch(); }; useEffect(() => { getInventoryData(); }, [itemPerPage, currentPage]); return (
Inventory List
{loading ? ( ) : inventoryData.length > 0 ? ( inventoryData.map((entry, i) => entry.products.map((product, j) => ( {/* Only show ID, Date, Time, Trade Name, PD/RD, and Actions on the first row of each entry */} {j === 0 && ( <> )} {/* Product details */} {/* Actions: only show on the first row of each entry */} {j === 0 && ( )} )) ) ) : ( )}
ID Date Time Trade Name PD/RD Product SKU Product Name Sale Inventory Actions
Loading...
{entry.uniqueId} {new Date(entry.createdAt).toLocaleString( "en-IN", { month: "short", day: "numeric", year: "numeric", } )} {new Date(entry.createdAt).toLocaleString( "en-IN", { hour: "numeric", minute: "numeric", hour12: true, } )} {entry.tradeName} {entry.designation} {product.SKU} {product.ProductName} {product.Sale} {product.Inventory}
No Data Available...
Showing {currentPage * itemPerPage - itemPerPage + 1} to{" "} {Math.min(currentPage * itemPerPage, totalData)} of{" "} {totalData} entries
  • setCurrentPage((prev) => prev - 1)} disabled={loading} > Previous
  • {!(currentPage - 1 < 1) && (
  • setCurrentPage((prev) => prev - 1) } disabled={loading} > {currentPage - 1}
  • )}
  • {currentPage}
  • {!( (currentPage + 1) * itemPerPage - itemPerPage > totalData - 1 ) && (
  • { setCurrentPage((prev) => prev + 1); }} disabled={loading} > {currentPage + 1}
  • )}
  • totalData - 1 ) ? "paginate_button page-item next" : "paginate_button page-item next disabled" } > setCurrentPage((prev) => prev + 1)} disabled={loading} > Next
); }; export default Inventory;