This repository was archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy pathinspector-controls.tsx
187 lines (174 loc) · 4.86 KB
/
inspector-controls.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
* External dependencies
*/
import { ElementType } from 'react';
import { __ } from '@wordpress/i18n';
import { InspectorControls } from '@wordpress/block-editor';
import { useSelect } from '@wordpress/data';
import { addFilter } from '@wordpress/hooks';
import { EditorBlock } from '@woocommerce/types';
import {
FormTokenField,
ToggleControl,
// eslint-disable-next-line @wordpress/no-unsafe-wp-apis
__experimentalToolsPanel as ToolsPanel,
// eslint-disable-next-line @wordpress/no-unsafe-wp-apis
__experimentalToolsPanelItem as ToolsPanelItem,
} from '@wordpress/components';
/**
* Internal dependencies
*/
import {
ProductQueryArguments,
ProductQueryBlock,
QueryBlockAttributes,
} from './types';
import {
isWooQueryBlockVariation,
setQueryAttribute,
useAllowedControls,
} from './utils';
import {
ALL_PRODUCT_QUERY_CONTROLS,
QUERY_LOOP_ID,
STOCK_STATUS_OPTIONS,
} from './constants';
import { PopularPresets } from './inspector-controls/popular-presets';
const NAMESPACED_CONTROLS = ALL_PRODUCT_QUERY_CONTROLS.map(
( id ) =>
`__woocommerce${ id[ 0 ].toUpperCase() }${ id.slice(
1
) }` as keyof ProductQueryArguments
);
function useDefaultWooQueryParamsForVariation(
variationName: string | undefined
): Partial< ProductQueryArguments > {
const variationAttributes: QueryBlockAttributes = useSelect(
( select ) =>
select( 'core/blocks' )
.getBlockVariations( QUERY_LOOP_ID )
.find(
( variation: ProductQueryBlock ) =>
variation.name === variationName
)?.attributes
);
return variationAttributes
? Object.assign(
{},
...NAMESPACED_CONTROLS.map( ( key ) => ( {
[ key ]: variationAttributes.query[ key ],
} ) )
)
: {};
}
/**
* Gets the id of a specific stock status from its text label
*
* In theory, we could use a `saveTransform` function on the
* `FormFieldToken` component to do the conversion. However, plugins
* can add custom stock statii which don't conform to our naming
* conventions.
*/
function getStockStatusIdByLabel( statusLabel: FormTokenField.Value ) {
const label =
typeof statusLabel === 'string' ? statusLabel : statusLabel.value;
return Object.entries( STOCK_STATUS_OPTIONS ).find(
( [ , value ] ) => value === label
)?.[ 0 ];
}
export const TOOLS_PANEL_CONTROLS = {
onSale: ( props: ProductQueryBlock ) => {
const { query } = props.attributes;
return (
<ToolsPanelItem
label={ __( 'Sale status', 'woo-gutenberg-products-block' ) }
hasValue={ () => query.__woocommerceOnSale }
>
<ToggleControl
label={ __(
'Show only products on sale',
'woo-gutenberg-products-block'
) }
checked={ query.__woocommerceOnSale || false }
onChange={ ( __woocommerceOnSale ) => {
setQueryAttribute( props, {
__woocommerceOnSale,
} );
} }
/>
</ToolsPanelItem>
);
},
stockStatus: ( props: ProductQueryBlock ) => {
const { query } = props.attributes;
return (
<ToolsPanelItem
label={ __( 'Stock status', 'woo-gutenberg-products-block' ) }
hasValue={ () => query.__woocommerceStockStatus }
>
<FormTokenField
label={ __(
'Stock status',
'woo-gutenberg-products-block'
) }
onChange={ ( statusLabels ) => {
const __woocommerceStockStatus = statusLabels
.map( getStockStatusIdByLabel )
.filter( Boolean ) as string[];
setQueryAttribute( props, {
__woocommerceStockStatus,
} );
} }
suggestions={ Object.values( STOCK_STATUS_OPTIONS ) }
validateInput={ ( value: string ) =>
Object.values( STOCK_STATUS_OPTIONS ).includes( value )
}
value={
query?.__woocommerceStockStatus?.map(
( key ) => STOCK_STATUS_OPTIONS[ key ]
) || []
}
__experimentalExpandOnFocus={ true }
/>
</ToolsPanelItem>
);
},
};
export const withProductQueryControls =
< T extends EditorBlock< T > >( BlockEdit: ElementType ) =>
( props: ProductQueryBlock ) => {
const allowedControls = useAllowedControls( props.attributes );
const defaultWooQueryParams = useDefaultWooQueryParamsForVariation(
props.attributes.namespace
);
return isWooQueryBlockVariation( props ) ? (
<>
<InspectorControls>
{ allowedControls?.includes( 'presets' ) && (
<PopularPresets { ...props } />
) }
<ToolsPanel
class="woocommerce-product-query-toolspanel"
label={ __(
'Advanced Filters',
'woo-gutenberg-products-block'
) }
resetAll={ () => {
setQueryAttribute( props, defaultWooQueryParams );
} }
>
{ Object.entries( TOOLS_PANEL_CONTROLS ).map(
( [ key, Control ] ) =>
allowedControls?.includes( key ) ? (
<Control { ...props } />
) : null
) }
</ToolsPanel>
</InspectorControls>
<BlockEdit { ...props } />
</>
) : (
<BlockEdit { ...props } />
);
};
addFilter( 'editor.BlockEdit', QUERY_LOOP_ID, withProductQueryControls );