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 { getAllProductsQuery } from '../../utils/queries/get-all-products-query'
|
||||||
import { OperationContext } from '@commerce/api/operations'
|
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({
|
export default function getAllProductsOperation({
|
||||||
commerce,
|
commerce,
|
||||||
@@ -31,6 +31,7 @@ export default function getAllProductsOperation({
|
|||||||
input: {
|
input: {
|
||||||
take: vars.first,
|
take: vars.first,
|
||||||
facetValueIds: vars.facetValueIds,
|
facetValueIds: vars.facetValueIds,
|
||||||
|
collectionSlug : vars.collectionSlug,
|
||||||
groupByProduct: true,
|
groupByProduct: true,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@@ -1,20 +1,21 @@
|
|||||||
import { ProductVariables } from '@framework/api/operations/get-all-products';
|
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 commerce from '@lib/api/commerce';
|
||||||
|
import { ifError } from 'assert';
|
||||||
import { GetStaticPropsContext } from 'next';
|
import { GetStaticPropsContext } from 'next';
|
||||||
import { Layout } from 'src/components/common';
|
import { Layout } from 'src/components/common';
|
||||||
import { FeaturedProductsCarousel, HomeBanner, HomeCategories, HomeCollection, HomeCTA, HomeFeature, HomeRecipe, HomeSubscribe, HomeVideo } from 'src/components/modules/home';
|
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 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 {
|
interface Props {
|
||||||
freshProducts: Product[],
|
veggie: Product[],
|
||||||
featuredProducts: Product[],
|
facets:Facet[]
|
||||||
|
|
||||||
}
|
}
|
||||||
export default function Home({ freshProducts, featuredProducts }: Props) {
|
export default function Home({ veggie, facets }: Props) {
|
||||||
console.log("total: ", freshProducts.length, featuredProducts.length)
|
// console.log("total: ", freshProducts.length, featuredProducts.length)
|
||||||
console.log("rs: ", freshProducts, featuredProducts)
|
console.log("rs: ", veggie)
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<HomeBanner />
|
<HomeBanner />
|
||||||
@@ -46,37 +47,43 @@ export async function getStaticProps({
|
|||||||
config,
|
config,
|
||||||
preview,
|
preview,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
const freshProductvariables: ProductVariables = {}
|
const freshProductvariables: ProductVariables = {}
|
||||||
const freshFacetId = getFreshProductFacetId(facets)
|
// const freshFacetId = getFreshProductFacetId(facets)
|
||||||
|
const veggieId = getFacetIdByName(facets,FACET.CATEGORY.PARENT_NAME,FACET.CATEGORY.VEGGIE)
|
||||||
if (freshFacetId) {
|
console.log("veggieId",veggieId)
|
||||||
freshProductvariables.facetValueIds = [freshFacetId]
|
console.log("facets",facets)
|
||||||
|
if (veggieId) {
|
||||||
|
freshProductvariables.facetValueIds = [veggieId]
|
||||||
}
|
}
|
||||||
|
// if (freshFacetId) {
|
||||||
|
// freshProductvariables.facetValueIds = [freshFacetId]
|
||||||
|
// }
|
||||||
const freshProductsPromise = commerce.getAllProducts({
|
const freshProductsPromise = commerce.getAllProducts({
|
||||||
variables: freshProductvariables,
|
variables: freshProductvariables,
|
||||||
config,
|
config,
|
||||||
preview,
|
preview,
|
||||||
})
|
})
|
||||||
|
|
||||||
const allFeaturedFacetId = getAllFeaturedFacetId(facets)
|
// const allFeaturedFacetId = getAllFeaturedFacetId(facets)
|
||||||
const featuredProductsPromise = commerce.getAllProducts({
|
// const featuredProductsPromise = commerce.getAllProducts({
|
||||||
variables: {
|
// variables: {
|
||||||
facetValueIds: allFeaturedFacetId
|
// facetValueIds: allFeaturedFacetId
|
||||||
},
|
// },
|
||||||
config,
|
// config,
|
||||||
preview,
|
// preview,
|
||||||
})
|
// })
|
||||||
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const rs = await Promise.all([freshProductsPromise, featuredProductsPromise])
|
const rs = await Promise.all([freshProductsPromise])
|
||||||
|
// const rs = await Promise.all([freshProductsPromise, featuredProductsPromise])
|
||||||
|
|
||||||
return {
|
return {
|
||||||
props: {
|
props: {
|
||||||
freshProducts: rs[0].products,
|
veggie: rs[0].products,
|
||||||
featuredProducts: rs[1].products
|
// featuredProducts: rs[1].products
|
||||||
|
facets
|
||||||
},
|
},
|
||||||
revalidate: 60,
|
revalidate: 60,
|
||||||
}
|
}
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { useCartDrawer } from 'src/components/contexts';
|
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 { PRODUCT_CART_DATA_TEST } from 'src/utils/demo-data';
|
||||||
import { DrawerCommon } from '..';
|
import { DrawerCommon } from '..';
|
||||||
import s from './CartDrawer.module.scss';
|
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 BLUR_DATA_IMG = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mN8fBIAApUBruKYvzsAAAAASUVORK5CYII='
|
||||||
|
|
||||||
export const SOCIAL_LINKS = {
|
export const SOCIAL_LINKS = {
|
||||||
@@ -113,6 +115,10 @@ export const FACET = {
|
|||||||
PARENT_NAME: 'Featured',
|
PARENT_NAME: 'Featured',
|
||||||
FRESH: 'Fresh',
|
FRESH: 'Fresh',
|
||||||
BEST_SELLERS: 'Best seller'
|
BEST_SELLERS: 'Best seller'
|
||||||
|
},
|
||||||
|
CATEGORY: {
|
||||||
|
PARENT_NAME:"category",
|
||||||
|
VEGGIE:"veggie"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -21,6 +21,13 @@ export function getFreshProductFacetId(facets: Facet[]) {
|
|||||||
return freshFacetValue?.id
|
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[]) {
|
export function getAllFeaturedFacetId(facets: Facet[]) {
|
||||||
const featuredFacet = facets.find((item: Facet) => item.name === FACET.FEATURE.PARENT_NAME)
|
const featuredFacet = facets.find((item: Facet) => item.name === FACET.FEATURE.PARENT_NAME)
|
||||||
const rs = featuredFacet?.values.map((item: FacetValue) => item.id)
|
const rs = featuredFacet?.values.map((item: FacetValue) => item.id)
|
||||||
|
@@ -52,4 +52,13 @@ export type filterContextType = {
|
|||||||
visible: boolean;
|
visible: boolean;
|
||||||
open: () => void;
|
open: () => void;
|
||||||
close: () => 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