From c5e97143cde690b2e0ff14ed25b9194d7bdf4e5b Mon Sep 17 00:00:00 2001 From: "Brian.Jiang2021" Date: Thu, 11 May 2023 16:42:09 +0800 Subject: [PATCH] fix: quick order add search https://bigc-b2b.atlassian.net/browse/BUN-969 --- .../src/pages/quickorder/Quickorder.tsx | 2 +- .../quickorder/components/QuickOrderPad.tsx | 100 +++++++++++++++++- .../storefront/src/pages/quote/QuoteDraft.tsx | 8 +- .../components/SearchProduct.tsx | 4 +- 4 files changed, 106 insertions(+), 8 deletions(-) diff --git a/apps/storefront/src/pages/quickorder/Quickorder.tsx b/apps/storefront/src/pages/quickorder/Quickorder.tsx index eeb78ec8..0b07fa70 100644 --- a/apps/storefront/src/pages/quickorder/Quickorder.tsx +++ b/apps/storefront/src/pages/quickorder/Quickorder.tsx @@ -60,7 +60,7 @@ function Quickorder() { pl: isMobile ? '0px !important' : '16px', }} > - {role !== 2 && } + {role !== 2 && } diff --git a/apps/storefront/src/pages/quickorder/components/QuickOrderPad.tsx b/apps/storefront/src/pages/quickorder/components/QuickOrderPad.tsx index 5fd1ac2a..bb9c1531 100644 --- a/apps/storefront/src/pages/quickorder/components/QuickOrderPad.tsx +++ b/apps/storefront/src/pages/quickorder/components/QuickOrderPad.tsx @@ -14,9 +14,16 @@ import { useMobile } from '@/hooks' import { addProductToCart, createCart, getCartInfo } from '@/shared/service/bc' import { snackbar } from '@/utils' +import SearchProduct from '../../shoppingListDetails/components/SearchProduct' + import QuickAdd from './QuickAdd' -export default function QuickOrderPad() { +interface QuickOrderPadProps { + isB2BUser: boolean +} + +export default function QuickOrderPad(props: QuickOrderPadProps) { + const { isB2BUser } = props const [isMobile] = useMobile() const [isOpenBulkLoadCSV, setIsOpenBulkLoadCSV] = useState(false) @@ -308,6 +315,90 @@ export default function QuickOrderPad() { } } + const handleVerifyProduct = (products: CustomFieldItems) => { + const { + variantId, + variants, + inventoryLevel, + inventoryTracking, + orderQuantityMaximum, + orderQuantityMinimum, + quantity, + sku, + } = products + const isStock = inventoryTracking !== 'none' + let purchasingDisabled = false + let stock = inventoryLevel + let productSku = sku + + const currentVariant = variants.find( + (variant: CustomFieldItems) => +variant.variant_id === +variantId + ) + if (currentVariant) { + purchasingDisabled = currentVariant.purchasing_disabled + stock = + inventoryTracking === 'variant' ? currentVariant.inventory_level : stock + productSku = currentVariant.sku || sku + } + + if (purchasingDisabled) { + snackbar.error(`SKU ${productSku} cannot be purchased in online store.`, { + isClose: true, + }) + + return false + } + + if (isStock && +quantity > +stock) { + snackbar.error( + `SKU ${productSku} do not have enough stock, please change quantity.`, + { + isClose: true, + } + ) + + return false + } + + if (+orderQuantityMinimum > 0 && +quantity < orderQuantityMinimum) { + snackbar.error( + `You need to purchase a minimum of ${orderQuantityMinimum} of the ${productSku} per order.`, + { + isClose: true, + } + ) + + return false + } + + if (+orderQuantityMaximum > 0 && +quantity > +orderQuantityMaximum) { + snackbar.error( + `You need to purchase a maximum of ${orderQuantityMaximum} of the ${productSku} per order.`, + { + isClose: true, + } + ) + + return false + } + + return true + } + + const handleQuickSearchAddCart = async (productData: CustomFieldItems[]) => { + const currentProduct = productData[0] + const isPassVerify = handleVerifyProduct(currentProduct) + try { + if (isPassVerify) { + quickAddToList(productData) + } + } catch (error) { + console.error(error) + } + + return productData + } + useEffect(() => { if (productData?.length > 0) { setAddBtnText(`Add ${productData.length} products to cart`) @@ -331,6 +422,13 @@ export default function QuickOrderPad() { Quick order pad + + item.sku === node.variantSku ) - allPrice += +node.basePrice * node.quantity + allPrice += +(node?.basePrice || 0) * +(node?.quantity || 0) - allTaxPrice += +node.taxPrice * node.quantity + allTaxPrice += +(node?.taxPrice || 0) * +(node?.quantity || 0) const items = { productId: node.productsSearch.id, sku: node.variantSku, - basePrice: (+node.basePrice).toFixed(2), + basePrice: (+(node?.basePrice || 0)).toFixed(2), // taxPrice: (+node.taxPrice).toFixed(2), discount: '0.00', - offeredPrice: (+node.basePrice).toFixed(2), + offeredPrice: (+(node?.basePrice || 0)).toFixed(2), quantity: node.quantity, variantId: varantsItem.variant_id, imageUrl: node.primaryImage, diff --git a/apps/storefront/src/pages/shoppingListDetails/components/SearchProduct.tsx b/apps/storefront/src/pages/shoppingListDetails/components/SearchProduct.tsx index 05e312af..b1c81fcc 100644 --- a/apps/storefront/src/pages/shoppingListDetails/components/SearchProduct.tsx +++ b/apps/storefront/src/pages/shoppingListDetails/components/SearchProduct.tsx @@ -13,7 +13,7 @@ import ChooseOptionsDialog from './ChooseOptionsDialog' import ProductListDialog from './ProductListDialog' interface SearchProductProps { - updateList: () => void + updateList?: () => void addToList: (products: CustomFieldItems[]) => CustomFieldItems searchDialogTitle?: string addButtonText?: string @@ -21,7 +21,7 @@ interface SearchProductProps { } export default function SearchProduct({ - updateList, + updateList = () => {}, addToList, searchDialogTitle, addButtonText,