PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 2.0.0
GenerateBlocks v2.0.0
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 / components / QueryInspectorControls.jsx
generateblocks / src / blocks / query / components Last commit date
AddQueryParameterButton.jsx 1 year ago BlockSettings.jsx 1 year ago ControlBuilder.jsx 1 year ago DateQueryControl.jsx 1 year ago DateTimeControl.jsx 1 year ago ParameterControl.jsx 1 year ago ParameterList.jsx 1 year ago QueryInspectorControls.jsx 1 year ago SelectQueryParameter.jsx 1 year ago TaxonomyParameterControl.jsx 1 year ago editor.scss 1 year ago
QueryInspectorControls.jsx
147 lines
1 import { __ } from '@wordpress/i18n';
2 import { useEffect, useMemo, useState } from '@wordpress/element';
3 import { ToggleControl, SelectControl } from '@wordpress/components';
4 import { applyFilters } from '@wordpress/hooks';
5
6 import { isEqual } from 'lodash';
7 import {
8 QueryClient,
9 QueryClientProvider,
10 } from '@tanstack/react-query';
11
12 import useQueryReducer from '@hooks/useQueryReducer';
13 import { SelectQueryParameter } from './SelectQueryParameter';
14 import { AddQueryParameterButton } from './AddQueryParameterButton';
15 import { ParameterList } from './ParameterList';
16 import { getParameters } from '../query-parameters';
17
18 const queryClient = new QueryClient();
19
20 export function QueryInspectorControls( { attributes, setAttributes, context } ) {
21 const { queryState, setParameter, removeParameter } = useQueryReducer( attributes.query );
22 const [ displayParameterSelect, setDisplayParameterSelect ] = useState( false );
23
24 useEffect( () => {
25 setAttributes( { query: queryState } );
26 }, [ JSON.stringify( queryState ), ! isEqual( attributes.query, queryState ) ] );
27
28 const parameterOptions = useMemo( () => (
29 getParameters().map( ( parameter ) => {
30 parameter.isDisabled = ! parameter.isRepeatable && Object.keys( queryState ).includes( parameter.id );
31
32 return parameter;
33 } )
34 ), [ queryState ] );
35
36 const queryTypes = applyFilters(
37 'generateblocks.editor.query.queryTypes',
38 [
39 {
40 label: __( 'Post Query', 'generateblocks' ),
41 value: 'WP_Query',
42 help: __( 'Standard WP_Query for posts and pages.', 'generateblocks' ),
43 },
44 ],
45 attributes
46 );
47
48 const selectedQueryType = useMemo( () => {
49 return queryTypes.find( ( option ) => option.value === attributes.queryType );
50 }, [ queryTypes, attributes.queryType ] );
51
52 return (
53 <QueryClientProvider client={ queryClient }>
54 { queryTypes.length > 1 && (
55 <SelectControl
56 value={ attributes.queryType }
57 options={ queryTypes }
58 onChange={ ( value ) => setAttributes( { queryType: value, queryData: [], query: [] } ) }
59 label={ __( 'Query Type', 'generateblocks' ) }
60 help={ selectedQueryType?.help }
61 />
62 ) }
63
64 <SelectControl
65 label={ __( 'Pagination type', 'generateblocks' ) }
66 value={ attributes.paginationType }
67 options={ [
68 {
69 label: __( 'Standard', 'generateblocks' ),
70 value: 'standard',
71 },
72 {
73 label: __( 'Instant', 'generateblocks' ),
74 value: 'instant',
75 },
76 ] }
77 onChange={ ( value ) => setAttributes( { paginationType: value } ) }
78 />
79 { 'WP_Query' === attributes.queryType && (
80 <>
81 <ToggleControl
82 label={ __( 'Inherit query from template', 'generateblocks' ) }
83 help={ __( 'Toggle to use the global query context that is set with the current template, such as an archive or search.', 'generateblocks' ) }
84 checked={ !! attributes.inheritQuery }
85 onChange={ ( value ) => setAttributes( { inheritQuery: value } ) }
86 />
87 { ! attributes.inheritQuery &&
88 <>
89 <ParameterList
90 query={ queryState }
91 setParameter={ setParameter }
92 removeParameter={ removeParameter }
93 queryClient={ queryClient }
94 />
95
96 { ! displayParameterSelect &&
97 <AddQueryParameterButton onClick={ () => {
98 setDisplayParameterSelect( true );
99 } } />
100 }
101
102 { displayParameterSelect &&
103 <SelectQueryParameter
104 options={ parameterOptions }
105 onChange={ ( option ) => {
106 if (
107 !! option.isRepeatable &&
108 Array.isArray( option.default ) &&
109 !! option.repeatableDefaultValue
110 ) {
111 const parameterValue = !! queryState[ option.id ]
112 ? queryState[ option.id ]
113 : option.default;
114
115 setParameter( option.id, [ ...parameterValue, option.repeatableDefaultValue ] );
116 } else {
117 setParameter( option.id, option.default );
118 }
119
120 setDisplayParameterSelect( false );
121 } }
122 />
123 }
124 </>
125 }
126 </>
127 ) }
128 {
129 applyFilters(
130 'generateblocks.editor.query.inspectorControls',
131 null,
132 {
133 queryType: attributes.queryType,
134 attributes,
135 setAttributes,
136 queryState,
137 setParameter,
138 removeParameter,
139 context,
140 queryClient,
141 }
142 )
143 }
144 </QueryClientProvider>
145 );
146 }
147