Skip to content

Commit

Permalink
fix: add quote draft products
Browse files Browse the repository at this point in the history
  • Loading branch information
kris-liu-smile authored and BrianJiang2021 committed May 18, 2023
1 parent 457c75f commit 88f46c3
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 61 deletions.
9 changes: 2 additions & 7 deletions apps/storefront/src/hooks/dom/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { searchB2BProducts, searchBcProducts } from '@/shared/service/b2b'
import { getCartInfoWithOptions } from '@/shared/service/bc'
import {
addQuoteDraftProduce,
addQuoteDraftProducts,
calculateProductListPrice,
getCalculatedProductPrice,
globalSnackbar,
Expand Down Expand Up @@ -264,17 +265,11 @@ const addQuoteToCart = (setOpenPage: DispatchProps) => {
}

newProduct.push(quoteListitem)

// addQuoteDraftProduce(quoteListitem, quantity, optionsList || [])
})

await calculateProductListPrice(newProduct, '2')

newProduct.forEach((product: CustomFieldItems) => {
const { optionList, quantity } = product.node

addQuoteDraftProduce(product, quantity, JSON.parse(optionList) || [])
})
addQuoteDraftProducts(newProduct)

globalSnackbar.success('Product was added to your quote.', {
jsx: () =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
} from '@/shared/service/b2b/graphql/product'
import { addProductToCart, createCart, getCartInfo } from '@/shared/service/bc'
import {
addQuoteDraftProduce,
addQuoteDraftProducts,
calculateProductListPrice,
currencyFormat,
getProductPriceIncTax,
Expand Down Expand Up @@ -330,13 +330,14 @@ function QuickOrderFooter(props: QuickOrderFooterProps) {
})

await calculateProductListPrice(newProducts, '2')
newProducts.forEach((product: CustomFieldItems) => {
addQuoteDraftProduce(
product,
product.node.quantity,
JSON.parse(product.node.optionList) || []
)
})
addQuoteDraftProducts(newProducts)
// newProducts.forEach((product: CustomFieldItems) => {
// addQuoteDraftProduce(
// product,
// product.node.quantity,
// JSON.parse(product.node.optionList) || []
// )
// })
if (isSuccess) {
snackbar.success('', {
jsx: successTip({
Expand Down
24 changes: 2 additions & 22 deletions apps/storefront/src/pages/quote/QuoteDraft.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
import { store } from '@/store'
import { AddressItemType, BCAddressItemType } from '@/types/address'
import {
addQuoteDraftProduce,
addQuoteDraftProducts,
B3LStorage,
B3SStorage,
getDefaultCurrencyInfo,
Expand Down Expand Up @@ -338,27 +338,7 @@ function QuoteDraft({ setOpenPage }: QuoteDraftProps) {
}

const addToQuote = (products: CustomFieldItems[]) => {
products.forEach((product) => {
const {
optionList,
quantity,
variantSku,
productsSearch: { variants },
basePrice,
} = product.node
const variantItem = variants.find(
(item: CustomFieldItems) => item.sku === variantSku
)

product.node.basePrice = basePrice
product.node.tax =
variantItem.bc_calculated_price.tax_inclusive -
variantItem.bc_calculated_price.tax_exclusive

const newOptionList = JSON.parse(optionList) || []

addQuoteDraftProduce(product, quantity, newOptionList)
})
addQuoteDraftProducts(products)

quoteTableRef.current?.refreshList()
}
Expand Down
11 changes: 3 additions & 8 deletions apps/storefront/src/pages/quote/components/AddToQuote.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { B3CollapseContainer, B3Upload, CustomButton } from '@/components'
import { PRODUCT_DEFAULT_IMAGE } from '@/constants'
import { searchB2BProducts, searchBcProducts } from '@/shared/service/b2b'
import {
addQuoteDraftProduce,
addQuoteDraftProducts,
calculateProductListPrice,
snackbar,
} from '@/utils'
Expand Down Expand Up @@ -220,13 +220,8 @@ export default function AddToQuote(props: AddToListProps) {

if (isSuccess) {
await calculateProductListPrice(newProducts, '2')
newProducts.forEach((item: CustomFieldItems) => {
addQuoteDraftProduce(
item,
+item.node.quantity,
JSON.parse(item.node.optionList) || []
)
})

addQuoteDraftProducts(newProducts)
snackbar.success('Products were added to your quote.', {
isClose: true,
})
Expand Down
70 changes: 54 additions & 16 deletions apps/storefront/src/utils/b3Product/b3Product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -810,29 +810,66 @@ const setModifierQtyPrice = async (
}
}

const compareOption = (
langList: CustomFieldItems[],
shortList: CustomFieldItems[]
) => {
let flag = true
langList.forEach((item: CustomFieldItems) => {
const option = shortList.find(
(list: CustomFieldItems) => list.optionId === item.optionId
)
if (!option) {
if (item?.optionValue) flag = false
} else if (item.optionValue !== option.optionValue) flag = false
})
return flag
}

const addQuoteDraftProducts = (products: CustomFieldItems[]) => {
const b2bQuoteDraftList = B3LStorage.get('b2bQuoteDraftList') || []
if (b2bQuoteDraftList.length === 0) {
B3LStorage.set('b2bQuoteDraftList', products)
return
}
if (products.length) {
products.forEach((quoteProduct: CustomFieldItems) => {
const optionList = JSON.parse(quoteProduct.node.optionList)
const productIndex = b2bQuoteDraftList.findIndex(
(item: CustomFieldItems) => {
const oldOptionList = JSON.parse(item.node.optionList)
const isAdd =
oldOptionList.length > optionList.length
? compareOption(oldOptionList, optionList)
: compareOption(optionList, oldOptionList)

return item.node.variantSku === quoteProduct.node.variantSku && isAdd
}
)

if (productIndex !== -1) {
b2bQuoteDraftList[productIndex].node.quantity +=
quoteProduct.node.quantity
if (quoteProduct.node?.calculatedValue) {
b2bQuoteDraftList[productIndex].node.calculatedValue =
quoteProduct.node.calculatedValue
}
} else {
b2bQuoteDraftList.push(quoteProduct)
}
})
}

B3LStorage.set('b2bQuoteDraftList', b2bQuoteDraftList)
}

const addQuoteDraftProduce = async (
quoteListitem: CustomFieldItems,
qty: number,
optionList: CustomFieldItems[]
) => {
const b2bQuoteDraftList = B3LStorage.get('b2bQuoteDraftList') || []

const compareOption = (
langList: CustomFieldItems[],
shortList: CustomFieldItems[]
) => {
let flag = true
langList.forEach((item: CustomFieldItems) => {
const option = shortList.find(
(list: CustomFieldItems) => list.optionId === item.optionId
)
if (!option) {
if (item?.optionValue) flag = false
} else if (item.optionValue !== option.optionValue) flag = false
})
return flag
}

const index = b2bQuoteDraftList.findIndex(
(item: QuoteListitemProps) =>
item?.node?.variantSku === quoteListitem.node.variantSku
Expand Down Expand Up @@ -898,6 +935,7 @@ const calculateIsInclude = (price: number | string, tax: number | string) => {

export {
addQuoteDraftProduce,
addQuoteDraftProducts,
calculateIsInclude,
calculateProductListPrice,
getCalculatedParams,
Expand Down
1 change: 1 addition & 0 deletions apps/storefront/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { validatorRules } from './validatorRules'

export {
addQuoteDraftProduce,
addQuoteDraftProducts,
calculateIsInclude,
calculateProductListPrice,
getCalculatedParams,
Expand Down

0 comments on commit 88f46c3

Please sign in to comment.