diff --git a/framework/commerce/types/product.ts b/framework/commerce/types/product.ts index fec48a63d..39f594a7a 100644 --- a/framework/commerce/types/product.ts +++ b/framework/commerce/types/product.ts @@ -1,3 +1,4 @@ +import { CurrencyCode } from './../../vendure/schema.d'; import { FacetValueFilterInput, LogicalOperator, SearchResultSortParameter } from "@framework/schema" export type ProductImage = { @@ -46,6 +47,21 @@ export type Product = { options: ProductOption[] } +export type ProductCard = { + id: string + name: string + slug?: string + imageSrc: string + price: number + currencyCode: CurrencyCode + oldPrice?: number, + discount?: number + weight?: number + // TODO: collection + category?: string, + isNotSell?: boolean +} + export type SearchProductsBody = { search?: string categoryId?: string | number diff --git a/framework/vendure/utils/normalize.ts b/framework/vendure/utils/normalize.ts index b99aa5096..a5cb96c8a 100644 --- a/framework/vendure/utils/normalize.ts +++ b/framework/vendure/utils/normalize.ts @@ -1,24 +1,23 @@ -import { Product } from '@commerce/types/product' +import { Product, ProductCard } from '@commerce/types/product' import { Cart } from '@commerce/types/cart' import { CartFragment, SearchResultFragment } from '../schema' -export function normalizeSearchResult(item: SearchResultFragment): Product { - const imageUrl = item.productAsset?.preview ? item.productAsset?.preview + '?w=800&mode=crop' : '' +export function normalizeSearchResult(item: SearchResultFragment): ProductCard { return { + id: item.productId, name: item.productName, - description: item.description, slug: item.slug, - path: item.slug, - images: imageUrl ? [{ url: imageUrl }] : [], - price: { - // TODO: check price - value: (item.priceWithTax as any).min / 100, - currencyCode: item.currencyCode, - }, - // TODO: check product option - options: [], - sku: item.sku, + imageSrc: item.productAsset?.preview ? item.productAsset?.preview + '?w=800&mode=crop' : '', + price: (item.priceWithTax as any).min / 100, + currencyCode: item.currencyCode, + + // TODO: + // oldPrice: item.price + // discount + // isNotSell + // weight + // category } } diff --git a/pages/index.tsx b/pages/index.tsx index 55b5dd5a5..4fc797c26 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -1,27 +1,29 @@ +import { ProductCard } from '@commerce/types/product'; import { ProductVariables } from '@framework/api/operations/get-all-products'; import { Facet, Product } from '@framework/schema'; import commerce from '@lib/api/commerce'; import { ifError } from 'assert'; import { GetStaticPropsContext } from 'next'; import { Layout } from 'src/components/common'; -import { FeaturedProductsCarousel, HomeBanner, HomeCategories, HomeCollection, HomeCTA, HomeFeature, HomeRecipe, HomeSubscribe, HomeVideo } from 'src/components/modules/home'; +import { FeaturedProductsCarousel, FreshProducts, HomeBanner, HomeCategories, HomeCollection, HomeCTA, HomeFeature, HomeRecipe, HomeSubscribe, HomeVideo } from 'src/components/modules/home'; import HomeSpice from 'src/components/modules/home/HomeSpice/HomeSpice'; import { FACET } from 'src/utils/constanst.utils'; import { getAllFeaturedFacetId, getFacetIdByName, getFreshProductFacetId } from 'src/utils/funtion.utils'; interface Props { - veggie: Product[], + veggie: ProductCard[], facets:Facet[] + freshProducts: ProductCard[], + featuredProducts: ProductCard[], } -export default function Home({ veggie, facets }: Props) { - // console.log("total: ", freshProducts.length, featuredProducts.length) - console.log("rs: ", veggie) +export default function Home({ freshProducts, featuredProducts, veggie }: Props) { return ( <> - + + @@ -49,41 +51,45 @@ export async function getStaticProps({ }) const freshProductvariables: ProductVariables = {} - // const freshFacetId = getFreshProductFacetId(facets) - const veggieId = getFacetIdByName(facets,FACET.CATEGORY.PARENT_NAME,FACET.CATEGORY.VEGGIE) - console.log("veggieId",veggieId) - console.log("facets",facets) - if (veggieId) { - freshProductvariables.facetValueIds = [veggieId] + const freshFacetId = getFreshProductFacetId(facets) + if (freshFacetId) { + freshProductvariables.facetValueIds = [freshFacetId] } - // if (freshFacetId) { - // freshProductvariables.facetValueIds = [freshFacetId] - // } const freshProductsPromise = commerce.getAllProducts({ variables: freshProductvariables, config, preview, }) - // const allFeaturedFacetId = getAllFeaturedFacetId(facets) - // const featuredProductsPromise = commerce.getAllProducts({ - // variables: { - // facetValueIds: allFeaturedFacetId - // }, - // config, - // preview, - // }) + const veggieProductvariables: ProductVariables = {} + const veggieId = getFacetIdByName(facets,FACET.CATEGORY.PARENT_NAME,FACET.CATEGORY.VEGGIE) + if (veggieId) { + veggieProductvariables.facetValueIds = [veggieId] + } + const veggieProductsPromise = commerce.getAllProducts({ + variables: veggieProductvariables, + config, + preview, + }) + + const allFeaturedFacetId = getAllFeaturedFacetId(facets) + const featuredProductsPromise = commerce.getAllProducts({ + variables: { + facetValueIds: allFeaturedFacetId + }, + config, + preview, + }) try { - const rs = await Promise.all([freshProductsPromise]) - // const rs = await Promise.all([freshProductsPromise, featuredProductsPromise]) + const rs = await Promise.all([veggieProductsPromise,featuredProductsPromise,freshProductsPromise]) return { props: { - veggie: rs[0].products, - // featuredProducts: rs[1].products - facets + veggie: veggieId ? rs[0].products : [], + featuredProducts: rs[1].products, + freshProducts: freshFacetId ? rs[2].products : [], }, revalidate: 60, } diff --git a/public/assets/images/default_img.jpg b/public/assets/images/default_img.jpg new file mode 100644 index 000000000..f72d03596 Binary files /dev/null and b/public/assets/images/default_img.jpg differ diff --git a/src/components/common/ImgWithLink/ImgWithLink.tsx b/src/components/common/ImgWithLink/ImgWithLink.tsx index 4f61946e0..a31a14832 100644 --- a/src/components/common/ImgWithLink/ImgWithLink.tsx +++ b/src/components/common/ImgWithLink/ImgWithLink.tsx @@ -1,7 +1,7 @@ import React from 'react' import s from './ImgWithLink.module.scss' import Image from 'next/image' -import { BLUR_DATA_IMG } from 'src/utils/constanst.utils' +import { BLUR_DATA_IMG, DEFAULT_IMG } from 'src/utils/constanst.utils' export interface ImgWithLinkProps { src: string, @@ -12,7 +12,7 @@ export interface ImgWithLinkProps { const ImgWithLink = ({ src, alt, blurDataURL = BLUR_DATA_IMG }: ImgWithLinkProps) => { return (
- {alt}
- -
- -
+ + +
+ +
+
+ { + category &&
{category}
+ }
- -
{name}
+ + +
{name}
+
{weight}
-
{price}
+
{price} {currencyCode}
@@ -80,4 +89,4 @@ const ProductCard = ({ ) } -export default ProductCard +export default ProductCardComponent diff --git a/src/components/modules/home/FreshProducts/FreshProducts.tsx b/src/components/modules/home/FreshProducts/FreshProducts.tsx index b06138079..33b872032 100644 --- a/src/components/modules/home/FreshProducts/FreshProducts.tsx +++ b/src/components/modules/home/FreshProducts/FreshProducts.tsx @@ -1,101 +1,15 @@ +import { ProductCard } from '@commerce/types/product' import { Product } from '@framework/schema' import React from 'react' import { CollectionCarcousel } from '..' -import image5 from '../../../../../public/assets/images/image5.png' -import image6 from '../../../../../public/assets/images/image6.png' -import image7 from '../../../../../public/assets/images/image7.png' -import image8 from '../../../../../public/assets/images/image8.png' interface FreshProductsProps { - data: Product[] + data: ProductCard[] } -const dataTest = [ - { - name: 'Tomato', - weight: '250g', - category: 'VEGGIE', - price: 'Rp 27.500', - imageSrc: image5.src, - }, - { - name: 'Cucumber', - weight: '250g', - category: 'VEGGIE', - price: 'Rp 27.500', - imageSrc: image6.src, - }, - { - name: 'Carrot', - weight: '250g', - category: 'VEGGIE', - price: 'Rp 27.500', - imageSrc: image7.src, - }, - { - name: 'Salad', - weight: '250g', - category: 'VEGGIE', - price: 'Rp 27.500', - imageSrc: image8.src, - }, - { - name: 'Tomato', - weight: '250g', - category: 'VEGGIE', - price: 'Rp 27.500', - imageSrc: image5.src, - }, - { - name: 'Cucumber', - weight: '250g', - category: 'VEGGIE', - price: 'Rp 27.500', - imageSrc: image6.src, - }, - { - name: 'Tomato', - weight: '250g', - category: 'VEGGIE', - price: 'Rp 27.500', - imageSrc: image5.src, - }, - { - name: 'Cucumber', - weight: '250g', - category: 'VEGGIE', - price: 'Rp 27.500', - imageSrc: image6.src, - }, - { - name: 'Carrot', - weight: '250g', - category: 'VEGGIE', - price: 'Rp 27.500', - imageSrc: image7.src, - }, - { - name: 'Salad', - weight: '250g', - category: 'VEGGIE', - price: 'Rp 27.500', - imageSrc: image8.src, - }, - { - name: 'Tomato', - weight: '250g', - category: 'VEGGIE', - price: 'Rp 27.500', - imageSrc: image5.src, - }, - { - name: 'Cucumber', - weight: '250g', - category: 'VEGGIE', - price: 'Rp 27.500', - imageSrc: image6.src, - }, -] -const FreshProducts = ({data}: FreshProductsProps) => { +const FreshProducts = ({ data }: FreshProductsProps) => { + if (data.length === 0) { + return null + } return (
{ subtitle="Last call! Shop deep deals on 100+ bulk picks while you can." category={"veggie"} /> - - - - -
) } diff --git a/src/components/modules/home/HomeCollection/HomeCollection.tsx b/src/components/modules/home/HomeCollection/HomeCollection.tsx index bd1cd33d6..4753f9ebe 100644 --- a/src/components/modules/home/HomeCollection/HomeCollection.tsx +++ b/src/components/modules/home/HomeCollection/HomeCollection.tsx @@ -1,10 +1,13 @@ import React from 'react' +import { ProductCard } from '@commerce/types/product' import { CollectionCarcousel } from '..' import image5 from '../../../../../public/assets/images/image5.png' import image6 from '../../../../../public/assets/images/image6.png' import image7 from '../../../../../public/assets/images/image7.png' import image8 from '../../../../../public/assets/images/image8.png' -interface HomeCollectionProps {} +interface HomeCollectionProps { + data: ProductCard[] +} const dataTest = [ { name: 'Tomato', @@ -92,47 +95,39 @@ const dataTest = [ }, ] -const HomeCollection = (props: HomeCollectionProps) => { +const HomeCollection = ({data}: HomeCollectionProps) => { return (
-