mirror of
https://github.com/vercel/commerce.git
synced 2025-07-23 04:36:49 +00:00
get veggie
This commit is contained in:
@@ -5,7 +5,7 @@ import { normalizeSearchResult } from '../../utils/normalize'
|
||||
import { getAllProductsQuery } from '../../utils/queries/get-all-products-query'
|
||||
import { OperationContext } from '@commerce/api/operations'
|
||||
|
||||
export type ProductVariables = { first?: number, facetValueIds?: string[] }
|
||||
export type ProductVariables = { first?: number, facetValueIds?: string[], collectionSlug?:string }
|
||||
|
||||
export default function getAllProductsOperation({
|
||||
commerce,
|
||||
@@ -31,6 +31,7 @@ export default function getAllProductsOperation({
|
||||
input: {
|
||||
take: vars.first,
|
||||
facetValueIds: vars.facetValueIds,
|
||||
collectionSlug : vars.collectionSlug,
|
||||
groupByProduct: true,
|
||||
},
|
||||
}
|
||||
|
@@ -1,20 +1,21 @@
|
||||
import { ProductVariables } from '@framework/api/operations/get-all-products';
|
||||
import { Product } from '@framework/schema';
|
||||
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 HomeSpice from 'src/components/modules/home/HomeSpice/HomeSpice';
|
||||
import { getAllFeaturedFacetId, getFreshProductFacetId } from 'src/utils/funtion.utils';
|
||||
import { FACET } from 'src/utils/constanst.utils';
|
||||
import { getAllFeaturedFacetId, getFacetIdByName, getFreshProductFacetId } from 'src/utils/funtion.utils';
|
||||
|
||||
interface Props {
|
||||
freshProducts: Product[],
|
||||
featuredProducts: Product[],
|
||||
|
||||
veggie: Product[],
|
||||
facets:Facet[]
|
||||
}
|
||||
export default function Home({ freshProducts, featuredProducts }: Props) {
|
||||
console.log("total: ", freshProducts.length, featuredProducts.length)
|
||||
console.log("rs: ", freshProducts, featuredProducts)
|
||||
export default function Home({ veggie, facets }: Props) {
|
||||
// console.log("total: ", freshProducts.length, featuredProducts.length)
|
||||
console.log("rs: ", veggie)
|
||||
return (
|
||||
<>
|
||||
<HomeBanner />
|
||||
@@ -47,36 +48,42 @@ export async function getStaticProps({
|
||||
preview,
|
||||
})
|
||||
|
||||
|
||||
const freshProductvariables: ProductVariables = {}
|
||||
const freshFacetId = getFreshProductFacetId(facets)
|
||||
|
||||
if (freshFacetId) {
|
||||
freshProductvariables.facetValueIds = [freshFacetId]
|
||||
// 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]
|
||||
}
|
||||
// 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 allFeaturedFacetId = getAllFeaturedFacetId(facets)
|
||||
// const featuredProductsPromise = commerce.getAllProducts({
|
||||
// variables: {
|
||||
// facetValueIds: allFeaturedFacetId
|
||||
// },
|
||||
// config,
|
||||
// preview,
|
||||
// })
|
||||
|
||||
|
||||
try {
|
||||
const rs = await Promise.all([freshProductsPromise, featuredProductsPromise])
|
||||
const rs = await Promise.all([freshProductsPromise])
|
||||
// const rs = await Promise.all([freshProductsPromise, featuredProductsPromise])
|
||||
|
||||
return {
|
||||
props: {
|
||||
freshProducts: rs[0].products,
|
||||
featuredProducts: rs[1].products
|
||||
veggie: rs[0].products,
|
||||
// featuredProducts: rs[1].products
|
||||
facets
|
||||
},
|
||||
revalidate: 60,
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
import React from 'react';
|
||||
import { useCartDrawer } from 'src/components/contexts';
|
||||
import useGetActiveOrder from 'src/components/hooks/useGetActiveOrder';
|
||||
import useGetActiveOrder from 'src/components/hooks/cart/useGetActiveOrder';
|
||||
import { PRODUCT_CART_DATA_TEST } from 'src/utils/demo-data';
|
||||
import { DrawerCommon } from '..';
|
||||
import s from './CartDrawer.module.scss';
|
||||
|
2
src/components/hooks/cart/index.ts
Normal file
2
src/components/hooks/cart/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export { default as useAddProductToCart } from './useAddProductToCart'
|
||||
export { default as useGetActiveOrder } from './useGetActiveOrder'
|
40
src/components/hooks/cart/useAddProductToCart.tsx
Normal file
40
src/components/hooks/cart/useAddProductToCart.tsx
Normal file
@@ -0,0 +1,40 @@
|
||||
import { useState } from 'react'
|
||||
import { CommonError } from 'src/domains/interfaces/CommonError'
|
||||
import rawFetcher from 'src/utils/rawFetcher'
|
||||
import { AddItemToOrderMutation, AddItemToOrderMutationVariables } from '@framework/schema'
|
||||
import { errorMapping } from 'src/utils/errrorMapping'
|
||||
import { useGetActiveOrder } from '.'
|
||||
import { addItemToOrderMutation } from '@framework/utils/mutations/add-item-to-order-mutation'
|
||||
|
||||
const useAddProductToCart = () => {
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [error, setError] = useState<CommonError | null>(null)
|
||||
const { mutate } = useGetActiveOrder()
|
||||
|
||||
const addProduct = (options:AddItemToOrderMutationVariables,
|
||||
fCallBack: (isSuccess: boolean, message?: string) => void
|
||||
) => {
|
||||
setError(null)
|
||||
setLoading(true)
|
||||
rawFetcher<AddItemToOrderMutation>({
|
||||
query: addItemToOrderMutation ,
|
||||
variables: options,
|
||||
})
|
||||
.then(({ data }) => {
|
||||
if (data.addItemToOrder.__typename !== "Order") {
|
||||
throw CommonError.create(errorMapping(data.addItemToOrder.message), data.addItemToOrder.errorCode)
|
||||
}
|
||||
mutate()
|
||||
fCallBack(true)
|
||||
})
|
||||
.catch((error) => {
|
||||
setError(error)
|
||||
fCallBack(false, error.message)
|
||||
})
|
||||
.finally(() => setLoading(false))
|
||||
}
|
||||
|
||||
return { loading, addProduct, error }
|
||||
}
|
||||
|
||||
export default useAddProductToCart
|
@@ -1,3 +1,5 @@
|
||||
import { FacetConstant } from "./types.utils"
|
||||
|
||||
export const BLUR_DATA_IMG = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mN8fBIAApUBruKYvzsAAAAASUVORK5CYII='
|
||||
|
||||
export const SOCIAL_LINKS = {
|
||||
@@ -113,6 +115,10 @@ export const FACET = {
|
||||
PARENT_NAME: 'Featured',
|
||||
FRESH: 'Fresh',
|
||||
BEST_SELLERS: 'Best seller'
|
||||
},
|
||||
CATEGORY: {
|
||||
PARENT_NAME:"category",
|
||||
VEGGIE:"veggie"
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -21,6 +21,13 @@ export function getFreshProductFacetId(facets: Facet[]) {
|
||||
return freshFacetValue?.id
|
||||
}
|
||||
|
||||
export function getFacetIdByName(facets: Facet[], facetName: string, valueName:string) {
|
||||
const featuredFacet = facets.find((item: Facet) => item.name === facetName)
|
||||
const freshFacetValue = featuredFacet?.values.find((item: FacetValue) => item.name === valueName)
|
||||
return freshFacetValue?.id
|
||||
}
|
||||
|
||||
|
||||
export function getAllFeaturedFacetId(facets: Facet[]) {
|
||||
const featuredFacet = facets.find((item: Facet) => item.name === FACET.FEATURE.PARENT_NAME)
|
||||
const rs = featuredFacet?.values.map((item: FacetValue) => item.id)
|
||||
|
@@ -53,3 +53,12 @@ export type filterContextType = {
|
||||
open: () => void;
|
||||
close: () => void;
|
||||
};
|
||||
|
||||
export interface StringMap { [key: string]: string; }
|
||||
|
||||
export interface FacetMap extends StringMap{
|
||||
PARENT_NAME: string
|
||||
}
|
||||
export interface FacetConstant{
|
||||
[key: string]: FacetMap;
|
||||
}
|
Reference in New Issue
Block a user