diff --git a/pages/product/[slug].tsx b/pages/product/[slug].tsx index f3e31a655..24901277e 100644 --- a/pages/product/[slug].tsx +++ b/pages/product/[slug].tsx @@ -9,13 +9,13 @@ import { BLOGS_DATA_TEST, INGREDIENT_DATA_TEST, RECIPE_DATA_TEST } from 'src/uti import { getAllPromies } from 'src/utils/funtion.utils' import { PromiseWithKey } from 'src/utils/types.utils' -export default function Slug({ product, relevantProducts }: InferGetStaticPropsType) { +export default function Slug({ product, relevantProducts, collections }: InferGetStaticPropsType) { return <> - + @@ -43,7 +43,7 @@ export async function getStaticProps({ throw new Error(`Product with slug '${params!.slug}' not found`) } - // relevant product + // relevant product (filter by product detail's facetIds) const relevantFacetIds = product.facetValueIds if (relevantFacetIds && relevantFacetIds.length > 0) { const relevantProductsPromise = commerce.getAllProducts({ @@ -60,6 +60,15 @@ export async function getStaticProps({ } + // collection + const collectionsPromise = commerce.getAllCollections({ + variables: {}, + config, + preview, + }) + promisesWithKey.push({ key: 'collections', promise: collectionsPromise, keyResult: 'collections' }) + + try { const promises = getAllPromies(promisesWithKey) const rs = await Promise.all(promises) diff --git a/src/components/modules/home/FreshProducts/FreshProducts.tsx b/src/components/modules/home/FreshProducts/FreshProducts.tsx index cf773d495..6d30459f3 100644 --- a/src/components/modules/home/FreshProducts/FreshProducts.tsx +++ b/src/components/modules/home/FreshProducts/FreshProducts.tsx @@ -2,21 +2,13 @@ import { ProductCard } from '@commerce/types/product' import { Collection } from '@framework/schema' import React, { useMemo } from 'react' import { OPTION_ALL, QUERY_KEY, ROUTE } from 'src/utils/constanst.utils' +import { getCategoryNameFromCollectionId } from 'src/utils/funtion.utils' import { CollectionCarcousel } from '..' interface FreshProductsProps { data: ProductCard[] collections: Collection[] } -const getCategoryNameFromCollectionId = (colelctions: Collection[], collectionId?: string ) => { - if (!collectionId) { - return '' - } - - const collection = colelctions.find(item => item.id === collectionId) - return collection?.name || '' -} - const FreshProducts = ({ data, collections }: FreshProductsProps) => { const dataWithCategory = useMemo(() => { return data.map(item => { diff --git a/src/components/modules/product-detail/ReleventProducts/ReleventProducts.tsx b/src/components/modules/product-detail/ReleventProducts/ReleventProducts.tsx index d6480beb4..147115dc2 100644 --- a/src/components/modules/product-detail/ReleventProducts/ReleventProducts.tsx +++ b/src/components/modules/product-detail/ReleventProducts/ReleventProducts.tsx @@ -1,17 +1,34 @@ import { ProductCard } from '@commerce/types/product'; -import React from 'react'; +import { Collection } from '@framework/schema'; +import React, { useMemo } from 'react'; import ListProductWithInfo from 'src/components/common/ListProductWithInfo/ListProductWithInfo'; +import { getCategoryNameFromCollectionId } from 'src/utils/funtion.utils'; interface Props { data: ProductCard[] + collections: Collection[] + } -const ReleventProducts = ({ data }: Props) => { +const ReleventProducts = ({ data, collections }: Props) => { + const dataWithCategoryName = useMemo(() => { + return data.map(item => { + return { + ...item, + collection: getCategoryNameFromCollectionId(collections, item.collectionIds ? item.collectionIds[0] : undefined) + } + }) + }, [data, collections]) + + if (data.length === 0) { + return null + } + return ( ); }; diff --git a/src/utils/funtion.utils.ts b/src/utils/funtion.utils.ts index 9f712326d..408d9d0fd 100644 --- a/src/utils/funtion.utils.ts +++ b/src/utils/funtion.utils.ts @@ -1,5 +1,5 @@ import { Facet } from "@commerce/types/facet"; -import { FacetValue, SearchResultSortParameter } from './../../framework/vendure/schema.d'; +import { Collection, FacetValue, SearchResultSortParameter } from './../../framework/vendure/schema.d'; import { CODE_FACET_DISCOUNT, CODE_FACET_FEATURED, CODE_FACET_FEATURED_VARIANT, PRODUCT_SORT_OPTION_VALUE } from "./constanst.utils"; import { PromiseWithKey, SortOrder } from "./types.utils"; @@ -116,6 +116,15 @@ export function getFacetIdsFromCodes(facets: FacetValue[], codes?: string[]): st return ids } +export const getCategoryNameFromCollectionId = (colelctions: Collection[], collectionId?: string ) => { + if (!collectionId) { + return '' + } + + const collection = colelctions.find(item => item.id === collectionId) + return collection?.name || '' +} + export function getAllPromies(promies: PromiseWithKey[]) { return promies.map(item => item.promise) -} +} \ No newline at end of file