mirror of
https://github.com/vercel/commerce.git
synced 2025-07-27 04:01:23 +00:00
get veggie
This commit is contained in:
@@ -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)
|
||||
|
@@ -52,4 +52,13 @@ export type filterContextType = {
|
||||
visible: boolean;
|
||||
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