mirror of
https://github.com/vercel/commerce.git
synced 2025-07-22 20:26:49 +00:00
Improved Categories (#339)
* Improved Categories * Improved Categories * Improved Categories * Improved Categories * Improved Categories * Improved Categories
This commit is contained in:
@@ -3,6 +3,8 @@ import type { RecursivePartial, RecursiveRequired } from '../api/utils/types'
|
||||
import filterEdges from '../api/utils/filter-edges'
|
||||
import { BigcommerceConfig, getConfig } from '../api'
|
||||
import { categoryTreeItemFragment } from '../api/fragments/category-tree'
|
||||
import { Category } from '@commerce/types'
|
||||
import getSlug from '@lib/get-slug'
|
||||
|
||||
// Get 3 levels of categories
|
||||
export const getSiteInfoQuery = /* GraphQL */ `
|
||||
@@ -56,7 +58,7 @@ export type Brands = BrandEdge[]
|
||||
|
||||
export type GetSiteInfoResult<
|
||||
T extends { categories: any[]; brands: any[] } = {
|
||||
categories: CategoriesTree
|
||||
categories: Category[]
|
||||
brands: Brands
|
||||
}
|
||||
> = T
|
||||
@@ -68,7 +70,7 @@ async function getSiteInfo(opts?: {
|
||||
}): Promise<GetSiteInfoResult>
|
||||
|
||||
async function getSiteInfo<
|
||||
T extends { categories: any[]; brands: any[] },
|
||||
T extends { categories: Category[]; brands: any[] },
|
||||
V = any
|
||||
>(opts: {
|
||||
query: string
|
||||
@@ -94,11 +96,20 @@ async function getSiteInfo({
|
||||
query,
|
||||
{ variables }
|
||||
)
|
||||
const categories = data.site?.categoryTree
|
||||
|
||||
let categories = data!.site!.categoryTree?.map(
|
||||
({ entityId, name, path }: any) => ({
|
||||
id: `${entityId}`,
|
||||
name,
|
||||
slug: getSlug(path),
|
||||
path,
|
||||
})
|
||||
)
|
||||
|
||||
const brands = data.site?.brands?.edges
|
||||
|
||||
return {
|
||||
categories: (categories as RecursiveRequired<typeof categories>) ?? [],
|
||||
categories: categories ?? [],
|
||||
brands: filterEdges(brands as RecursiveRequired<typeof brands>),
|
||||
}
|
||||
}
|
||||
|
@@ -6,7 +6,7 @@ export default useSearch as UseSearch<typeof handler>
|
||||
|
||||
export type SearchProductsInput = {
|
||||
search?: string
|
||||
categoryId?: number
|
||||
categoryId?: number | string
|
||||
brandId?: number
|
||||
sort?: string
|
||||
}
|
||||
|
Reference in New Issue
Block a user