This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
/
index.tsx
136 lines (126 loc) · 4.37 KB
/
index.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
/**
* External dependencies
*/
import type { ElementType } from 'react';
import type { BlockEditProps } from '@wordpress/blocks';
import { InspectorControls, BlockControls } from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
import { useMemo } from '@wordpress/element';
import { EditorBlock } from '@woocommerce/types';
import { addFilter } from '@wordpress/hooks';
import { ProductCollectionFeedbackPrompt } from '@woocommerce/editor-components/feedback-prompt';
import {
// @ts-expect-error Using experimental features
// eslint-disable-next-line @wordpress/no-unsafe-wp-apis
__experimentalToolsPanel as ToolsPanel,
} from '@wordpress/components';
/**
* Internal dependencies
*/
import { ProductCollectionAttributes } from '../types';
import { setQueryAttribute } from '../utils';
import { DEFAULT_FILTERS, getDefaultSettings } from '../constants';
import UpgradeNotice from './upgrade-notice';
import ColumnsControl from './columns-control';
import InheritQueryControl from './inherit-query-control';
import OrderByControl from './order-by-control';
import OnSaleControl from './on-sale-control';
import StockStatusControl from './stock-status-control';
import KeywordControl from './keyword-control';
import AttributesControl from './attributes-control';
import TaxonomyControls from './taxonomy-controls';
import HandPickedProductsControl from './hand-picked-products-control';
import AuthorControl from './author-control';
import DisplayLayoutControl from './display-layout-control';
import { replaceProductCollectionWithProducts } from '../../shared/scripts';
const ProductCollectionInspectorControls = (
props: BlockEditProps< ProductCollectionAttributes >
) => {
const query = props.attributes.query;
const inherit = query?.inherit;
const displayQueryControls = inherit === false;
const setQueryAttributeBind = useMemo(
() => setQueryAttribute.bind( null, props ),
[ props ]
);
const displayControlProps = {
setAttributes: props.setAttributes,
displayLayout: props.attributes.displayLayout,
};
const queryControlProps = {
setQueryAttribute: setQueryAttributeBind,
query,
};
return (
<InspectorControls>
<BlockControls>
<DisplayLayoutControl { ...displayControlProps } />
</BlockControls>
<ToolsPanel
label={ __( 'Settings', 'woo-gutenberg-products-block' ) }
resetAll={ () => {
const defaultSettings = getDefaultSettings(
props.attributes
);
props.setAttributes( defaultSettings );
} }
>
<ColumnsControl { ...displayControlProps } />
<InheritQueryControl { ...queryControlProps } />
{ displayQueryControls ? (
<OrderByControl { ...queryControlProps } />
) : null }
</ToolsPanel>
{ displayQueryControls ? (
<ToolsPanel
label={ __( 'Filters', 'woo-gutenberg-products-block' ) }
resetAll={ ( resetAllFilters: ( () => void )[] ) => {
setQueryAttribute( props, DEFAULT_FILTERS );
resetAllFilters.forEach( ( resetFilter ) =>
resetFilter()
);
} }
className="wc-block-editor-product-collection-inspector-toolspanel__filters"
>
<OnSaleControl { ...queryControlProps } />
<StockStatusControl { ...queryControlProps } />
<HandPickedProductsControl { ...queryControlProps } />
<KeywordControl { ...queryControlProps } />
<AttributesControl { ...queryControlProps } />
<TaxonomyControls { ...queryControlProps } />
<AuthorControl { ...queryControlProps } />
</ToolsPanel>
) : null }
<ProductCollectionFeedbackPrompt />
</InspectorControls>
);
};
export default ProductCollectionInspectorControls;
export const withUpgradeNoticeControls =
< T extends EditorBlock< T > >( BlockEdit: ElementType ) =>
( props: BlockEditProps< ProductCollectionAttributes > ) => {
const { attributes, clientId, isSelected } = props;
const { displayUpgradeNotice } = attributes;
const upgradeNoticeProps = {
clientId,
isSelected,
revertMigration: replaceProductCollectionWithProducts,
};
// @todo: Implement the logic to display option for manual upgrade.
// before enabling automatic upgrade and in case of reverting manual upgrade.
return (
<>
<InspectorControls>
{ displayUpgradeNotice && (
<UpgradeNotice { ...upgradeNoticeProps } />
) }
</InspectorControls>
<BlockEdit { ...props } />
</>
);
};
addFilter(
'editor.BlockEdit',
'woocommerce/product-collection',
withUpgradeNoticeControls
);