Merge branch 'release-stable' of github.com:KieIO/grocery-vercel-commerce into feature/m3-viewed-product

This commit is contained in:
DatNguyen
2021-10-18 14:38:40 +07:00
53 changed files with 1215 additions and 180 deletions

View File

@@ -3,13 +3,14 @@ import { Collection } from '@commerce/types/collection'
import { Product, ProductCard } from '@commerce/types/product'
import commerce from '@lib/api/commerce'
import { GetStaticPathsContext, GetStaticPropsContext, InferGetStaticPropsType } from 'next'
import { useEffect } from 'react'
import { useEffect, useState } from 'react'
import { Layout, RecipeDetail, RecommendedRecipes, RelevantBlogPosts } from 'src/components/common'
import { useLocalStorage } from 'src/components/hooks/useLocalStorage'
import { ProductInfoDetail, ReleventProducts, ViewedProducts } from 'src/components/modules/product-detail'
import { LOCAL_STORAGE_KEY, MAX_PRODUCT_CAROUSEL, REVALIDATE_TIME } from 'src/utils/constanst.utils'
import { BLOGS_DATA_TEST, INGREDIENT_DATA_TEST, RECIPE_DATA_TEST } from 'src/utils/demo-data'
import { getAllPromies } from 'src/utils/funtion.utils'
import { normalizeProductCard } from '@framework/utils/normalize';
import { PromiseWithKey } from 'src/utils/types.utils'
interface Props {
relevantProducts: ProductCard[],
@@ -17,23 +18,17 @@ interface Props {
collections: Collection[]
}
export default function Slug({ product, relevantProducts, collections }: Props) {
const [viewedProduct,setViewedProduct] = useLocalStorage<Product[]>(LOCAL_STORAGE_KEY.VIEWEDPRODUCT, []);
const [local,setLocal] = useLocalStorage<Product[]>(LOCAL_STORAGE_KEY.VIEWEDPRODUCT, []);
const [viewed, setViewed] = useState<ProductCard[]>([])
useEffect(() => {
// const local = localStorage.getItem(LOCAL_STORAGE_KEY.VIEWEDPRODUCT)
// if(local){
// const oldList:Product[] = JSON.parse(local)
// if(!oldList.find(p => p.id === product.id)){
// localStorage.setItem(LOCAL_STORAGE_KEY.VIEWEDPRODUCT,JSON.stringify([...oldList,product]))
// }
// }else{
// localStorage.setItem(LOCAL_STORAGE_KEY.VIEWEDPRODUCT,JSON.stringify([product]))
// }
if(viewedProduct){
if(!viewedProduct.find(p => p.id === product.id)){
setViewedProduct([...viewedProduct, product])
if(local){
if(!local.find(p => p.id === product.id)){
setLocal([...local, product])
}else{
setViewed(local.filter((p)=>p.id !== product.id).map((p)=>normalizeProductCard(p)))
}
}else{
setViewedProduct([product])
setLocal([product])
}
}, [product])
@@ -42,7 +37,7 @@ export default function Slug({ product, relevantProducts, collections }: Props)
<RecipeDetail ingredients={INGREDIENT_DATA_TEST} />
<RecommendedRecipes data={RECIPE_DATA_TEST} />
<ReleventProducts data={relevantProducts} collections={collections}/>
<ViewedProducts/>
<ViewedProducts data={viewed}/>
<RelevantBlogPosts data={BLOGS_DATA_TEST} title="relevent blog posts" />
</>
}