@wix/headless-stores 0.0.58 → 0.0.60
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/dist/react/Category.d.ts +5 -4
- package/cjs/dist/react/Product.d.ts +13 -12
- package/cjs/dist/react/ProductList.d.ts +3 -4
- package/cjs/dist/react/ProductList.js +2 -2
- package/cjs/dist/react/core/ProductList.d.ts +0 -3
- package/cjs/dist/react/core/ProductList.js +1 -3
- package/cjs/dist/services/index.d.ts +1 -2
- package/cjs/dist/services/index.js +1 -2
- package/cjs/dist/services/products-list-search-service.d.ts +1 -128
- package/cjs/dist/services/products-list-search-service.js +1 -507
- package/cjs/dist/services/products-list-service.d.ts +65 -1
- package/cjs/dist/services/products-list-service.js +413 -1
- package/dist/react/Category.d.ts +5 -4
- package/dist/react/Product.d.ts +13 -12
- package/dist/react/ProductList.d.ts +3 -4
- package/dist/react/ProductList.js +2 -2
- package/dist/react/core/ProductList.d.ts +0 -3
- package/dist/react/core/ProductList.js +1 -3
- package/dist/services/index.d.ts +1 -2
- package/dist/services/index.js +1 -2
- package/dist/services/products-list-search-service.d.ts +0 -128
- package/dist/services/products-list-search-service.js +1 -507
- package/dist/services/products-list-service.d.ts +65 -1
- package/dist/services/products-list-service.js +413 -1
- package/package.json +3 -2
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
import { productsV3, customizationsV3 } from '@wix/stores';
|
|
2
|
-
import { type Category } from './category-service.js';
|
|
3
|
-
import { SortType } from './../enums/sort-enums.js';
|
|
4
|
-
export { SortType } from './../enums/sort-enums.js';
|
|
5
|
-
/**
|
|
6
|
-
* Enumeration of inventory status types available for filtering.
|
|
7
|
-
* Re-exports the Wix inventory availability status enum values.
|
|
8
|
-
*/
|
|
9
|
-
export declare const InventoryStatusType: typeof productsV3.InventoryAvailabilityStatus;
|
|
10
|
-
/**
|
|
11
|
-
* Type for inventory status values.
|
|
12
|
-
* Re-exports the Wix inventory availability status enum type.
|
|
13
|
-
*/
|
|
14
|
-
export type InventoryStatusType = productsV3.InventoryAvailabilityStatus;
|
|
15
|
-
/**
|
|
16
|
-
* Initial search state that can be loaded from URL parameters.
|
|
17
|
-
*/
|
|
18
|
-
export type InitialSearchState = {
|
|
19
|
-
sort?: SortType;
|
|
20
|
-
limit?: number;
|
|
21
|
-
cursor?: string | null;
|
|
22
|
-
priceRange?: {
|
|
23
|
-
min?: number;
|
|
24
|
-
max?: number;
|
|
25
|
-
};
|
|
26
|
-
inventoryStatuses?: InventoryStatusType[];
|
|
27
|
-
productOptions?: Record<string, string[]>;
|
|
28
|
-
category?: Category;
|
|
29
|
-
visible?: boolean;
|
|
30
|
-
productType?: string;
|
|
31
|
-
};
|
|
32
|
-
/**
|
|
33
|
-
* Configuration interface for the Products List Search service.
|
|
34
|
-
*/
|
|
35
|
-
export type ProductsListSearchServiceConfig = {
|
|
36
|
-
initialSearchState?: InitialSearchState;
|
|
37
|
-
};
|
|
38
|
-
/**
|
|
39
|
-
* Service definition for the Products List Search service.
|
|
40
|
-
* This consolidates sort, pagination, and filtering functionality.
|
|
41
|
-
*/
|
|
42
|
-
export declare const ProductsListSearchServiceDefinition: string & {
|
|
43
|
-
__api: {};
|
|
44
|
-
__config: {};
|
|
45
|
-
isServiceDefinition?: boolean;
|
|
46
|
-
};
|
|
47
|
-
/**
|
|
48
|
-
* Convert URL sort format to SortType enum
|
|
49
|
-
*/
|
|
50
|
-
export declare function convertUrlSortToSortType(urlSort: string): SortType | null;
|
|
51
|
-
/**
|
|
52
|
-
* Parse URL and build complete search options with all filters, sort, and pagination.
|
|
53
|
-
* This function extracts search parameters, filters, sorting, and pagination from a URL
|
|
54
|
-
* and converts them into the format expected by the Wix Stores API.
|
|
55
|
-
*
|
|
56
|
-
* @param {string} url - The URL to parse search parameters from
|
|
57
|
-
* @param {Category[]} categoriesList - List of available categories for category slug resolution
|
|
58
|
-
* @param {productsV3.V3ProductSearch} [defaultSearchOptions] - Default search options to merge with parsed URL parameters
|
|
59
|
-
* @returns {Promise<{searchOptions: productsV3.V3ProductSearch, initialSearchState: InitialSearchState}>}
|
|
60
|
-
* Object containing both API-ready search options and UI-ready initial state
|
|
61
|
-
*
|
|
62
|
-
* @example
|
|
63
|
-
* ```tsx
|
|
64
|
-
* // Parse URL with filters, sort, and pagination
|
|
65
|
-
* const categories = await loadCategoriesListServiceConfig();
|
|
66
|
-
* const { searchOptions, initialSearchState } = await parseUrlToSearchOptions(
|
|
67
|
-
* 'https://example.com/products?sort=price:desc&Color=red,blue&minPrice=50',
|
|
68
|
-
* categories.categories
|
|
69
|
-
* );
|
|
70
|
-
*
|
|
71
|
-
* // Use searchOptions for API calls
|
|
72
|
-
* const products = await productsV3.searchProducts(searchOptions);
|
|
73
|
-
*
|
|
74
|
-
* // Use initialSearchState for UI initialization
|
|
75
|
-
* const filterState = initialSearchState.productOptions; // { colorId: ['red-id', 'blue-id'] }
|
|
76
|
-
* ```
|
|
77
|
-
*/
|
|
78
|
-
export declare function parseUrlToSearchOptions(url: string, categoriesList: Category[], customizations: customizationsV3.Customization[], defaultSearchOptions?: productsV3.V3ProductSearch): Promise<{
|
|
79
|
-
searchOptions: productsV3.V3ProductSearch;
|
|
80
|
-
initialSearchState: InitialSearchState;
|
|
81
|
-
}>;
|
|
82
|
-
/**
|
|
83
|
-
* Load search service configuration from URL or parsed URL result.
|
|
84
|
-
* This function provides the configuration for the Products List Search service,
|
|
85
|
-
* including customizations and initial search state.
|
|
86
|
-
*
|
|
87
|
-
* @param {string | { searchOptions: productsV3.V3ProductSearch; initialSearchState: InitialSearchState }} input - Either a URL to parse or parsed URL result from parseUrlToSearchOptions
|
|
88
|
-
* @returns {Promise<ProductsListSearchServiceConfig>} Promise that resolves to the search service configuration
|
|
89
|
-
*
|
|
90
|
-
* @example
|
|
91
|
-
* ```tsx
|
|
92
|
-
* // Option 1: Load from URL (will parse filters, sort, pagination from URL params)
|
|
93
|
-
* const searchConfig = await loadProductsListSearchServiceConfig(window.location.href);
|
|
94
|
-
*
|
|
95
|
-
* // Option 2: Custom parsing with defaults
|
|
96
|
-
* const categories = await loadCategoriesListServiceConfig();
|
|
97
|
-
* const parsed = await parseUrlToSearchOptions(
|
|
98
|
-
* window.location.href,
|
|
99
|
-
* categories.categories,
|
|
100
|
-
* {
|
|
101
|
-
* cursorPaging: { limit: 12 },
|
|
102
|
-
* filter: { 'categoryIds': ['123'] },
|
|
103
|
-
* sort: [{ fieldName: 'name' as const, order: 'ASC' as const }]
|
|
104
|
-
* }
|
|
105
|
-
* );
|
|
106
|
-
* const searchConfig = await loadProductsListSearchServiceConfig(parsed);
|
|
107
|
-
*
|
|
108
|
-
* // Option 3: Performance optimization - use parsed result for both services (no duplicate parsing)
|
|
109
|
-
* const categories = await loadCategoriesListServiceConfig();
|
|
110
|
-
* const parsed = await parseUrlToSearchOptions(url, categories.categories);
|
|
111
|
-
* const [productsConfig, searchConfig] = await Promise.all([
|
|
112
|
-
* loadProductsListServiceConfig(parsed),
|
|
113
|
-
* loadProductsListSearchServiceConfig(parsed),
|
|
114
|
-
* ]);
|
|
115
|
-
* ```
|
|
116
|
-
*/
|
|
117
|
-
export declare function loadProductsListSearchServiceConfig(input: string | {
|
|
118
|
-
searchOptions: productsV3.V3ProductSearch;
|
|
119
|
-
initialSearchState: InitialSearchState;
|
|
120
|
-
}): Promise<ProductsListSearchServiceConfig>;
|
|
121
|
-
/**
|
|
122
|
-
* Implementation of the Products List Search service
|
|
123
|
-
*/
|
|
124
|
-
export declare const ProductsListSearchService: import("@wix/services-definitions").ServiceFactory<string & {
|
|
125
|
-
__api: {};
|
|
126
|
-
__config: {};
|
|
127
|
-
isServiceDefinition?: boolean;
|
|
128
|
-
}, ProductsListSearchServiceConfig>;
|
|
@@ -1,507 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { SignalsServiceDefinition } from '@wix/services-definitions/core-services/signals';
|
|
3
|
-
import { DEFAULT_QUERY_LIMIT } from './products-list-service.js';
|
|
4
|
-
import { productsV3, customizationsV3 } from '@wix/stores';
|
|
5
|
-
import { loadCategoriesListServiceConfig } from './categories-list-service.js';
|
|
6
|
-
import { SortType } from './../enums/sort-enums.js';
|
|
7
|
-
export { SortType } from './../enums/sort-enums.js';
|
|
8
|
-
/**
|
|
9
|
-
* Enumeration of inventory status types available for filtering.
|
|
10
|
-
* Re-exports the Wix inventory availability status enum values.
|
|
11
|
-
*/
|
|
12
|
-
export const InventoryStatusType = productsV3.InventoryAvailabilityStatus;
|
|
13
|
-
/**
|
|
14
|
-
* Service definition for the Products List Search service.
|
|
15
|
-
* This consolidates sort, pagination, and filtering functionality.
|
|
16
|
-
*/
|
|
17
|
-
export const ProductsListSearchServiceDefinition = defineService('products-list-search');
|
|
18
|
-
/**
|
|
19
|
-
* Convert SortType enum to URL format
|
|
20
|
-
*/
|
|
21
|
-
function convertSortTypeToUrl(sortType) {
|
|
22
|
-
switch (sortType) {
|
|
23
|
-
case SortType.NAME_ASC:
|
|
24
|
-
return 'name';
|
|
25
|
-
case SortType.NAME_DESC:
|
|
26
|
-
return 'name:desc';
|
|
27
|
-
case SortType.PRICE_ASC:
|
|
28
|
-
return 'price';
|
|
29
|
-
case SortType.PRICE_DESC:
|
|
30
|
-
return 'price:desc';
|
|
31
|
-
case SortType.NEWEST:
|
|
32
|
-
return 'newest';
|
|
33
|
-
case SortType.RECOMMENDED:
|
|
34
|
-
return 'recommended';
|
|
35
|
-
default:
|
|
36
|
-
return 'name';
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Convert URL sort format to SortType enum
|
|
41
|
-
*/
|
|
42
|
-
export function convertUrlSortToSortType(urlSort) {
|
|
43
|
-
const sortParts = urlSort.split(':');
|
|
44
|
-
const field = sortParts[0]?.toLowerCase();
|
|
45
|
-
const order = sortParts[1]?.toLowerCase() === 'desc' ? 'desc' : 'asc';
|
|
46
|
-
switch (field) {
|
|
47
|
-
case 'name':
|
|
48
|
-
return order === 'desc' ? SortType.NAME_DESC : SortType.NAME_ASC;
|
|
49
|
-
case 'price':
|
|
50
|
-
return order === 'desc' ? SortType.PRICE_DESC : SortType.PRICE_ASC;
|
|
51
|
-
case 'newest':
|
|
52
|
-
case 'created':
|
|
53
|
-
return SortType.NEWEST;
|
|
54
|
-
case 'recommended':
|
|
55
|
-
return SortType.RECOMMENDED;
|
|
56
|
-
default:
|
|
57
|
-
return null;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Update URL with current search state (sort, filters, pagination)
|
|
62
|
-
*/
|
|
63
|
-
//@ts-expect-error
|
|
64
|
-
function updateUrlWithSearchState(searchState) {
|
|
65
|
-
if (typeof window === 'undefined')
|
|
66
|
-
return;
|
|
67
|
-
const { sort, filters, customizations, catalogBounds, categorySlug } = searchState;
|
|
68
|
-
// Convert filter IDs back to human-readable names for URL
|
|
69
|
-
const humanReadableOptions = {};
|
|
70
|
-
for (const [optionId, choiceIds] of Object.entries(filters?.productOptions ?? {})) {
|
|
71
|
-
const option = customizations.find((c) => c._id === optionId);
|
|
72
|
-
if (option && option.name) {
|
|
73
|
-
const choiceNames = [];
|
|
74
|
-
for (const choiceId of choiceIds) {
|
|
75
|
-
const choice = option.choicesSettings?.choices?.find((c) => c._id === choiceId);
|
|
76
|
-
if (choice && choice.name) {
|
|
77
|
-
choiceNames.push(choice.name);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
if (choiceNames.length > 0) {
|
|
81
|
-
humanReadableOptions[option.name] = choiceNames;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
// Start with current URL parameters to preserve non-search parameters
|
|
86
|
-
const params = new URLSearchParams(window.location.search);
|
|
87
|
-
// Define search-related parameters that we manage
|
|
88
|
-
const searchParams = [
|
|
89
|
-
'sort',
|
|
90
|
-
'limit',
|
|
91
|
-
'cursor',
|
|
92
|
-
'minPrice',
|
|
93
|
-
'maxPrice',
|
|
94
|
-
'inventoryStatus',
|
|
95
|
-
'visible',
|
|
96
|
-
'productType',
|
|
97
|
-
// Product option names will be dynamically added below
|
|
98
|
-
// Note: category is NOT included here as it's handled in the URL path
|
|
99
|
-
];
|
|
100
|
-
// Remove existing search parameters first
|
|
101
|
-
searchParams.forEach((param) => params.delete(param));
|
|
102
|
-
// Remove existing product option parameters (they have dynamic names)
|
|
103
|
-
for (const customization of customizations) {
|
|
104
|
-
if (customization.customizationType ===
|
|
105
|
-
customizationsV3.CustomizationType.PRODUCT_OPTION &&
|
|
106
|
-
customization.name) {
|
|
107
|
-
params.delete(customization.name);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
// Add sort parameter (only if not default)
|
|
111
|
-
const urlSort = convertSortTypeToUrl(sort);
|
|
112
|
-
if (sort !== SortType.NAME_ASC) {
|
|
113
|
-
params.set('sort', urlSort);
|
|
114
|
-
}
|
|
115
|
-
// Add price range parameters only if they differ from catalog bounds
|
|
116
|
-
if (filters.priceRange?.min &&
|
|
117
|
-
filters.priceRange.min > catalogBounds.minPrice) {
|
|
118
|
-
params.set('minPrice', filters.priceRange.min.toString());
|
|
119
|
-
}
|
|
120
|
-
if (filters.priceRange?.max &&
|
|
121
|
-
filters.priceRange.max < catalogBounds.maxPrice) {
|
|
122
|
-
params.set('maxPrice', filters.priceRange.max.toString());
|
|
123
|
-
}
|
|
124
|
-
// Add inventory status parameters
|
|
125
|
-
if (filters.inventoryStatuses && filters.inventoryStatuses.length > 0) {
|
|
126
|
-
params.set('inventoryStatus', filters.inventoryStatuses.join(','));
|
|
127
|
-
}
|
|
128
|
-
// Add visibility filter (only if explicitly false, since true is default)
|
|
129
|
-
if (filters.visible === false) {
|
|
130
|
-
params.set('visible', 'false');
|
|
131
|
-
}
|
|
132
|
-
// Add product type filter
|
|
133
|
-
if (filters.productType) {
|
|
134
|
-
params.set('productType', filters.productType);
|
|
135
|
-
}
|
|
136
|
-
// Add product options as individual parameters (Color=Red,Blue&Size=Large)
|
|
137
|
-
for (const [optionName, values] of Object.entries(humanReadableOptions)) {
|
|
138
|
-
if (values.length > 0) {
|
|
139
|
-
params.set(optionName, values.join(','));
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
// Handle URL path construction with category
|
|
143
|
-
let baseUrl = window.location.pathname;
|
|
144
|
-
// If categorySlug is provided, replace the last path segment (which represents the category)
|
|
145
|
-
if (categorySlug) {
|
|
146
|
-
const pathSegments = baseUrl.split('/').filter(Boolean);
|
|
147
|
-
if (pathSegments.length > 0) {
|
|
148
|
-
// Replace the last segment with the new category slug
|
|
149
|
-
pathSegments[pathSegments.length - 1] = categorySlug;
|
|
150
|
-
baseUrl = '/' + pathSegments.join('/');
|
|
151
|
-
}
|
|
152
|
-
else {
|
|
153
|
-
// If no segments, just use the category slug
|
|
154
|
-
baseUrl = `/${categorySlug}`;
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
// Build the new URL
|
|
158
|
-
const newUrl = params.toString()
|
|
159
|
-
? `${baseUrl}?${params.toString()}`
|
|
160
|
-
: baseUrl;
|
|
161
|
-
// Only update if URL actually changed
|
|
162
|
-
if (newUrl !== window.location.pathname + window.location.search) {
|
|
163
|
-
window.history.pushState(null, '', newUrl);
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
/**
|
|
167
|
-
* Parse URL and build complete search options with all filters, sort, and pagination.
|
|
168
|
-
* This function extracts search parameters, filters, sorting, and pagination from a URL
|
|
169
|
-
* and converts them into the format expected by the Wix Stores API.
|
|
170
|
-
*
|
|
171
|
-
* @param {string} url - The URL to parse search parameters from
|
|
172
|
-
* @param {Category[]} categoriesList - List of available categories for category slug resolution
|
|
173
|
-
* @param {productsV3.V3ProductSearch} [defaultSearchOptions] - Default search options to merge with parsed URL parameters
|
|
174
|
-
* @returns {Promise<{searchOptions: productsV3.V3ProductSearch, initialSearchState: InitialSearchState}>}
|
|
175
|
-
* Object containing both API-ready search options and UI-ready initial state
|
|
176
|
-
*
|
|
177
|
-
* @example
|
|
178
|
-
* ```tsx
|
|
179
|
-
* // Parse URL with filters, sort, and pagination
|
|
180
|
-
* const categories = await loadCategoriesListServiceConfig();
|
|
181
|
-
* const { searchOptions, initialSearchState } = await parseUrlToSearchOptions(
|
|
182
|
-
* 'https://example.com/products?sort=price:desc&Color=red,blue&minPrice=50',
|
|
183
|
-
* categories.categories
|
|
184
|
-
* );
|
|
185
|
-
*
|
|
186
|
-
* // Use searchOptions for API calls
|
|
187
|
-
* const products = await productsV3.searchProducts(searchOptions);
|
|
188
|
-
*
|
|
189
|
-
* // Use initialSearchState for UI initialization
|
|
190
|
-
* const filterState = initialSearchState.productOptions; // { colorId: ['red-id', 'blue-id'] }
|
|
191
|
-
* ```
|
|
192
|
-
*/
|
|
193
|
-
export async function parseUrlToSearchOptions(url, categoriesList, customizations, defaultSearchOptions) {
|
|
194
|
-
const urlObj = new URL(url);
|
|
195
|
-
const searchParams = urlObj.searchParams;
|
|
196
|
-
// Build search options
|
|
197
|
-
const searchOptions = {
|
|
198
|
-
cursorPaging: {
|
|
199
|
-
limit: DEFAULT_QUERY_LIMIT,
|
|
200
|
-
},
|
|
201
|
-
...defaultSearchOptions,
|
|
202
|
-
};
|
|
203
|
-
// Initialize search state for service
|
|
204
|
-
const initialSearchState = {};
|
|
205
|
-
// Extract category slug from URL path
|
|
206
|
-
// The category slug is always the last segment of the path
|
|
207
|
-
const pathSegments = urlObj.pathname.split('/').filter(Boolean);
|
|
208
|
-
let category = undefined;
|
|
209
|
-
if (pathSegments.length > 0) {
|
|
210
|
-
const lastSegment = pathSegments[pathSegments.length - 1];
|
|
211
|
-
// Check if the last segment matches any category in the categories list
|
|
212
|
-
category = categoriesList.find((cat) => cat.slug === lastSegment);
|
|
213
|
-
if (category) {
|
|
214
|
-
initialSearchState.category = category;
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
// Handle text search (q parameter)
|
|
218
|
-
const query = searchParams.get('q');
|
|
219
|
-
if (query) {
|
|
220
|
-
searchOptions.search = {
|
|
221
|
-
expression: query,
|
|
222
|
-
};
|
|
223
|
-
}
|
|
224
|
-
// Handle sorting
|
|
225
|
-
const sort = searchParams.get('sort');
|
|
226
|
-
if (sort) {
|
|
227
|
-
const sortType = convertUrlSortToSortType(sort);
|
|
228
|
-
if (sortType) {
|
|
229
|
-
initialSearchState.sort = sortType;
|
|
230
|
-
// Apply sort to search options
|
|
231
|
-
switch (sortType) {
|
|
232
|
-
case SortType.NAME_ASC:
|
|
233
|
-
searchOptions.sort = [
|
|
234
|
-
{ fieldName: 'name', order: productsV3.SortDirection.ASC },
|
|
235
|
-
];
|
|
236
|
-
break;
|
|
237
|
-
case SortType.NAME_DESC:
|
|
238
|
-
searchOptions.sort = [
|
|
239
|
-
{ fieldName: 'name', order: productsV3.SortDirection.DESC },
|
|
240
|
-
];
|
|
241
|
-
break;
|
|
242
|
-
case SortType.PRICE_ASC:
|
|
243
|
-
searchOptions.sort = [
|
|
244
|
-
{
|
|
245
|
-
fieldName: 'actualPriceRange.minValue.amount',
|
|
246
|
-
order: productsV3.SortDirection.ASC,
|
|
247
|
-
},
|
|
248
|
-
];
|
|
249
|
-
break;
|
|
250
|
-
case SortType.PRICE_DESC:
|
|
251
|
-
searchOptions.sort = [
|
|
252
|
-
{
|
|
253
|
-
fieldName: 'actualPriceRange.minValue.amount',
|
|
254
|
-
order: productsV3.SortDirection.DESC,
|
|
255
|
-
},
|
|
256
|
-
];
|
|
257
|
-
break;
|
|
258
|
-
case SortType.RECOMMENDED:
|
|
259
|
-
searchOptions.sort = [
|
|
260
|
-
{
|
|
261
|
-
fieldName: 'name',
|
|
262
|
-
order: productsV3.SortDirection.DESC,
|
|
263
|
-
},
|
|
264
|
-
];
|
|
265
|
-
break;
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
// Handle pagination
|
|
270
|
-
const limit = searchParams.get('limit');
|
|
271
|
-
const cursor = searchParams.get('cursor');
|
|
272
|
-
if (limit || cursor) {
|
|
273
|
-
searchOptions.cursorPaging = {};
|
|
274
|
-
if (limit) {
|
|
275
|
-
const limitNum = parseInt(limit, 10);
|
|
276
|
-
if (!isNaN(limitNum) && limitNum > 0) {
|
|
277
|
-
searchOptions.cursorPaging.limit = limitNum;
|
|
278
|
-
initialSearchState.limit = limitNum;
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
if (cursor) {
|
|
282
|
-
searchOptions.cursorPaging.cursor = cursor;
|
|
283
|
-
initialSearchState.cursor = cursor;
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
// Handle filtering for search options
|
|
287
|
-
const filter = {};
|
|
288
|
-
const visible = searchParams.get('visible');
|
|
289
|
-
if (visible !== null) {
|
|
290
|
-
filter['visible'] = visible === 'true';
|
|
291
|
-
initialSearchState.visible = visible === 'true';
|
|
292
|
-
}
|
|
293
|
-
const productType = searchParams.get('productType');
|
|
294
|
-
if (productType) {
|
|
295
|
-
filter['productType'] = productType;
|
|
296
|
-
initialSearchState.productType = productType;
|
|
297
|
-
}
|
|
298
|
-
// Add category filter if found
|
|
299
|
-
if (category) {
|
|
300
|
-
filter['allCategoriesInfo.categories'] = {
|
|
301
|
-
$matchItems: [{ _id: { $in: [category._id] } }],
|
|
302
|
-
};
|
|
303
|
-
}
|
|
304
|
-
// Price range filtering
|
|
305
|
-
const minPrice = searchParams.get('minPrice');
|
|
306
|
-
const maxPrice = searchParams.get('maxPrice');
|
|
307
|
-
if (minPrice || maxPrice) {
|
|
308
|
-
initialSearchState.priceRange = {};
|
|
309
|
-
if (minPrice) {
|
|
310
|
-
const minPriceNum = parseFloat(minPrice);
|
|
311
|
-
if (!isNaN(minPriceNum)) {
|
|
312
|
-
filter['actualPriceRange.minValue.amount'] = { $gte: minPriceNum };
|
|
313
|
-
initialSearchState.priceRange.min = minPriceNum;
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
if (maxPrice) {
|
|
317
|
-
const maxPriceNum = parseFloat(maxPrice);
|
|
318
|
-
if (!isNaN(maxPriceNum)) {
|
|
319
|
-
filter['actualPriceRange.maxValue.amount'] = { $lte: maxPriceNum };
|
|
320
|
-
initialSearchState.priceRange.max = maxPriceNum;
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
const inventoryStatus = searchParams.get('inventoryStatus');
|
|
325
|
-
if (inventoryStatus) {
|
|
326
|
-
filter['inventory.availabilityStatus'] = {
|
|
327
|
-
$in: inventoryStatus.split(','),
|
|
328
|
-
};
|
|
329
|
-
}
|
|
330
|
-
// Parse product options from URL parameters
|
|
331
|
-
const reservedParams = [
|
|
332
|
-
'minPrice',
|
|
333
|
-
'maxPrice',
|
|
334
|
-
'inventory_status',
|
|
335
|
-
'inventoryStatus',
|
|
336
|
-
'visible',
|
|
337
|
-
'productType',
|
|
338
|
-
'q',
|
|
339
|
-
'limit',
|
|
340
|
-
'cursor',
|
|
341
|
-
'sort',
|
|
342
|
-
];
|
|
343
|
-
const productOptionsById = {};
|
|
344
|
-
for (const [optionName, optionValues] of searchParams.entries()) {
|
|
345
|
-
if (reservedParams.includes(optionName))
|
|
346
|
-
continue;
|
|
347
|
-
// Find the option by name in customizations
|
|
348
|
-
const option = customizations.find((c) => c.name === optionName &&
|
|
349
|
-
c.customizationType ===
|
|
350
|
-
customizationsV3.CustomizationType.PRODUCT_OPTION);
|
|
351
|
-
if (option && option._id) {
|
|
352
|
-
const choiceValues = optionValues.split(',').filter(Boolean);
|
|
353
|
-
const choiceIds = [];
|
|
354
|
-
// Convert choice names to IDs
|
|
355
|
-
for (const choiceName of choiceValues) {
|
|
356
|
-
const choice = option.choicesSettings?.choices?.find((c) => c.name === choiceName);
|
|
357
|
-
if (choice && choice._id) {
|
|
358
|
-
choiceIds.push(choice._id);
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
|
-
if (choiceIds.length > 0) {
|
|
362
|
-
productOptionsById[option._id] = choiceIds;
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
|
-
if (Object.keys(productOptionsById).length > 0) {
|
|
367
|
-
initialSearchState.productOptions = productOptionsById;
|
|
368
|
-
// Add product option filter to search options
|
|
369
|
-
filter[`options.choicesSettings.choices.choiceId`] = {
|
|
370
|
-
$hasSome: Object.values(productOptionsById).flat(),
|
|
371
|
-
};
|
|
372
|
-
}
|
|
373
|
-
// Add filter to search options if any filters were set
|
|
374
|
-
if (Object.keys(filter).length > 0) {
|
|
375
|
-
searchOptions.filter = filter;
|
|
376
|
-
}
|
|
377
|
-
// Add aggregations for getting filter options
|
|
378
|
-
searchOptions.aggregations = [
|
|
379
|
-
{
|
|
380
|
-
name: 'minPrice',
|
|
381
|
-
fieldPath: 'actualPriceRange.minValue.amount',
|
|
382
|
-
type: 'SCALAR',
|
|
383
|
-
scalar: { type: 'MIN' },
|
|
384
|
-
},
|
|
385
|
-
{
|
|
386
|
-
name: 'maxPrice',
|
|
387
|
-
fieldPath: 'actualPriceRange.maxValue.amount',
|
|
388
|
-
type: 'SCALAR',
|
|
389
|
-
scalar: { type: 'MAX' },
|
|
390
|
-
},
|
|
391
|
-
{
|
|
392
|
-
name: 'optionNames',
|
|
393
|
-
fieldPath: 'options.name',
|
|
394
|
-
type: productsV3.SortType.VALUE,
|
|
395
|
-
value: {
|
|
396
|
-
limit: 20,
|
|
397
|
-
sortType: productsV3.SortType.VALUE,
|
|
398
|
-
sortDirection: productsV3.SortDirection.ASC,
|
|
399
|
-
},
|
|
400
|
-
},
|
|
401
|
-
{
|
|
402
|
-
name: 'choiceNames',
|
|
403
|
-
fieldPath: 'options.choicesSettings.choices.name',
|
|
404
|
-
type: productsV3.SortType.VALUE,
|
|
405
|
-
value: {
|
|
406
|
-
limit: 50,
|
|
407
|
-
sortType: productsV3.SortType.VALUE,
|
|
408
|
-
sortDirection: productsV3.SortDirection.ASC,
|
|
409
|
-
},
|
|
410
|
-
},
|
|
411
|
-
{
|
|
412
|
-
name: 'inventoryStatus',
|
|
413
|
-
fieldPath: 'inventory.availabilityStatus',
|
|
414
|
-
type: productsV3.SortType.VALUE,
|
|
415
|
-
value: {
|
|
416
|
-
limit: 10,
|
|
417
|
-
sortType: productsV3.SortType.VALUE,
|
|
418
|
-
sortDirection: productsV3.SortDirection.ASC,
|
|
419
|
-
},
|
|
420
|
-
},
|
|
421
|
-
];
|
|
422
|
-
return { searchOptions, initialSearchState };
|
|
423
|
-
}
|
|
424
|
-
/**
|
|
425
|
-
* Load search service configuration from URL or parsed URL result.
|
|
426
|
-
* This function provides the configuration for the Products List Search service,
|
|
427
|
-
* including customizations and initial search state.
|
|
428
|
-
*
|
|
429
|
-
* @param {string | { searchOptions: productsV3.V3ProductSearch; initialSearchState: InitialSearchState }} input - Either a URL to parse or parsed URL result from parseUrlToSearchOptions
|
|
430
|
-
* @returns {Promise<ProductsListSearchServiceConfig>} Promise that resolves to the search service configuration
|
|
431
|
-
*
|
|
432
|
-
* @example
|
|
433
|
-
* ```tsx
|
|
434
|
-
* // Option 1: Load from URL (will parse filters, sort, pagination from URL params)
|
|
435
|
-
* const searchConfig = await loadProductsListSearchServiceConfig(window.location.href);
|
|
436
|
-
*
|
|
437
|
-
* // Option 2: Custom parsing with defaults
|
|
438
|
-
* const categories = await loadCategoriesListServiceConfig();
|
|
439
|
-
* const parsed = await parseUrlToSearchOptions(
|
|
440
|
-
* window.location.href,
|
|
441
|
-
* categories.categories,
|
|
442
|
-
* {
|
|
443
|
-
* cursorPaging: { limit: 12 },
|
|
444
|
-
* filter: { 'categoryIds': ['123'] },
|
|
445
|
-
* sort: [{ fieldName: 'name' as const, order: 'ASC' as const }]
|
|
446
|
-
* }
|
|
447
|
-
* );
|
|
448
|
-
* const searchConfig = await loadProductsListSearchServiceConfig(parsed);
|
|
449
|
-
*
|
|
450
|
-
* // Option 3: Performance optimization - use parsed result for both services (no duplicate parsing)
|
|
451
|
-
* const categories = await loadCategoriesListServiceConfig();
|
|
452
|
-
* const parsed = await parseUrlToSearchOptions(url, categories.categories);
|
|
453
|
-
* const [productsConfig, searchConfig] = await Promise.all([
|
|
454
|
-
* loadProductsListServiceConfig(parsed),
|
|
455
|
-
* loadProductsListSearchServiceConfig(parsed),
|
|
456
|
-
* ]);
|
|
457
|
-
* ```
|
|
458
|
-
*/
|
|
459
|
-
export async function loadProductsListSearchServiceConfig(input) {
|
|
460
|
-
let initialSearchState;
|
|
461
|
-
if (typeof input === 'string') {
|
|
462
|
-
// URL input - parse it
|
|
463
|
-
const categoriesListConfig = await loadCategoriesListServiceConfig();
|
|
464
|
-
const { items: customizations = [] } = await customizationsV3
|
|
465
|
-
.queryCustomizations()
|
|
466
|
-
.find();
|
|
467
|
-
const { initialSearchState: parsedState } = await parseUrlToSearchOptions(input, categoriesListConfig.categories, customizations);
|
|
468
|
-
initialSearchState = parsedState;
|
|
469
|
-
}
|
|
470
|
-
else {
|
|
471
|
-
// Parsed URL result - use initialSearchState directly (no duplicate work)
|
|
472
|
-
initialSearchState = input.initialSearchState;
|
|
473
|
-
}
|
|
474
|
-
return {
|
|
475
|
-
initialSearchState,
|
|
476
|
-
};
|
|
477
|
-
}
|
|
478
|
-
/**
|
|
479
|
-
* Implementation of the Products List Search service
|
|
480
|
-
*/
|
|
481
|
-
export const ProductsListSearchService = implementService.withConfig()(ProductsListSearchServiceDefinition, ({ getService }) => {
|
|
482
|
-
let firstRun = true;
|
|
483
|
-
const signalsService = getService(SignalsServiceDefinition);
|
|
484
|
-
if (typeof window !== 'undefined') {
|
|
485
|
-
// Watch for changes in any search parameters and update search options
|
|
486
|
-
signalsService.effect(() => {
|
|
487
|
-
// Read all signals to establish dependencies
|
|
488
|
-
if (firstRun) {
|
|
489
|
-
firstRun = false;
|
|
490
|
-
return;
|
|
491
|
-
}
|
|
492
|
-
// TODO fix it blat
|
|
493
|
-
// const currentFilters = {
|
|
494
|
-
// ...(selectedVisible !== null && { visible: selectedVisible }),
|
|
495
|
-
// ...(selectedProductType && { productType: selectedProductType }),
|
|
496
|
-
// };
|
|
497
|
-
// updateUrlWithSearchState({
|
|
498
|
-
// sort,
|
|
499
|
-
// filters: currentFilters,
|
|
500
|
-
// customizations,
|
|
501
|
-
// catalogBounds,
|
|
502
|
-
// categorySlug: selectedCategory?.slug || undefined,
|
|
503
|
-
// });
|
|
504
|
-
});
|
|
505
|
-
}
|
|
506
|
-
return {};
|
|
507
|
-
});
|
|
1
|
+
"use strict";
|