| 1 |
/* eslint-disable no-nested-ternary */ |
| 2 |
/** |
| 3 |
* WordPress dependencies. |
| 4 |
*/ |
| 5 |
import { getBlockType } from '@wordpress/blocks'; |
| 6 |
import { InspectorControls, useBlockProps, Warning } from '@wordpress/block-editor'; |
| 7 |
import { |
| 8 |
Disabled, |
| 9 |
PanelBody, |
| 10 |
Flex, |
| 11 |
FlexItem, |
| 12 |
Placeholder, |
| 13 |
TextControl, |
| 14 |
} from '@wordpress/components'; |
| 15 |
import { useSelect } from '@wordpress/data'; |
| 16 |
import { WPElement } from '@wordpress/element'; |
| 17 |
import { __, sprintf } from '@wordpress/i18n'; |
| 18 |
|
| 19 |
/** |
| 20 |
* Internal dependencies. |
| 21 |
*/ |
| 22 |
import icon from './icon'; |
| 23 |
import LoadingResponsePlaceholder from '../common/components/loading-response-placeholder'; |
| 24 |
import FacetMetaControl from '../common/components/facet-meta-control'; |
| 25 |
import RangeFilter from '../common/components/range-filter'; |
| 26 |
|
| 27 |
/** |
| 28 |
* Facet by Meta Range block edit component. |
| 29 |
* |
| 30 |
* @param {object} props Components props. |
| 31 |
* @param {object} props.attributes Block attributes. |
| 32 |
* @param {string} props.name Block name. |
| 33 |
* @param {Function} props.setAttributes Block attribute setter. |
| 34 |
* @returns {WPElement} Component element. |
| 35 |
*/ |
| 36 |
export default ({ attributes, name, setAttributes }) => { |
| 37 |
const { facet, prefix, suffix } = attributes; |
| 38 |
|
| 39 |
const { title } = getBlockType(name); |
| 40 |
|
| 41 |
const blockProps = useBlockProps(); |
| 42 |
|
| 43 |
const { |
| 44 |
min = false, |
| 45 |
max = false, |
| 46 |
isLoading = false, |
| 47 |
} = useSelect( |
| 48 |
(select) => { |
| 49 |
const range = select('elasticpress').getMetaRange(facet) || { isLoading: true }; |
| 50 |
|
| 51 |
return range; |
| 52 |
}, |
| 53 |
[facet], |
| 54 |
); |
| 55 |
|
| 56 |
/** |
| 57 |
* Facet change handler. |
| 58 |
* |
| 59 |
* @param {string} facet Selected facet. |
| 60 |
* @returns {void} |
| 61 |
*/ |
| 62 |
const onChangeFacet = (facet) => { |
| 63 |
setAttributes({ facet }); |
| 64 |
}; |
| 65 |
|
| 66 |
/** |
| 67 |
* Prefix change handler. |
| 68 |
* |
| 69 |
* @param {string} prefix Prefix value. |
| 70 |
* @returns {void} |
| 71 |
*/ |
| 72 |
const onChangePrefix = (prefix) => { |
| 73 |
setAttributes({ prefix }); |
| 74 |
}; |
| 75 |
|
| 76 |
/** |
| 77 |
* Suffix change handler. |
| 78 |
* |
| 79 |
* @param {string} suffix Suffix value. |
| 80 |
* @returns {void} |
| 81 |
*/ |
| 82 |
const onChangeSuffix = (suffix) => { |
| 83 |
setAttributes({ suffix }); |
| 84 |
}; |
| 85 |
|
| 86 |
return ( |
| 87 |
<> |
| 88 |
<InspectorControls> |
| 89 |
<PanelBody title={__('Settings', 'elasticpress')}> |
| 90 |
<FacetMetaControl onChange={onChangeFacet} value={facet} /> |
| 91 |
<Flex> |
| 92 |
<FlexItem> |
| 93 |
<TextControl |
| 94 |
label={__('Value prefix', 'elasticpress')} |
| 95 |
onChange={onChangePrefix} |
| 96 |
value={prefix} |
| 97 |
/> |
| 98 |
</FlexItem> |
| 99 |
<FlexItem> |
| 100 |
<TextControl |
| 101 |
label={__('Value suffix', 'elasticpress')} |
| 102 |
onChange={onChangeSuffix} |
| 103 |
value={suffix} |
| 104 |
/> |
| 105 |
</FlexItem> |
| 106 |
</Flex> |
| 107 |
</PanelBody> |
| 108 |
</InspectorControls> |
| 109 |
<div {...blockProps}> |
| 110 |
{facet ? ( |
| 111 |
isLoading ? ( |
| 112 |
<LoadingResponsePlaceholder /> |
| 113 |
) : min !== false && max !== false ? ( |
| 114 |
<Disabled> |
| 115 |
<RangeFilter |
| 116 |
min={min} |
| 117 |
max={max} |
| 118 |
prefix={prefix} |
| 119 |
suffix={suffix} |
| 120 |
value={[min, max]} |
| 121 |
/> |
| 122 |
</Disabled> |
| 123 |
) : ( |
| 124 |
<Warning> |
| 125 |
{sprintf( |
| 126 |
/* translators: %s: Field name. */ |
| 127 |
__( |
| 128 |
'Preview unavailable. The "%s" field does not appear to contain numeric values. Select a new meta field key or populate the field with numeric values to enable filtering by range.', |
| 129 |
'elasticpress', |
| 130 |
), |
| 131 |
facet, |
| 132 |
)} |
| 133 |
</Warning> |
| 134 |
) |
| 135 |
) : ( |
| 136 |
<Placeholder icon={icon} label={title}> |
| 137 |
<FacetMetaControl onChange={onChangeFacet} value={facet} /> |
| 138 |
</Placeholder> |
| 139 |
)} |
| 140 |
</div> |
| 141 |
</> |
| 142 |
); |
| 143 |
}; |
| 144 |
|