PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 1.5.2
GenerateBlocks v1.5.2
trunk 1.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.3.0
generateblocks / src / blocks / query-loop / components / InspectorControls.js
generateblocks / src / blocks / query-loop / components Last commit date
inspector-controls 4 years ago BlockControls.js 4 years ago InspectorControls.js 4 years ago LayoutSelector.js 4 years ago LoopRenderer.js 4 years ago QueryLoopRenderer.js 4 years ago utils.js 4 years ago
InspectorControls.js
102 lines
1 import { InspectorControls } from '@wordpress/block-editor';
2 import { __ } from '@wordpress/i18n';
3 import PanelArea from '../../../components/panel-area';
4 import { useEffect, useMemo, useState } from '@wordpress/element';
5 import SelectQueryParameter from './inspector-controls/SelectQueryParameter';
6 import AddQueryParameterButton from './inspector-controls/AddQueryParameterButton';
7 import ParameterList from './inspector-controls/parameter-list';
8 import useQueryReducer from '../hooks/useQueryReducer';
9 import isEmpty from '../../../utils/object-is-empty';
10 import queryParameterOptions from '../query-parameters';
11 import getIcon from '../../../utils/get-icon';
12 import { ToggleControl } from '@wordpress/components';
13 import { isEqual } from 'lodash';
14
15 export default ( { attributes, setAttributes } ) => {
16 const { queryState, insertParameters, setParameter, removeParameter } = useQueryReducer();
17 const [ displayParameterSelect, setDisplayParameterSelect ] = useState( false );
18
19 useEffect( () => {
20 if ( ! isEmpty( attributes.query ) ) {
21 insertParameters( attributes.query );
22 } else {
23 insertParameters( {
24 post_type: 'post',
25 per_page: 10,
26 } );
27 }
28 }, [] );
29
30 useEffect( () => {
31 if ( ! isEmpty( queryState ) && ! isEqual( attributes.query, queryState ) ) {
32 setAttributes( { query: queryState } );
33 }
34 }, [ queryState ] );
35
36 const parameterOptions = useMemo( () => (
37 queryParameterOptions.map( ( parameter ) => {
38 parameter.isDisabled = ! parameter.isRepeatable && Object.keys( queryState ).includes( parameter.id );
39
40 return parameter;
41 } )
42 ), [ queryState ] );
43
44 return (
45 <InspectorControls>
46 <PanelArea
47 id={ 'queryLoopControls' }
48 title={ __( 'Query Parameters', 'generateblocks' ) }
49 initialOpen={ true }
50 icon={ getIcon( 'query-params' ) }
51 className="gblocks-panel-label"
52 >
53 <ToggleControl
54 label={ __( 'Inherit query from template', 'generateblocks' ) }
55 help={ __( 'Toggle to use the global query context that is set with the current template, such as an archive or search.', 'generateblocks' ) }
56 checked={ !! attributes.inheritQuery }
57 onChange={ ( value ) => setAttributes( { inheritQuery: value } ) }
58 />
59
60 { ! attributes.inheritQuery &&
61 <>
62 <ParameterList
63 query={ queryState }
64 setParameter={ setParameter }
65 removeParameter={ removeParameter }
66 />
67
68 { ! displayParameterSelect &&
69 <AddQueryParameterButton onClick={ () => {
70 setDisplayParameterSelect( true );
71 } } />
72 }
73
74 { displayParameterSelect &&
75 <SelectQueryParameter
76 options={ parameterOptions }
77 onChange={ ( option ) => {
78 if (
79 !! option.isRepeatable &&
80 Array.isArray( option.default ) &&
81 !! option.repeatableDefaultValue
82 ) {
83 const parameterValue = !! queryState[ option.id ]
84 ? queryState[ option.id ]
85 : option.default;
86
87 setParameter( option.id, [ ...parameterValue, option.repeatableDefaultValue ] );
88 } else {
89 setParameter( option.id, option.default );
90 }
91
92 setDisplayParameterSelect( false );
93 } }
94 />
95 }
96 </>
97 }
98 </PanelArea>
99 </InspectorControls>
100 );
101 };
102