// @ts-expect-error WordPress block types are provided at runtime (bundled as externals) import { useBlockProps, InspectorControls } from '@wordpress/block-editor'; // @ts-expect-error WordPress block types are provided at runtime (bundled as externals) import ServerSideRender from '@wordpress/server-side-render'; import { useSelect } from '@wordpress/data'; import { Button, SelectControl, PanelBody } from '@wordpress/components'; import { useCallback } from '@wordpress/element'; import type { EditProps } from './consts'; export default function Edit({ attributes, setAttributes }: EditProps) { const { formId, newPetitionLink } = attributes; const blockAtts = useBlockProps(); const fetchPetitions = useCallback(() => { return useSelect((select) => { // @ts-ignore return select('core').getEntityRecords( 'postType', 'petitioner-petition', { per_page: -1, _fields: 'title,id', } ); }, []); }, []); const allPetitions = fetchPetitions() || []; const petitionOptions = [ { label: 'Please select your petition', value: '' }, ]; allPetitions.forEach((el) => { let label = el.title.raw; const limit = 40; if (label.length > limit) { label = label.substring(0, limit) + '...'; } const objToPush = { label, value: el.id }; petitionOptions.push(objToPush); }); const FinalComponent = useCallback(() => { if (!allPetitions.length) { return (

Petitioner form

Looks like you haven't added any forms yet!

); } if (!formId) { return (

Petitioner form

Use the dropdown in the block settings to select your petition.

); } return (
); }, [allPetitions, formId, attributes, newPetitionLink]); return (
setAttributes({ formId: el })} />
); }