-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8528d9a
commit 11e6b7b
Showing
13 changed files
with
559 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import {useMemo} from 'react'; | ||
import {type UserCollectionFilterFragment} from 'storefrontapi.generated'; | ||
|
||
export const useFilterCounts = ( | ||
filters: UserCollectionFilterFragment[], | ||
patternKey: string, | ||
): Record<string, number> => { | ||
return useMemo(() => { | ||
const pattern = new RegExp(`^${patternKey}-(.+)$`); | ||
const counts: Record<string, number> = {}; | ||
|
||
filters.forEach((filter) => { | ||
filter.values.forEach((value) => { | ||
const match = value.label.match(pattern); | ||
if (match) { | ||
const key = match[1]; | ||
counts[key] = value.count || 0; // default to 0 if not found | ||
} | ||
}); | ||
}); | ||
|
||
return counts; | ||
}, [filters, patternKey]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import {type ProductFilter} from '@shopify/hydrogen/storefront-api-types'; | ||
|
||
export function generateQuerySearch(filters: ProductFilter[]): string { | ||
const groupedFilters: Record<string, string[]> = {}; | ||
|
||
filters.forEach((filter) => { | ||
if (filter.tag) { | ||
const [type, value] = filter.tag.split('-'); | ||
if (!groupedFilters[type]) { | ||
groupedFilters[type] = []; | ||
} | ||
groupedFilters[type].push(`${type}-${value}`); | ||
} | ||
}); | ||
|
||
const queryParts: string[] = []; | ||
Object.keys(groupedFilters).forEach((key) => { | ||
if (groupedFilters[key].length > 1) { | ||
queryParts.push(`(${groupedFilters[key].join(' AND ')})`); | ||
} else { | ||
queryParts.push(groupedFilters[key][0]); | ||
} | ||
}); | ||
|
||
return queryParts.join(' AND '); | ||
} |
Oops, something went wrong.