|
| 1 | +/** |
| 2 | + * WordPress dependencies |
| 3 | + */ |
| 4 | +import { |
| 5 | + FormTokenField, |
| 6 | + SelectControl, |
| 7 | + ToggleControl, |
| 8 | +} from '@wordpress/components'; |
| 9 | +import { __ } from '@wordpress/i18n'; |
| 10 | +import { useSelect } from '@wordpress/data'; |
| 11 | +import { store as coreStore, useEntityRecords } from '@wordpress/core-data'; |
| 12 | +import { useMemo } from '@wordpress/element'; |
| 13 | + |
| 14 | +/** |
| 15 | + * Internal dependencies |
| 16 | + */ |
| 17 | +import useDebouncedInputValue from '../hooks/useDebouncedInputValue'; |
| 18 | +const operatorOptions = [ 'IN', 'NOT IN', 'EXISTS', 'NOT EXISTS', 'AND' ]; |
| 19 | + |
| 20 | +const updateTaxonomyQuery = ( queries, queryId, item, value ) => { |
| 21 | + return queries.map( ( query ) => { |
| 22 | + if ( query.id === queryId ) { |
| 23 | + return { |
| 24 | + ...query, |
| 25 | + [ item ]: value, |
| 26 | + }; |
| 27 | + } |
| 28 | + return query; |
| 29 | + } ); |
| 30 | +}; |
| 31 | + |
| 32 | +const SingleTaxonomyControl = ( { |
| 33 | + taxonomy, |
| 34 | + terms = [], |
| 35 | + availableTaxonomies, |
| 36 | + includeChildren, |
| 37 | + setAttributes, |
| 38 | + attributes, |
| 39 | + id, |
| 40 | + operator, |
| 41 | +} ) => { |
| 42 | + const [ searchTerm, setSearchTerm ] = useDebouncedInputValue( '', 500 ); |
| 43 | + const taxInfo = useSelect( ( select ) => |
| 44 | + select( coreStore ).getTaxonomy( taxonomy ) |
| 45 | + ); |
| 46 | + |
| 47 | + const isHierarchical = taxInfo?.hierarchical ?? false; |
| 48 | + |
| 49 | + const { records } = useEntityRecords( 'taxonomy', taxonomy, { |
| 50 | + per_page: 10, |
| 51 | + search: searchTerm, |
| 52 | + _fields: 'id,name', |
| 53 | + context: 'view', |
| 54 | + } ); |
| 55 | + |
| 56 | + const suggestions = useMemo( () => { |
| 57 | + return ( records ?? [] ).map( ( term ) => term.name ); |
| 58 | + }, [ records ] ); |
| 59 | + |
| 60 | + return ( |
| 61 | + <> |
| 62 | + <SelectControl |
| 63 | + key={ id } |
| 64 | + label={ __( 'Taxonomy', 'advanced-query-loop' ) } |
| 65 | + value={ taxonomy } |
| 66 | + options={ [ |
| 67 | + { label: 'Choose taxonomy', value: '' }, |
| 68 | + ...availableTaxonomies.map( ( { name, slug } ) => { |
| 69 | + return { |
| 70 | + label: name, |
| 71 | + value: slug, |
| 72 | + }; |
| 73 | + } ), |
| 74 | + ] } |
| 75 | + onChange={ ( newTaxonomy ) => { |
| 76 | + setAttributes( { |
| 77 | + query: { |
| 78 | + ...attributes.query, |
| 79 | + tax_query: { |
| 80 | + ...attributes.query.tax_query, |
| 81 | + queries: updateTaxonomyQuery( |
| 82 | + attributes.query.tax_query.queries, |
| 83 | + id, |
| 84 | + 'taxonomy', |
| 85 | + newTaxonomy |
| 86 | + ), |
| 87 | + }, |
| 88 | + }, |
| 89 | + } ); |
| 90 | + } } |
| 91 | + /> |
| 92 | + { taxonomy.length > 1 && ( |
| 93 | + <> |
| 94 | + <FormTokenField |
| 95 | + label={ __( 'Term(s)', 'advanced-query-loop' ) } |
| 96 | + suggestions={ suggestions } |
| 97 | + value={ terms } |
| 98 | + onInputChange={ ( newInput ) => { |
| 99 | + setSearchTerm( newInput ); |
| 100 | + } } |
| 101 | + onChange={ ( newTerms ) => { |
| 102 | + setAttributes( { |
| 103 | + query: { |
| 104 | + ...attributes.query, |
| 105 | + tax_query: { |
| 106 | + ...attributes.query.tax_query, |
| 107 | + queries: updateTaxonomyQuery( |
| 108 | + attributes.query.tax_query.queries, |
| 109 | + id, |
| 110 | + 'terms', |
| 111 | + newTerms |
| 112 | + ), |
| 113 | + }, |
| 114 | + }, |
| 115 | + } ); |
| 116 | + } } |
| 117 | + /> |
| 118 | + <SelectControl |
| 119 | + label={ __( 'Operator', 'advanced-query-loop' ) } |
| 120 | + value={ operator } |
| 121 | + options={ [ |
| 122 | + ...operatorOptions.map( ( value ) => { |
| 123 | + return { label: value, value }; |
| 124 | + } ), |
| 125 | + ] } |
| 126 | + onChange={ ( newOperator ) => { |
| 127 | + setAttributes( { |
| 128 | + query: { |
| 129 | + ...attributes.query, |
| 130 | + tax_query: { |
| 131 | + ...attributes.query.tax_query, |
| 132 | + queries: updateTaxonomyQuery( |
| 133 | + attributes.query.tax_query.queries, |
| 134 | + id, |
| 135 | + 'operator', |
| 136 | + newOperator |
| 137 | + ), |
| 138 | + }, |
| 139 | + }, |
| 140 | + } ); |
| 141 | + } } |
| 142 | + /> |
| 143 | + { isHierarchical && ( |
| 144 | + <ToggleControl |
| 145 | + label={ __( |
| 146 | + 'Include children?', |
| 147 | + 'advanced-query-loop' |
| 148 | + ) } |
| 149 | + checked={ includeChildren } |
| 150 | + onChange={ () => { |
| 151 | + setAttributes( { |
| 152 | + query: { |
| 153 | + ...attributes.query, |
| 154 | + tax_query: { |
| 155 | + ...attributes.query.tax_query, |
| 156 | + queries: updateTaxonomyQuery( |
| 157 | + attributes.query.tax_query |
| 158 | + .queries, |
| 159 | + id, |
| 160 | + 'include_children', |
| 161 | + ! includeChildren |
| 162 | + ), |
| 163 | + }, |
| 164 | + }, |
| 165 | + } ); |
| 166 | + } } |
| 167 | + help={ __( |
| 168 | + 'For hierarchical taxonomies only', |
| 169 | + 'advanced-query-loop' |
| 170 | + ) } |
| 171 | + /> |
| 172 | + ) } |
| 173 | + </> |
| 174 | + ) } |
| 175 | + </> |
| 176 | + ); |
| 177 | +}; |
| 178 | + |
| 179 | +export default SingleTaxonomyControl; |
0 commit comments