-
Notifications
You must be signed in to change notification settings - Fork 314
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2722 from 10up/feature/facet-block
Facet block
- Loading branch information
Showing
10 changed files
with
449 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"$schema": "https://schemas.wp.org/trunk/block.json", | ||
"apiVersion": 2, | ||
"title": "Facet (ElasticPress)", | ||
"textdomain": "elasticpress", | ||
"name": "elasticpress/facet", | ||
"icon": "feedback", | ||
"category": "widgets", | ||
"attributes": { | ||
"facet": { | ||
"type": "string", | ||
"default": "" | ||
}, | ||
"orderby": { | ||
"type" : "string", | ||
"default": "count", | ||
"enum" : [ "count", "name" ] | ||
}, | ||
"order": { | ||
"type": "string", | ||
"default": "desc", | ||
"enum": [ "desc", "asc" ] | ||
} | ||
}, | ||
"supports": { | ||
"html": false | ||
}, | ||
"editorScript": "file:/../../../../dist/js/facets-block-script.min.js", | ||
"style": "file:/../../../../dist/css/facets-styles.min.css" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { InspectorControls, useBlockProps } from '@wordpress/block-editor'; | ||
import { | ||
PanelBody, | ||
RadioControl, | ||
SelectControl, | ||
Spinner, | ||
Placeholder, | ||
} from '@wordpress/components'; | ||
import { Fragment, useEffect, useState, useCallback } from '@wordpress/element'; | ||
import apiFetch from '@wordpress/api-fetch'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
const FacetBlockEdit = (props) => { | ||
const { attributes, setAttributes } = props; | ||
const [taxonomies, setTaxonomies] = useState({}); | ||
const [preview, setPreview] = useState(''); | ||
const [loading, setLoading] = useState(false); | ||
const { facet, orderby, order } = attributes; | ||
|
||
const blockProps = useBlockProps(); | ||
|
||
const load = useCallback(async () => { | ||
const taxonomies = await apiFetch({ | ||
path: '/elasticpress/v1/facets/taxonomies', | ||
}); | ||
setTaxonomies(taxonomies); | ||
}, [setTaxonomies]); | ||
|
||
useEffect(load, [load]); | ||
|
||
useEffect(() => { | ||
setLoading(true); | ||
const params = new URLSearchParams({ | ||
facet, | ||
orderby, | ||
order, | ||
}); | ||
apiFetch({ | ||
path: `/elasticpress/v1/facets/block-preview?${params}`, | ||
}) | ||
.then((preview) => setPreview(preview)) | ||
.finally(() => setLoading(false)); | ||
}, [facet, orderby, order]); | ||
|
||
return ( | ||
<Fragment> | ||
<InspectorControls> | ||
<PanelBody title={__('Facet Settings', 'elasticpress')}> | ||
<SelectControl | ||
label={__('Taxonomy', 'elasticpress')} | ||
value={facet} | ||
options={[ | ||
...Object.entries(taxonomies).map(([slug, taxonomy]) => ({ | ||
label: taxonomy.label, | ||
value: slug, | ||
})), | ||
]} | ||
onChange={(value) => setAttributes({ facet: value })} | ||
/> | ||
<RadioControl | ||
label={__('Order By', 'elasticpress')} | ||
help={__('The field used to order available options', 'elasticpress')} | ||
selected={orderby} | ||
options={[ | ||
{ label: __('Count', 'elasticpress'), value: 'count' }, | ||
{ label: __('Name', 'elasticpress'), value: 'name' }, | ||
]} | ||
onChange={(value) => setAttributes({ orderby: value })} | ||
/> | ||
<RadioControl | ||
label={__('Order', 'elasticpress')} | ||
selected={order} | ||
options={[ | ||
{ label: __('ASC', 'elasticpress'), value: 'asc' }, | ||
{ label: __('DESC', 'elasticpress'), value: 'desc' }, | ||
]} | ||
onChange={(value) => setAttributes({ order: value })} | ||
/> | ||
</PanelBody> | ||
</InspectorControls> | ||
|
||
<div {...blockProps}> | ||
{loading && ( | ||
<Placeholder> | ||
<Spinner /> | ||
</Placeholder> | ||
)} | ||
{/* eslint-disable-next-line react/no-danger */} | ||
{!loading && <div dangerouslySetInnerHTML={{ __html: preview }} />} | ||
</div> | ||
</Fragment> | ||
); | ||
}; | ||
export default FacetBlockEdit; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import edit from './edit'; | ||
import block from './block.json'; | ||
|
||
const { registerBlockType } = wp.blocks; | ||
|
||
registerBlockType(block, { | ||
edit, | ||
save: () => {}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.