PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 2.0.2
GenerateBlocks v2.0.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 3 years ago BlockControls.js 3 years ago InspectorAdvancedControls.js 2 years ago InspectorControls.js 3 years ago LayoutSelector.js 3 years ago LoopRenderer.js 3 years ago QueryLoopRenderer.js 2 years ago utils.js 3 years ago
InspectorControls.js
98 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( attributes.query );
17 const [ displayParameterSelect, setDisplayParameterSelect ] = useState( false );
18
19 useEffect( () => {
20 if ( isEmpty( attributes.query ) ) {
21 insertParameters( {
22 post_type: 'post',
23 per_page: 10,
24 } );
25 }
26 }, [] );
27
28 useEffect( () => {
29 setAttributes( { query: queryState } );
30 }, [ JSON.stringify( queryState ), ! isEqual( attributes.query, queryState ) ] );
31
32 const parameterOptions = useMemo( () => (
33 queryParameterOptions.map( ( parameter ) => {
34 parameter.isDisabled = ! parameter.isRepeatable && Object.keys( queryState ).includes( parameter.id );
35
36 return parameter;
37 } )
38 ), [ queryState ] );
39
40 return (
41 <InspectorControls>
42 <PanelArea
43 id={ 'queryLoopControls' }
44 title={ __( 'Query Parameters', 'generateblocks' ) }
45 initialOpen={ true }
46 icon={ getIcon( 'query-params' ) }
47 className="gblocks-panel-label"
48 >
49 <ToggleControl
50 label={ __( 'Inherit query from template', 'generateblocks' ) }
51 help={ __( 'Toggle to use the global query context that is set with the current template, such as an archive or search.', 'generateblocks' ) }
52 checked={ !! attributes.inheritQuery }
53 onChange={ ( value ) => setAttributes( { inheritQuery: value } ) }
54 />
55
56 { ! attributes.inheritQuery &&
57 <>
58 <ParameterList
59 query={ queryState }
60 setParameter={ setParameter }
61 removeParameter={ removeParameter }
62 />
63
64 { ! displayParameterSelect &&
65 <AddQueryParameterButton onClick={ () => {
66 setDisplayParameterSelect( true );
67 } } />
68 }
69
70 { displayParameterSelect &&
71 <SelectQueryParameter
72 options={ parameterOptions }
73 onChange={ ( option ) => {
74 if (
75 !! option.isRepeatable &&
76 Array.isArray( option.default ) &&
77 !! option.repeatableDefaultValue
78 ) {
79 const parameterValue = !! queryState[ option.id ]
80 ? queryState[ option.id ]
81 : option.default;
82
83 setParameter( option.id, [ ...parameterValue, option.repeatableDefaultValue ] );
84 } else {
85 setParameter( option.id, option.default );
86 }
87
88 setDisplayParameterSelect( false );
89 } }
90 />
91 }
92 </>
93 }
94 </PanelArea>
95 </InspectorControls>
96 );
97 };
98