PluginProbe
ElasticPress / 4.6.1
ElasticPress v4.6.1
5.3.5 5.3.4 3.6.5 3.6.6 4.0.0 4.0.1 4.1.0 4.2.0 4.2.1 4.2.2 4.3.0 4.3.1 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.7.0 4.7.1 4.7.2 5.0.0 5.0.1 5.0.2 All 108 releases
elasticpress / assets / js / blocks / facets / meta-range / edit.js

edit.js in ElasticPress 4.6.1, at assets/js/blocks/facets/meta-range/edit.js

262 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /* eslint-disable no-nested-ternary */
2 /* global facetMetaBlock */
3
4 /**
5 * WordPress dependencies.
6 */
7 import apiFetch from '@wordpress/api-fetch';
8 import { InspectorControls, useBlockProps, Warning } from '@wordpress/block-editor';
9 import {
10 Disabled,
11 PanelBody,
12 Placeholder,
13 Spinner,
14 SelectControl,
15 TextControl,
16 } from '@wordpress/components';
17 import {
18 createInterpolateElement,
19 useEffect,
20 useMemo,
21 useState,
22 WPElement,
23 } from '@wordpress/element';
24 import { __, sprintf } from '@wordpress/i18n';
25
26 /**
27 * Internal dependencies.
28 */
29 import RangeFacet from './components/range-facet';
30
31 /**
32 * Preview loading component.
33 *
34 * @returns {WPElement} Component element.
35 */
36 const PreviewLoading = () => {
37 return (
38 <Placeholder>
39 <Spinner />
40 </Placeholder>
41 );
42 };
43
44 /**
45 * Preview component.
46 *
47 * @param {object} props Component props.
48 * @param {number} props.min Minumum value.
49 * @param {number} props.max Maximum value.
50 * @param {string} props.prefix Value prefix.
51 * @param {string} props.suffix Value suffix.
52 * @returns {WPElement} Component element.
53 */
54 const Preview = ({ min, max, prefix, suffix }) => {
55 return (
56 <Disabled>
57 <RangeFacet min={min} max={max} prefix={prefix} suffix={suffix} value={[min, max]} />
58 </Disabled>
59 );
60 };
61
62 /**
63 * Preview unavailable component.
64 *
65 * @param {object} props Component props.
66 * @param {string} props.value Selected value.
67 * @returns {WPElement} Component element.
68 */
69 const PreviewUnavailable = ({ value }) => {
70 return (
71 <Warning>
72 {sprintf(
73 /* translators: %s: Field name. */
74 __(
75 '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.',
76 'elasticpress',
77 ),
78 value,
79 )}
80 </Warning>
81 );
82 };
83
84 /**
85 * Key selection component.
86 *
87 * @param {object} props Component props.
88 * @param {Function} props.onChange Change handler.
89 * @param {object[]} props.options Select options.
90 * @param {string} props.value Selected facet.
91 * @returns {WPElement} Component element.
92 */
93 const FacetControl = ({ onChange, options, value }) => {
94 return (
95 <SelectControl
96 disabled={options.length <= 1}
97 help={createInterpolateElement(
98 __(
99 'This is the list of meta fields indexed in Elasticsearch. If your desired field does not appear in this list please try to <a>sync your content</a>',
100 'elasticpress',
101 ),
102 // eslint-disable-next-line jsx-a11y/anchor-has-content, jsx-a11y/control-has-associated-label
103 { a: <a href={facetMetaBlock.syncUrl} /> },
104 )}
105 label={__('Meta Field Key', 'elasticpress')}
106 onChange={onChange}
107 options={options}
108 value={value}
109 />
110 );
111 };
112
113 /**
114 * Block wizard component.
115 *
116 * @param {object} props Component props.
117 * @returns {WPElement} Component element.
118 */
119 const Wizard = (props) => {
120 return (
121 <Placeholder label={__('Facet by Meta Range', 'elasticpress')}>
122 <FacetControl {...props} />
123 </Placeholder>
124 );
125 };
126
127 /**
128 * Facet by Meta Range block edit component.
129 *
130 * @param {object} props Components props.
131 * @param {object} props.attributes Block attributes.
132 * @param {Function} props.setAttributes Block attribute setter.
133 * @returns {WPElement} Component element.
134 */
135 export default (props) => {
136 const { attributes, setAttributes } = props;
137 const { facet, prefix, suffix } = attributes;
138
139 const blockProps = useBlockProps();
140 const [isLoading, setIsLoading] = useState(false);
141 const [min, setMin] = useState(false);
142 const [max, setMax] = useState(false);
143 const [metaKeys, setMetaKeys] = useState([]);
144
145 /**
146 * Key options.
147 */
148 const options = useMemo(() => {
149 return [
150 {
151 label: __('Select key', 'elasticpress'),
152 value: '',
153 },
154 ...metaKeys.map((metaKey) => ({
155 label: metaKey,
156 value: metaKey,
157 })),
158 ];
159 }, [metaKeys]);
160
161 /**
162 * Change handler.
163 *
164 * @param {string} value Selected value.
165 * @returns {void}
166 */
167 const onChange = (value) => {
168 setAttributes({ facet: value });
169 };
170
171 /**
172 * Change handler.
173 *
174 * @param {string} value Selected value.
175 * @returns {void}
176 */
177 const onChangePrefix = (value) => {
178 setAttributes({ prefix: value });
179 };
180
181 /**
182 * Change handler.
183 *
184 * @param {string} value Selected value.
185 * @returns {void}
186 */
187 const onChangeSuffix = (value) => {
188 setAttributes({ suffix: value });
189 };
190
191 /**
192 * Handle changes to the selected facet.
193 */
194 const handleFacet = () => {
195 setIsLoading(true);
196
197 const params = new URLSearchParams({ facet });
198
199 apiFetch({
200 path: `/elasticpress/v1/facets/meta-range/block-preview?${params}`,
201 })
202 .then((response) => {
203 if (response.success) {
204 setMin(response.data.min);
205 setMax(response.data.max);
206 } else {
207 setMin(false);
208 setMax(false);
209 }
210 })
211 .finally(() => setIsLoading(false));
212 };
213
214 /**
215 * Handle initialization.
216 */
217 const handleInit = () => {
218 apiFetch({
219 path: '/elasticpress/v1/facets/meta-range/keys',
220 }).then(setMetaKeys);
221 };
222
223 /**
224 * Effects.
225 */
226 useEffect(handleFacet, [facet]);
227 useEffect(handleInit, []);
228
229 return (
230 <>
231 <InspectorControls>
232 <PanelBody title={__('Facet Settings', 'elasticpress')}>
233 <FacetControl onChange={onChange} options={options} value={facet} />
234 <TextControl
235 label={__('Value prefix', 'elasticpress')}
236 onChange={onChangePrefix}
237 value={prefix}
238 />
239 <TextControl
240 label={__('Value suffix', 'elasticpress')}
241 onChange={onChangeSuffix}
242 value={suffix}
243 />
244 </PanelBody>
245 </InspectorControls>
246 <div {...blockProps}>
247 {facet ? (
248 isLoading ? (
249 <PreviewLoading />
250 ) : min !== false && max !== false ? (
251 <Preview min={min} max={max} prefix={prefix} suffix={suffix} />
252 ) : (
253 <PreviewUnavailable value={facet} />
254 )
255 ) : (
256 <Wizard onChange={onChange} options={options} value={facet} />
257 )}
258 </div>
259 </>
260 );
261 };
262