/** * WordPress dependencies */ import { TextControl, SelectControl, BaseControl, } from '@safe-wordpress/components'; import { useInstanceId } from '@safe-wordpress/compose'; import { useSelect } from '@safe-wordpress/data'; import { useEffect, useMemo } from '@safe-wordpress/element'; import { _x } from '@safe-wordpress/i18n'; /** * External dependencies */ import clsx from 'clsx'; import { sortBy } from 'lodash'; import { ConversionActionScope, ErrorText, PostSearcher, } from '@nab/components'; import { store as NAB_DATA } from '@nab/data'; import { EMPTY_ARRAY } from '@nab/utils'; import type { CAEditProps } from '@nab/types'; /** * Internal dependencies */ import { getNameFieldLabel, isNamedForm } from './helpers'; import type { Attributes } from './types'; export const Edit = ( { attributes, scope, errors, setAttributes, setScope, }: CAEditProps< Attributes > ): JSX.Element => { const { formId, formType, formName = '' } = attributes; const instanceId = useInstanceId( Edit ); const formTypes = useFormTypes(); useEffect( () => { if ( ! formType && formTypes[ 0 ]?.value ) { setAttributes( { formType: formTypes[ 0 ].value, formId: undefined, formName: undefined, } ); } }, [ formType, formTypes, setAttributes ] ); return ( <> { formTypes.length > 1 && ( setAttributes( { formType: newFormType, formId: undefined, formName: undefined, } ) } /> ) } { isNamedForm( attributes ) ? ( } value={ formName } onChange={ ( newFormName ) => setAttributes( { formType, formId: 0, formName: newFormName, } ) } /> ) : ( } > setAttributes( { formType, formId: newFormId, formName: undefined, } ) } /> ) } ); }; // ===== // HOOKS // ===== const useFormTypes = () => { const entities = useSelect( ( select ) => select( NAB_DATA ).getKindEntities() || EMPTY_ARRAY, [] ); return useMemo( () => { const formTypes = entities .filter( ( pt ) => 'form' === pt.kind ) .map( ( pt ) => ( { value: pt.name, label: pt.labels.singular_name, } ) ); return sortBy( formTypes, 'label' ); }, [ entities ] ); };