/** * WordPress dependencies */ import { BaseControl, SelectControl, ToggleControl, } from '@safe-wordpress/components'; import { useInstanceId } from '@safe-wordpress/compose'; import { _x } from '@safe-wordpress/i18n'; /** * External dependencies */ import clsx from 'clsx'; import { ErrorText, PostSearcher } from '@nab/components'; import type { AllProducts, CAEditProps, ProductId, ProductSelection, SelectedProductIds, SomeProducts, } from '@nab/types'; /** * Internal dependencies */ import type { Attributes } from './types'; export const Edit = ( { attributes: attrs, setAttributes: setAttrs, errors, }: CAEditProps< Attributes > ): JSX.Element => { const { value: attributes } = attrs; const setAttributes = ( value: ProductSelection ): void => setAttrs( { type: 'product-selection', value, } ); return ( <> setAttributes( any ? SOME_PRODUCTS : ALL_PRODUCTS ) } /> { attributes.type === 'some-products' && ( setAttributes( { type: 'some-products', value } ) } errors={ errors } /> ) } ); }; // ============ // HELPER VIEWS // ============ const ProductSelectionView = ( { value, onChange, errors, }: { readonly value: SomeProducts[ 'value' ]; readonly onChange: ( value: SelectedProductIds ) => void; readonly errors: CAEditProps< Attributes >[ 'errors' ]; } ): JSX.Element | null => { const instanceId = useInstanceId( Edit ); if ( 'product-ids' !== value.type ) { return null; } return ( <> } help={ } > onChange( { ...value, productIds: newProductIds as ReadonlyArray< ProductId >, } ) } /> ); }; const Label = ( { value, onChange, }: { readonly value: SomeProducts[ 'value' ]; readonly onChange: ( value: SelectedProductIds ) => void; } ): JSX.Element | null => { if ( 'product-taxonomies' === value.type ) { return null; } const getValue = () => { if ( ! value.excluded ) { return 'included' as const; } if ( value.mode === 'and' ) { return 'excluded-product-set' as const; } return 'excluded-products' as const; }; return ( onChange( { ...value, excluded: 'included' === v ? undefined : true, mode: 'excluded-products' === v ? 'or' : 'and', } ) } /> ); }; // ========= // CONSTANTS // ========= const ALL_PRODUCTS: AllProducts = { type: 'all-products', }; const SOME_PRODUCTS: SomeProducts = { type: 'some-products', value: { type: 'product-ids', mode: 'and', productIds: [], }, };