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
/
product-search-results.ts
179 lines (158 loc) · 4.82 KB
/
product-search-results.ts
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
/**
* External dependencies
*/
import {
createBlock,
createBlocksFromInnerBlocksTemplate,
type BlockInstance,
type InnerBlockTemplate,
} from '@wordpress/blocks';
import { isWpVersion } from '@woocommerce/settings';
import { isExperimentalBuild } from '@woocommerce/block-settings';
import { __, sprintf } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import {
INNER_BLOCKS_TEMPLATE as productsInnerBlocksTemplate,
QUERY_DEFAULT_ATTRIBUTES as productsQueryDefaultAttributes,
} from '../product-query/constants';
import { VARIATION_NAME as productsVariationName } from '../product-query/variations/product-query';
import { createArchiveTitleBlock, createRowBlock } from './utils';
import { OnClickCallbackParameter, type InheritedAttributes } from './types';
const createNoResultsParagraph = () =>
createBlock( 'core/paragraph', {
content: __(
'No products were found matching your selection.',
'woo-gutenberg-products-block'
),
} );
const createProductSearch = () =>
createBlock( 'core/search', {
buttonPosition: 'button-outside',
buttonText: __( 'Search', 'woo-gutenberg-products-block' ),
buttonUseIcon: false,
showLabel: false,
placeholder: __( 'Search products…', 'woo-gutenberg-products-block' ),
query: { post_type: 'product' },
} );
const extendInnerBlocksWithNoResultsContent = (
innerBlocks: InnerBlockTemplate[],
inheritedAttributes: InheritedAttributes
) => {
const noResultsContent = [
createNoResultsParagraph(),
createProductSearch(),
];
const noResultsBlockName = 'core/query-no-results';
const noResultsBlockIndex = innerBlocks.findIndex(
( block ) => block[ 0 ] === noResultsBlockName
);
const noResultsBlock = innerBlocks[ noResultsBlockIndex ];
const attributes = {
...( noResultsBlock[ 1 ] || {} ),
...inheritedAttributes,
};
const extendedNoResults = [
noResultsBlockName,
attributes,
noResultsContent,
];
return [
...productsInnerBlocksTemplate.slice( 0, noResultsBlockIndex ),
extendedNoResults,
...productsInnerBlocksTemplate.slice( noResultsBlockIndex + 1 ),
];
};
const createProductsBlock = ( inheritedAttributes: InheritedAttributes ) => {
const productsInnerBlocksWithNoResults =
extendInnerBlocksWithNoResultsContent(
productsInnerBlocksTemplate,
inheritedAttributes
);
return createBlock(
'core/query',
{
...productsQueryDefaultAttributes,
...inheritedAttributes,
namespace: productsVariationName,
query: {
...productsQueryDefaultAttributes.query,
inherit: true,
},
},
createBlocksFromInnerBlocksTemplate( productsInnerBlocksWithNoResults )
);
};
const getBlockifiedTemplate = ( inheritedAttributes: InheritedAttributes ) =>
[
createArchiveTitleBlock( 'search-title', inheritedAttributes ),
createBlock( 'woocommerce/store-notices', inheritedAttributes ),
createRowBlock(
[
createBlock( 'woocommerce/product-results-count' ),
createBlock( 'woocommerce/catalog-sorting' ),
],
inheritedAttributes
),
createProductsBlock( inheritedAttributes ),
].filter( Boolean ) as BlockInstance[];
const isConversionPossible = () => {
// Blockification is possible for the WP version 6.1 and above,
// which are the versions the Products block supports.
return isExperimentalBuild() && isWpVersion( '6.1', '>=' );
};
const getDescriptionAllowingConversion = ( templateTitle: string ) =>
sprintf(
/* translators: %s is the template title */
__(
'Transform this template into multiple blocks so you can add, remove, reorder, and customize your %s template.',
'woo-gutenberg-products-block'
),
templateTitle
);
const getDescriptionDisallowingConversion = ( templateTitle: string ) =>
sprintf(
/* translators: %s is the template title */
__(
'This block serves as a placeholder for your %s. It will display the actual product image, title, price in your store. You can move this placeholder around and add more blocks around to customize the template.',
'woo-gutenberg-products-block'
),
templateTitle
);
const getDescription = ( templateTitle: string, canConvert: boolean ) => {
if ( canConvert ) {
return getDescriptionAllowingConversion( templateTitle );
}
return getDescriptionDisallowingConversion( templateTitle );
};
const onClickCallback = ( {
clientId,
attributes,
getBlocks,
replaceBlock,
selectBlock,
}: OnClickCallbackParameter ) => {
replaceBlock( clientId, getBlockifiedTemplate( attributes ) );
const blocks = getBlocks();
const groupBlock = blocks.find(
( block ) =>
block.name === 'core/group' &&
block.innerBlocks.some(
( innerBlock ) =>
innerBlock.name === 'woocommerce/store-notices'
)
);
if ( groupBlock ) {
selectBlock( groupBlock.clientId );
}
};
const getButtonLabel = () =>
__( 'Transform into blocks', 'woo-gutenberg-products-block' );
export {
getBlockifiedTemplate,
isConversionPossible,
getDescription,
getButtonLabel,
onClickCallback,
};