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 220
/
product-collection-placeholder.tsx
88 lines (81 loc) · 2.24 KB
/
product-collection-placeholder.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
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import {
store as blockEditorStore,
useBlockProps,
} from '@wordpress/block-editor';
import { Placeholder, Button } from '@wordpress/components';
import { useDispatch } from '@wordpress/data';
import {
createBlock,
// @ts-expect-error Missing types in Gutenberg
createBlocksFromInnerBlocksTemplate,
} from '@wordpress/blocks';
/**
* Internal dependencies
*/
import Icon from '../icon';
import type { ProductCollectionEditComponentProps } from '../types';
import { DEFAULT_ATTRIBUTES, INNER_BLOCKS_TEMPLATE } from '../constants';
import { getDefaultValueOfInheritQueryFromTemplate } from '../utils';
import blockJson from '../block.json';
const getDefaultProductCollection = () =>
createBlock(
blockJson.name,
{
...DEFAULT_ATTRIBUTES,
query: {
...DEFAULT_ATTRIBUTES.query,
inherit: getDefaultValueOfInheritQueryFromTemplate(),
},
},
createBlocksFromInnerBlocksTemplate( INNER_BLOCKS_TEMPLATE )
);
const ProductCollectionPlaceholder = (
props: ProductCollectionEditComponentProps
) => {
const { clientId, openPatternSelectionModal } = props;
const blockProps = useBlockProps();
// @ts-expect-error Missing types in Gutenberg
const { replaceBlock } = useDispatch( blockEditorStore );
// TODO: This is temporary action that will be changed into
// "Add custom collection"
const addDefaultProductCollection = () => {
const defaultProductCollection = getDefaultProductCollection();
replaceBlock( clientId, defaultProductCollection );
};
return (
<div { ...blockProps }>
<Placeholder
icon={ Icon }
label={ __(
'Product Collection',
'woo-gutenberg-products-block'
) }
instructions={ __(
'Choose from pre-existing collections or add default one.',
'woo-gutenberg-products-block'
) }
>
<Button variant="primary" onClick={ openPatternSelectionModal }>
{ __(
'Choose Collection',
'woo-gutenberg-products-block'
) }
</Button>
<Button
variant="tertiary"
onClick={ addDefaultProductCollection }
>
{ __(
'Add default Product Collection',
'woo-gutenberg-products-block'
) }
</Button>
</Placeholder>
</div>
);
};
export default ProductCollectionPlaceholder;