Improved Categories

This commit is contained in:
Bel Curcio 2021-05-28 18:32:20 -03:00
parent b47021c4a0
commit f1b4368149
3 changed files with 19 additions and 9 deletions

View File

@ -4,6 +4,8 @@ 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'
import { CategoryTreeItem } from '@framework/schema'
// Get 3 levels of categories
export const getSiteInfoQuery = /* GraphQL */ `
@ -49,6 +51,8 @@ export type CategoriesTree = NonNullable<
GetSiteInfoQuery['site']['categoryTree']
>
export type CategoryItem = Pick<CategoryTreeItem, 'entityId' | 'name' | 'path'>
export type BrandEdge = NonNullable<
NonNullable<GetSiteInfoQuery['site']['brands']['edges']>[0]
>
@ -57,7 +61,7 @@ export type Brands = BrandEdge[]
export type GetSiteInfoResult<
T extends { categories: any[]; brands: any[] } = {
categories: CategoriesTree
categories: Category[]
brands: Brands
}
> = T
@ -95,11 +99,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>),
}
}

View File

@ -13,12 +13,9 @@ import useSearch from '@framework/product/use-search'
import getAllPages from '@framework/common/get-all-pages'
import getSiteInfo from '@framework/common/get-site-info'
import getSlug from '@lib/get-slug'
import rangeMap from '@lib/range-map'
// TODO(bc) Remove this. This should come from the API
import getSlug from '@lib/get-slug'
// TODO (bc) : Remove or standarize this.
const SORT = Object.entries({
'latest-desc': 'Latest arrivals',
'trending-desc': 'Trending',

View File

@ -22,8 +22,8 @@
"@components/*": ["components/*"],
"@commerce": ["framework/commerce"],
"@commerce/*": ["framework/commerce/*"],
"@framework": ["framework/vendure"],
"@framework/*": ["framework/vendure/*"]
"@framework": ["framework/bigcommerce"],
"@framework/*": ["framework/bigcommerce/*"]
}
},
"include": ["next-env.d.ts", "**/*.d.ts", "**/*.ts", "**/*.tsx", "**/*.js"],