Skip to content

Commit

Permalink
fix: readd to cart issues
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlLiu2023 committed May 10, 2023
1 parent 69f0713 commit 088e4ad
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 46 deletions.
32 changes: 10 additions & 22 deletions apps/storefront/src/pages/quote/components/QuoteSummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from 'react'
import { Box, Card, CardContent, Grid, Typography } from '@mui/material'

import { store } from '@/store'
import { B3LStorage, currencyFormat } from '@/utils'

interface Summary {
Expand All @@ -28,39 +29,28 @@ const QuoteSummary = forwardRef((_, ref: Ref<unknown>) => {
...defaultSummary,
})

const {
global: { enteredInclusive: enteredInclusiveTax },
} = store.getState()

const priceCalc = (price: number) => parseFloat(price.toFixed(2))

const getSummary = () => {
const productList = B3LStorage.get('b2bQuoteDraftList') || []

const newQuoteSummary = productList.reduce(
(summary: Summary, product: CustomFieldItems) => {
const {
basePrice,
taxPrice: productTax,
quantity,
// additionalCalculatedPrices = [],
} = product.node
const { basePrice, taxPrice: productTax, quantity } = product.node

let { subtotal, grandTotal, tax } = summary

const { shipping } = summary

// let additionalCalculatedPriceTax = 0

// let additionalCalculatedPrice = 0

// additionalCalculatedPrices.forEach((item: CustomFieldItems) => {
// additionalCalculatedPriceTax += item.additionalCalculatedPriceTax
// additionalCalculatedPrice += item.additionalCalculatedPrice
// })

subtotal += priceCalc(
(+basePrice + +productTax) * quantity
)
tax += priceCalc(
(+productTax) * +quantity
(enteredInclusiveTax ? +basePrice : +basePrice + +productTax) *
quantity
)
tax += priceCalc(+productTax * +quantity)

grandTotal = subtotal + shipping

Expand Down Expand Up @@ -108,9 +98,7 @@ const QuoteSummary = forwardRef((_, ref: Ref<unknown>) => {
}}
>
<Typography>Sub total</Typography>
<Typography>
{priceFormat(quoteSummary.subtotal)}
</Typography>
<Typography>{priceFormat(quoteSummary.subtotal)}</Typography>
</Grid>

<Grid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import { PRODUCT_DEFAULT_IMAGE } from '@/constants'
import { useMobile } from '@/hooks'
import { addProductToCart, createCart, getCartInfo } from '@/shared/service/bc'
import { currencyFormat, snackbar } from '@/utils'
import { currencyFormat, setModifierQtyPrice, snackbar } from '@/utils'
import {
addlineItems,
getProductOptionsFields,
Expand Down Expand Up @@ -180,15 +180,22 @@ export default function ReAddToCart(props: ShoppingProductsProps) {

const itemStyle = isMobile ? mobileItemStyle : defaultItemStyle

const handleUpdateProductQty = (
const handleUpdateProductQty = async (
index: number,
value: number | string,
isValid: boolean
) => {
const newProduct: ProductsProps[] = [...products]
newProduct[index].node.quantity = +value
newProduct[index].isValid = isValid
setValidateFailureProducts(newProduct)
const caculateProduct = await setModifierQtyPrice(
newProduct[index].node,
+value
)
if (caculateProduct) {
;(newProduct[index] as CustomFieldItems).node = caculateProduct
setValidateFailureProducts(newProduct)
}
}

const handleCancelClicked = () => {
Expand Down Expand Up @@ -257,10 +264,11 @@ export default function ReAddToCart(props: ShoppingProductsProps) {
}
}

const handleClearNoStock = () => {
const handleClearNoStock = async () => {
const newProduct = products.filter(
(item: ProductsProps) => item.isStock === '0' || item.stock !== 0
)
const requestArr: Promise<any>[] = []
newProduct.forEach((product) => {
const {
node: { quantity },
Expand All @@ -281,8 +289,17 @@ export default function ReAddToCart(props: ShoppingProductsProps) {
}

product.isValid = true

const qty = product?.node?.quantity ? +product.node.quantity : 0

requestArr.push(setModifierQtyPrice(product.node, qty))
})

const productArr = await Promise.all(requestArr)

productArr.forEach((item, index) => {
newProduct[index].node = item
})
setValidateFailureProducts(newProduct)
}

Expand Down Expand Up @@ -378,17 +395,16 @@ export default function ReAddToCart(props: ShoppingProductsProps) {
const { isStock, maxQuantity, minQuantity, stock } = product

const {
quantity,
quantity = 1,
primaryImage,
productName,
variantSku,
optionList,
productsSearch,
baseAllPrice,
basePrice,
} = product.node

const price = +baseAllPrice !== 0 ? +baseAllPrice : +basePrice
const price = +basePrice
const total = (price * (quantity ? +quantity : 0)).toFixed(2)

const newProduct: any = {
Expand Down
57 changes: 40 additions & 17 deletions apps/storefront/src/utils/b3Product/b3Product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -812,26 +812,49 @@ const calculateProductListPrice = async (
}
}

const setModifierQtyPrice = async (product: CustomFieldItems, qty: number) => {
const { productsSearch, optionList, variantSku, calculatedValue } = product

const newProduct = await getCalculatedProductPrice(
{
productsSearch,
optionList: JSON.parse(optionList),
sku: variantSku,
qty,
},
calculatedValue
)
const setModifierQtyPrice = async (
product: CustomFieldItems,
qty: number,
isRequest = true
) => {
try {
const { productsSearch, optionList, variantSku, calculatedValue } = product

if (newProduct) {
newProduct.node.id = product.id
let newProduct: CustomFieldItems | string = {}

return newProduct.node
}
if (isRequest) {
newProduct = await getCalculatedProductPrice(
{
productsSearch,
optionList: JSON.parse(optionList),
sku: variantSku,
qty,
},
calculatedValue
)
} else {
newProduct = getCalculatedProductPrice(
{
productsSearch,
optionList: JSON.parse(optionList),
sku: variantSku,
qty,
},
calculatedValue
)
}

if (newProduct && (newProduct as CustomFieldItems)?.node?.id) {
;(newProduct as CustomFieldItems).node.id = product.id

return product
return (newProduct as CustomFieldItems).node
}

return product
} catch (e) {
console.log(e)
return product
}
}

const calculateIsInclude = (price: number | string, tax: number | string) => {
Expand Down

0 comments on commit 088e4ad

Please sign in to comment.