import React, { useState, useEffect, useRef } from "react"; import { Link, useParams } from "react-router-dom"; import Select from "react-select"; import { all_routes } from "../../Router/all_routes"; import { DatePicker } from "antd"; import Addunits from "../../core/modals/inventory/addunits"; import AddCategory from "../../core/modals/inventory/addcategory"; import AddBrand from "../../core/modals/addbrand"; import { ArrowLeft, Calendar, ChevronDown, ChevronUp, Info, LifeBuoy, List, PlusCircle, Trash2, X, } from "feather-icons-react/build/IconComponents"; import { useDispatch, useSelector } from "react-redux"; import { setToogleHeader } from "../../core/redux/action"; import { fetchProduct } from "../../core/redux/actions/productActions"; import { OverlayTrigger, Tooltip } from "react-bootstrap"; import ImageWithBasePath from "../../core/img/imagewithbasebath"; const EditProduct = () => { const route = all_routes; const dispatch = useDispatch(); const { id } = useParams(); // Get product ID from URL const data = useSelector((state) => state.toggle_header); // Get product data from Redux store const { currentProduct, productLoading, productError } = useSelector((state) => state.products || {}); // Track if we've already fetched this product const fetchedProductId = useRef(null); // Form state for editing const [formData, setFormData] = useState({ name: '', slug: '', sku: '', description: '', price: '', category: null, // Change to null for Select component brand: null, // Change to null for Select component quantity: '', unit: null, // Change to null for Select component status: '', images: [] }); // Load product data if ID is provided and product not already loaded useEffect(() => { if (id && fetchedProductId.current !== id) { console.log('Fetching product details for ID:', id); fetchedProductId.current = id; dispatch(fetchProduct(id)); } }, [id, dispatch]); // Helper function to find option by value or label const findSelectOption = (options, value) => { if (!value) return null; return options.find(option => option.value === value || option.label === value || option.value.toLowerCase() === value.toLowerCase() || option.label.toLowerCase() === value.toLowerCase() ) || null; }; // Update form data when currentProduct changes useEffect(() => { if (currentProduct) { console.log('Product data loaded:', currentProduct); // Find matching options for select fields const categoryOption = findSelectOption(category, currentProduct.category || currentProduct.categoryName); const brandOption = findSelectOption(brand, currentProduct.brand || currentProduct.brandName); const unitOption = findSelectOption(unit, currentProduct.unit); setFormData({ name: currentProduct.name || currentProduct.productName || '', slug: currentProduct.slug || '', sku: currentProduct.sku || currentProduct.code || '', description: currentProduct.description || '', price: currentProduct.price || currentProduct.salePrice || '', category: categoryOption, brand: brandOption, quantity: currentProduct.quantity || currentProduct.stock || '', unit: unitOption, status: currentProduct.status || 'active', images: currentProduct.images || [] }); console.log('Form data updated:', { category: categoryOption, brand: brandOption, unit: unitOption }); } }, [currentProduct]); const [selectedDate, setSelectedDate] = useState(new Date()); const handleDateChange = (date) => { setSelectedDate(date); }; const [selectedDate1, setSelectedDate1] = useState(new Date()); const handleDateChange1 = (date) => { setSelectedDate1(date); }; const renderCollapseTooltip = (props) => ( Collapse ); const store = [ { value: "choose", label: "Choose" }, { value: "thomas", label: "Thomas" }, { value: "rasmussen", label: "Rasmussen" }, { value: "fredJohn", label: "Fred John" }, ]; const warehouse = [ { value: "choose", label: "Choose" }, { value: "legendary", label: "Legendary" }, { value: "determined", label: "Determined" }, { value: "sincere", label: "Sincere" }, ]; const category = [ { value: "choose", label: "Choose" }, { value: "lenovo", label: "Lenovo" }, { value: "electronics", label: "Electronics" }, { value: "laptop", label: "Laptop" }, { value: "computer", label: "Computer" }, { value: "mobile", label: "Mobile" }, ]; const subcategory = [ { value: "choose", label: "Choose" }, { value: "lenovo", label: "Lenovo" }, { value: "electronics", label: "Electronics" }, ]; const subsubcategories = [ { value: "Fruits", label: "Fruits" }, { value: "Computer", label: "Computer" }, { value: "Shoes", label: "Shoes" }, ]; const brand = [ { value: "choose", label: "Choose" }, { value: "nike", label: "Nike" }, { value: "bolt", label: "Bolt" }, ]; const unit = [ { value: "choose", label: "Choose" }, { value: "kg", label: "Kg" }, { value: "pc", label: "Pc" }, { value: "pcs", label: "Pcs" }, { value: "piece", label: "Piece" }, { value: "gram", label: "Gram" }, { value: "liter", label: "Liter" }, { value: "meter", label: "Meter" }, ]; const sellingtype = [ { value: "choose", label: "Choose" }, { value: "transactionalSelling", label: "Transactional selling" }, { value: "solutionSelling", label: "Solution selling" }, ]; const barcodesymbol = [ { value: "choose", label: "Choose" }, { value: "code34", label: "Code34" }, { value: "code35", label: "Code35" }, { value: "code36", label: "Code36" }, ]; const taxtype = [ { value: "exclusive", label: "Exclusive" }, { value: "salesTax", label: "Sales Tax" }, ]; const discounttype = [ { value: "choose", label: "Choose" }, { value: "percentage", label: "Percentage" }, { value: "cash", label: "Cash" }, ]; const discounttype1 = [ { value: "choose", label: "Choose" }, { value: "percentage", label: "Percentage" }, { value: "cash", label: "Cash" }, ]; const [isImageVisible, setIsImageVisible] = useState(true); const handleRemoveProduct = () => { setIsImageVisible(false); }; const [isImageVisible1, setIsImageVisible1] = useState(true); const handleRemoveProduct1 = () => { setIsImageVisible1(false); }; // Show loading state if (productLoading) { return (
Loading...

Loading product details...

); } // Show error state if (productError) { return (
Error: {productError}
Back to Product List
); } // Show not found state if (id && !currentProduct) { return (
Product not found! The product you're trying to edit doesn't exist.
Back to Product List
); } return (

Edit Product {currentProduct?.name ? `- ${currentProduct.name}` : ''}

  • Back to Product
  • { dispatch(setToogleHeader(!data)); }} >
{/* /add */}
Product Information
{ setFormData(prev => ({ ...prev, name: e.target.value })); }} />
{ setFormData(prev => ({ ...prev, slug: e.target.value })); }} />
{ setFormData(prev => ({ ...prev, sku: e.target.value })); }} /> Generate Code
Add New
{ setFormData(prev => ({ ...prev, brand: selectedOption })); }} isClearable />
Add New
Generate Code
{/* Editor */}