import { Box, ChevronUp, Edit, Eye, Filter, GitMerge, PlusCircle, RotateCcw, Sliders, StopCircle, Trash2, } from "feather-icons-react/build/IconComponents"; import React, { useState, useEffect } from "react"; import { useDispatch, useSelector } from "react-redux"; import { Link } from "react-router-dom"; import Select from "react-select"; import ImageWithBasePath from "../../core/img/imagewithbasebath"; import Brand from "../../core/modals/inventory/brand"; import withReactContent from "sweetalert2-react-content"; import Swal from "sweetalert2"; import { all_routes } from "../../Router/all_routes"; import { OverlayTrigger, Tooltip } from "react-bootstrap"; import Table from "../../core/pagination/datatable"; import { setToogleHeader } from "../../core/redux/action"; import { Download } from "react-feather"; import { fetchProducts, fetchProduct, deleteProduct, clearProductError } from "../../core/redux/actions/productActions"; const ProductList = () => { // Use new Redux structure for API data, fallback to legacy for existing functionality const { products: apiProducts, loading, error } = useSelector((state) => state.products); // Fallback to legacy data if API data is not available const legacyProducts = useSelector((state) => state.legacy?.product_list || []); const dataSource = apiProducts.length > 0 ? apiProducts : legacyProducts; const dispatch = useDispatch(); const data = useSelector((state) => state.legacy?.toggle_header || false); const [isFilterVisible, setIsFilterVisible] = useState(false); const [searchTerm, setSearchTerm] = useState(""); const toggleFilterVisibility = () => { setIsFilterVisible((prevVisibility) => !prevVisibility); }; const route = all_routes; // Fetch products on component mount useEffect(() => { const loadProducts = async () => { try { await dispatch(fetchProducts()); // Only fetch products - categories/brands may be included in response // or can be extracted from products data } catch (error) { console.error('Failed to load products:', error); } }; loadProducts(); }, [dispatch]); // Handle product deletion const handleDeleteProduct = async (productId) => { try { await dispatch(deleteProduct(productId)); // Show success message MySwal.fire({ title: "Deleted!", text: "Product has been deleted successfully.", icon: "success", className: "btn btn-success", customClass: { confirmButton: "btn btn-success", }, }); } catch (error) { console.error('Failed to delete product:', error); MySwal.fire({ title: "Error!", text: "Failed to delete product. Please try again.", icon: "error", className: "btn btn-danger", customClass: { confirmButton: "btn btn-danger", }, }); } }; // Handle search const handleSearch = (e) => { const value = e.target.value; setSearchTerm(value); // You can implement debounced search here // For now, we'll just update the search term }; // Clear error when component unmounts useEffect(() => { return () => { dispatch(clearProductError()); }; }, [dispatch]); const options = [ { value: "sortByDate", label: "Sort by Date" }, { value: "140923", label: "14 09 23" }, { value: "110923", label: "11 09 23" }, ]; const productlist = [ { value: "choose", label: "Choose Product" }, { value: "lenovo", label: "Lenovo 3rd Generation" }, { value: "nike", label: "Nike Jordan" }, ]; const categorylist = [ { value: "choose", label: "Choose Category" }, { value: "laptop", label: "Laptop" }, { value: "shoe", label: "Shoe" }, ]; const subcategorylist = [ { value: "choose", label: "Choose Sub Category" }, { value: "computers", label: "Computers" }, { value: "fruits", label: "Fruits" }, ]; const brandlist = [ { value: "all", label: "All Brand" }, { value: "lenovo", label: "Lenovo" }, { value: "nike", label: "Nike" }, ]; const price = [ { value: "price", label: "Price" }, { value: "12500", label: "$12,500.00" }, { value: "13000", label: "$13,000.00" }, // Replace with your actual values ]; const columns = [ { title: "Product", dataIndex: "product", render: (text, record) => ( {text} ), sorter: (a, b) => a.product.length - b.product.length, }, { title: "SKU", dataIndex: "sku", sorter: (a, b) => a.sku.length - b.sku.length, }, { title: "Category", dataIndex: "category", sorter: (a, b) => a.category.length - b.category.length, }, { title: "Brand", dataIndex: "brand", sorter: (a, b) => a.brand.length - b.brand.length, }, { title: "Price", dataIndex: "price", sorter: (a, b) => a.price.length - b.price.length, }, { title: "Unit", dataIndex: "unit", sorter: (a, b) => a.unit.length - b.unit.length, }, { title: "Qty", dataIndex: "qty", sorter: (a, b) => a.qty.length - b.qty.length, }, { title: "Created By", dataIndex: "createdby", render: (text, record) => ( {text} ), sorter: (a, b) => a.createdby.length - b.createdby.length, }, { title: "Action", dataIndex: "action", render: (text, record) => (
{ // Pre-fetch product details for editing if (record.id || record.key) { dispatch(fetchProduct(record.id || record.key)); } }} > { e.preventDefault(); MySwal.fire({ title: "Are you sure?", text: "You won't be able to revert this!", showCancelButton: true, confirmButtonColor: "#00ff00", confirmButtonText: "Yes, delete it!", cancelButtonColor: "#ff0000", cancelButtonText: "Cancel", }).then((result) => { if (result.isConfirmed) { handleDeleteProduct(record.id || record.key); } }); }} >
), sorter: (a, b) => a.createdby.length - b.createdby.length, }, ]; const MySwal = withReactContent(Swal); // Removed showConfirmationAlert as we handle confirmation inline const renderTooltip = (props) => ( Pdf ); const renderExcelTooltip = (props) => ( Excel ); const renderPrinterTooltip = (props) => ( Printer ); const renderRefreshTooltip = (props) => ( Refresh ); const renderCollapseTooltip = (props) => ( Collapse ); return (

Product List

Manage your products
  • { e.preventDefault(); dispatch(setToogleHeader(!data)); }} >
Add New Product
Import Product
{/* /product list */}
{" "} {" "} Search{" "}
{/* /Filter */}
{loading ? (
Loading...

Loading products...

) : error ? (
Error: {error}
) : ( )} {/* /product list */} ); }; export default ProductList;