Skip to content

Commit

Permalink
Merge pull request #2722 from 10up/feature/facet-block
Browse files Browse the repository at this point in the history
Facet block
  • Loading branch information
felipeelia authored May 12, 2022
2 parents f1ed382 + c2acb94 commit 080fca9
Show file tree
Hide file tree
Showing 10 changed files with 449 additions and 78 deletions.
130 changes: 67 additions & 63 deletions assets/css/facets.css
Original file line number Diff line number Diff line change
@@ -1,62 +1,70 @@
.widget_ep-facet input[type="search"] {
margin-bottom: 1rem;
}

.widget_ep-facet .searchable .inner {
max-height: 20em;
overflow: scroll;
}

.widget_ep-facet .term.hide {
display: none;
}

.widget_ep-facet .empty-term {
opacity: 0.5;
position: relative;
}

.widget_ep-facet .empty-term::after {
bottom: 0;
content: " ";
display: block;
left: 0;
position: absolute;
right: 0;
top: 0;
width: 100%;
z-index: 2;
}

.widget_ep-facet .level-1 {
padding-left: 20px;
}

.widget_ep-facet .level-2 {
padding-left: 40px;
}

.widget_ep-facet .level-3 {
padding-left: 60px;
}

.widget_ep-facet .level-4 {
padding-left: 80px;
}

.widget_ep-facet .level-5 {
padding-left: 100px;
}

.widget_ep-facet input[disabled] {
cursor: pointer;
opacity: 1;
}

.widget_ep-facet .term a {
align-items: center;
display: flex;
position: relative;
.widget_ep-facet,
.wp-block-elasticpress-facet {

& input[type="search"] {
margin-bottom: 1rem;
}

& .searchable .inner {
max-height: 20em;
overflow: scroll;
}

& .term.hide {
display: none;
}

& .empty-term {
opacity: 0.5;
position: relative;
}

& .empty-term::after {
bottom: 0;
content: " ";
display: block;
left: 0;
position: absolute;
right: 0;
top: 0;
width: 100%;
z-index: 2;
}

& .level-1 {
padding-left: 20px;
}

& .level-2 {
padding-left: 40px;
}

& .level-3 {
padding-left: 60px;
}

& .level-4 {
padding-left: 80px;
}

& .level-5 {
padding-left: 100px;
}

& input[disabled] {
cursor: pointer;
opacity: 1;
}

& .term a {
align-items: center;
display: flex;
position: relative;
}

& .term a:hover .ep-checkbox {
background-color: #ccc;
}
}

.ep-checkbox {
Expand Down Expand Up @@ -87,7 +95,3 @@
.ep-checkbox.checked::after {
display: block;
}

.widget_ep-facet .term a:hover .ep-checkbox {
background-color: #ccc;
}
30 changes: 30 additions & 0 deletions assets/js/blocks/facets/block.json
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"
}
94 changes: 94 additions & 0 deletions assets/js/blocks/facets/edit.js
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;
9 changes: 9 additions & 0 deletions assets/js/blocks/facets/index.js
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: () => {},
});
2 changes: 1 addition & 1 deletion assets/js/facets.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const handleFacetSearch = (event, facetTerms) => {
/**
* Filter facet choices to match the search field term
*/
const facets = document.querySelectorAll('.widget_ep-facet');
const facets = document.querySelectorAll('.widget_ep-facet, .wp-block-elasticpress-facet');

facets.forEach((facet) => {
const facetSearchInput = facet.querySelector('.facet-search');
Expand Down
Loading

0 comments on commit 080fca9

Please sign in to comment.