import { CheckboxControl, ComboboxControl, Flex, FormTokenField, TextControl, } from '@wordpress/components'; import { useEffect, useState } from '@wordpress/element'; import PropTypes from 'prop-types'; import { debounce } from 'lodash'; import { nextApiURL } from '../data/nextApiURL'; import { vfAPI } from '../shared/vfAPI'; import type { Product } from '../catalog/products/Product'; import type { Option } from '../types'; export const ProductDetails = (props) => { const [product, setProduct] = useState(); const [options, setOptions] = useState([]); const [filterValue, FilterValue] = useState(); const getManufacturers = () => { const params: { q?: string } = {}; if (filterValue) { params.q = filterValue; } const config = { params, }; const url = `${nextApiURL}/admin/manufacturers`; vfAPI.get(url, config).then((response) => { if (response.data.manufacturers) { setOptions( response.data.manufacturers.data.map((mfr) => { return { value: mfr.id, label: mfr.name, }; }) ); } }); }; const handleChange = (key: string, value: unknown) => { const updatedProduct = { ...product }; updatedProduct[key] = value; setProduct(updatedProduct); props.handleChange(key, value); }; const convertToTokens = (input: string): Array => { if (input?.length) { return input.split(','); } return []; }; const handleFilterValueChange = debounce((value: string) => { FilterValue(value); }, 500); useEffect(() => { setProduct(JSON.parse(props.product)); }, [props]); useEffect(() => { if (product?.manufacturer) { setOptions([ { value: product.manufacturer.id, label: product.manufacturer.name, }, ]); } }, []); useEffect(() => { getManufacturers(); }, [filterValue]); return ( <> {product && ( <> handleChange('brand_name', value)} /> { handleChange('manufacturer_id', value); }} options={options} onFilterValueChange={handleFilterValueChange} /> { handleChange('keywords', value.join(',')); }} /> handleChange('green', value)} /> {product.green && ( handleChange('green_attributes', value) } /> )} )} ); }; ProductDetails.propTypes = { product: PropTypes.string, handleChange: PropTypes.func, };