| 1 |
import { InspectorControls, useBlockProps } from '@wordpress/block-editor'; |
| 2 |
import { |
| 3 |
PanelBody, |
| 4 |
RadioControl, |
| 5 |
SelectControl, |
| 6 |
Spinner, |
| 7 |
Placeholder, |
| 8 |
} from '@wordpress/components'; |
| 9 |
import { Fragment, useEffect, useState, useCallback } from '@wordpress/element'; |
| 10 |
import apiFetch from '@wordpress/api-fetch'; |
| 11 |
import { __ } from '@wordpress/i18n'; |
| 12 |
|
| 13 |
const FacetBlockEdit = (props) => { |
| 14 |
const { attributes, setAttributes } = props; |
| 15 |
const [taxonomies, setTaxonomies] = useState({}); |
| 16 |
const [preview, setPreview] = useState(''); |
| 17 |
const [loading, setLoading] = useState(false); |
| 18 |
const { facet, orderby, order } = attributes; |
| 19 |
|
| 20 |
const blockProps = useBlockProps(); |
| 21 |
|
| 22 |
const load = useCallback(async () => { |
| 23 |
const taxonomies = await apiFetch({ |
| 24 |
path: '/elasticpress/v1/facets/taxonomies', |
| 25 |
}); |
| 26 |
setTaxonomies(taxonomies); |
| 27 |
}, [setTaxonomies]); |
| 28 |
|
| 29 |
useEffect(load, [load]); |
| 30 |
|
| 31 |
useEffect(() => { |
| 32 |
setLoading(true); |
| 33 |
const params = new URLSearchParams({ |
| 34 |
facet, |
| 35 |
orderby, |
| 36 |
order, |
| 37 |
}); |
| 38 |
apiFetch({ |
| 39 |
path: `/elasticpress/v1/facets/block-preview?${params}`, |
| 40 |
}) |
| 41 |
.then((preview) => setPreview(preview)) |
| 42 |
.finally(() => setLoading(false)); |
| 43 |
}, [facet, orderby, order]); |
| 44 |
|
| 45 |
return ( |
| 46 |
<Fragment> |
| 47 |
<InspectorControls> |
| 48 |
<PanelBody title={__('Facet Settings', 'elasticpress')}> |
| 49 |
<SelectControl |
| 50 |
label={__('Taxonomy', 'elasticpress')} |
| 51 |
value={facet} |
| 52 |
options={[ |
| 53 |
...Object.entries(taxonomies).map(([slug, taxonomy]) => ({ |
| 54 |
label: taxonomy.label, |
| 55 |
value: slug, |
| 56 |
})), |
| 57 |
]} |
| 58 |
onChange={(value) => setAttributes({ facet: value })} |
| 59 |
/> |
| 60 |
<RadioControl |
| 61 |
label={__('Order By', 'elasticpress')} |
| 62 |
help={__('The field used to order available options', 'elasticpress')} |
| 63 |
selected={orderby} |
| 64 |
options={[ |
| 65 |
{ label: __('Count', 'elasticpress'), value: 'count' }, |
| 66 |
{ label: __('Name', 'elasticpress'), value: 'name' }, |
| 67 |
]} |
| 68 |
onChange={(value) => setAttributes({ orderby: value })} |
| 69 |
/> |
| 70 |
<RadioControl |
| 71 |
label={__('Order', 'elasticpress')} |
| 72 |
selected={order} |
| 73 |
options={[ |
| 74 |
{ label: __('ASC', 'elasticpress'), value: 'asc' }, |
| 75 |
{ label: __('DESC', 'elasticpress'), value: 'desc' }, |
| 76 |
]} |
| 77 |
onChange={(value) => setAttributes({ order: value })} |
| 78 |
/> |
| 79 |
</PanelBody> |
| 80 |
</InspectorControls> |
| 81 |
|
| 82 |
<div {...blockProps}> |
| 83 |
{loading && ( |
| 84 |
<Placeholder> |
| 85 |
<Spinner /> |
| 86 |
</Placeholder> |
| 87 |
)} |
| 88 |
{/* eslint-disable-next-line react/no-danger */} |
| 89 |
{!loading && <div dangerouslySetInnerHTML={{ __html: preview }} />} |
| 90 |
</div> |
| 91 |
</Fragment> |
| 92 |
); |
| 93 |
}; |
| 94 |
export default FacetBlockEdit; |
| 95 |
|