Skip to content

Commit c2f751b

Browse files
arielWixttsahi
authored andcommitted
fixing another react-router case (#119)
1 parent 8231321 commit c2f751b

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed

packages/headless-components/stores/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@wix/headless-stores",
3-
"version": "0.0.49",
3+
"version": "0.0.50",
44
"type": "module",
55
"scripts": {
66
"prebuild": "cd ../media && yarn build && cd ../ecom && yarn build",

packages/headless-components/stores/src/services/products-list-search-service.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -346,23 +346,17 @@ export async function parseUrlToSearchOptions(
346346
// Initialize search state for service
347347
const initialSearchState: InitialSearchState = {};
348348

349-
// Extract category slug from URL path (e.g., /category/category-slug)
350-
const pathSegments = urlObj.pathname.split("/");
351-
const categoryIndex = pathSegments.findIndex(
352-
(segment) => segment === "category",
353-
);
354-
let categorySlug: string | null = null;
349+
// Extract category slug from URL path
350+
// The category slug is always the last segment of the path
351+
const pathSegments = urlObj.pathname.split("/").filter(Boolean);
355352
let category: Category | undefined = undefined;
356353

357-
if (categoryIndex !== -1 && categoryIndex + 1 < pathSegments.length) {
358-
categorySlug = pathSegments[categoryIndex + 1] || null;
359-
360-
// Find the category by slug from the provided categories list
361-
if (categorySlug) {
362-
category = categoriesList.find((cat) => cat.slug === categorySlug);
363-
if (category) {
364-
initialSearchState.category = category;
365-
}
354+
if (pathSegments.length > 0) {
355+
const lastSegment = pathSegments[pathSegments.length - 1];
356+
// Check if the last segment matches any category in the categories list
357+
category = categoriesList.find((cat) => cat.slug === lastSegment);
358+
if (category) {
359+
initialSearchState.category = category;
366360
}
367361
}
368362

@@ -629,11 +623,16 @@ export async function parseUrlToSearchOptions(
629623
* ```
630624
*/
631625
export async function loadProductsListSearchServiceConfig(
632-
input: string | { searchOptions: productsV3.V3ProductSearch; initialSearchState: InitialSearchState },
626+
input:
627+
| string
628+
| {
629+
searchOptions: productsV3.V3ProductSearch;
630+
initialSearchState: InitialSearchState;
631+
},
633632
): Promise<ProductsListSearchServiceConfig> {
634633
let initialSearchState: InitialSearchState;
635634

636-
if (typeof input === 'string') {
635+
if (typeof input === "string") {
637636
// URL input - parse it
638637
const categoriesListConfig = await loadCategoriesListServiceConfig();
639638
const { initialSearchState: parsedState } = await parseUrlToSearchOptions(

0 commit comments

Comments
 (0)