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) => (
Loading products...