Skip to content

Commit

Permalink
fix: quick order add search
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianJiang2021 committed May 18, 2023
1 parent c3688f9 commit c5e9714
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 8 deletions.
2 changes: 1 addition & 1 deletion apps/storefront/src/pages/quickorder/Quickorder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function Quickorder() {
pl: isMobile ? '0px !important' : '16px',
}}
>
{role !== 2 && <QuickOrderPad />}
{role !== 2 && <QuickOrderPad isB2BUser={isB2BUser} />}
</Grid>
</Grid>
</Box>
Expand Down
100 changes: 99 additions & 1 deletion apps/storefront/src/pages/quickorder/components/QuickOrderPad.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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`)
Expand All @@ -331,6 +422,13 @@ export default function QuickOrderPad() {
Quick order pad
</Typography>

<SearchProduct
addToList={handleQuickSearchAddCart}
searchDialogTitle="Quick order"
addButtonText="Add to cart"
isB2BUser={isB2BUser}
/>

<Divider />

<QuickAdd
Expand Down
8 changes: 4 additions & 4 deletions apps/storefront/src/pages/quote/QuoteDraft.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -460,17 +460,17 @@ function QuoteDraft({ setOpenPage }: QuoteDraftProps) {
(item: CustomFieldItems) => 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import ChooseOptionsDialog from './ChooseOptionsDialog'
import ProductListDialog from './ProductListDialog'

interface SearchProductProps {
updateList: () => void
updateList?: () => void
addToList: (products: CustomFieldItems[]) => CustomFieldItems
searchDialogTitle?: string
addButtonText?: string
isB2BUser: boolean
}

export default function SearchProduct({
updateList,
updateList = () => {},
addToList,
searchDialogTitle,
addButtonText,
Expand Down

0 comments on commit c5e9714

Please sign in to comment.