diff --git a/apps/storefront/src/hooks/dom/utils.ts b/apps/storefront/src/hooks/dom/utils.ts index 3ea4c47b..a29a0202 100644 --- a/apps/storefront/src/hooks/dom/utils.ts +++ b/apps/storefront/src/hooks/dom/utils.ts @@ -33,7 +33,7 @@ import { import { conversionProductsList, -} from '../../pages/shoppingListDetails/shared/config' +} from '../../utils/b3Product/shared/config' import { B3AddToQuoteTip, diff --git a/apps/storefront/src/pages/pdp/PDP.tsx b/apps/storefront/src/pages/pdp/PDP.tsx index e174771f..14c2a3c2 100644 --- a/apps/storefront/src/pages/pdp/PDP.tsx +++ b/apps/storefront/src/pages/pdp/PDP.tsx @@ -42,7 +42,7 @@ import CreateShoppingList from '../orderDetail/components/CreateShoppingList' import { conversionProductsList, -} from '../shoppingListDetails/shared/config' +} from '../../utils/b3Product/shared/config' interface PDPProps { setOpenPage: Dispatch>, diff --git a/apps/storefront/src/pages/quickorder/components/QuickAdd.tsx b/apps/storefront/src/pages/quickorder/components/QuickAdd.tsx index b30e0b93..3838f8aa 100644 --- a/apps/storefront/src/pages/quickorder/components/QuickAdd.tsx +++ b/apps/storefront/src/pages/quickorder/components/QuickAdd.tsx @@ -35,7 +35,7 @@ import { import { getQuickAddRowFields, -} from '../../shoppingListDetails/shared/config' +} from '../../../utils/b3Product/shared/config' import { SimpleObject, diff --git a/apps/storefront/src/pages/quickorder/components/QuickOrderFooter.tsx b/apps/storefront/src/pages/quickorder/components/QuickOrderFooter.tsx index 439d2ba3..a59757a1 100644 --- a/apps/storefront/src/pages/quickorder/components/QuickOrderFooter.tsx +++ b/apps/storefront/src/pages/quickorder/components/QuickOrderFooter.tsx @@ -67,7 +67,7 @@ import { import { conversionProductsList, -} from '../../shoppingListDetails/shared/config' +} from '../../../utils/b3Product/shared/config' import { OrderShoppingList, diff --git a/apps/storefront/src/pages/quickorder/components/QuickorderTable.tsx b/apps/storefront/src/pages/quickorder/components/QuickorderTable.tsx index 760de2ef..8f3f31d0 100644 --- a/apps/storefront/src/pages/quickorder/components/QuickorderTable.tsx +++ b/apps/storefront/src/pages/quickorder/components/QuickorderTable.tsx @@ -61,7 +61,7 @@ import QuickOrderCard from './QuickOrderCard' import { conversionProductsList, -} from '../../shoppingListDetails/shared/config' +} from '../../../utils/b3Product/shared/config' import { B3Sping, diff --git a/apps/storefront/src/pages/quote/QuoteDetail.tsx b/apps/storefront/src/pages/quote/QuoteDetail.tsx index 2d247fbe..55cdd7b0 100644 --- a/apps/storefront/src/pages/quote/QuoteDetail.tsx +++ b/apps/storefront/src/pages/quote/QuoteDetail.tsx @@ -66,7 +66,7 @@ import { } from './shared/config' import { conversionProductsList, -} from '../shoppingListDetails/shared/config' +} from '../../utils/b3Product/shared/config' import { QuoteTermsAndConditions, } from './components/QuoteTermsAndConditions' diff --git a/apps/storefront/src/pages/quote/QuoteDraft.tsx b/apps/storefront/src/pages/quote/QuoteDraft.tsx index e1dd2b3a..b3d7c987 100644 --- a/apps/storefront/src/pages/quote/QuoteDraft.tsx +++ b/apps/storefront/src/pages/quote/QuoteDraft.tsx @@ -93,7 +93,7 @@ import { getProductOptionsFields, conversionProductsList, ListItemProps, -} from '../shoppingListDetails/shared/config' +} from '../../utils/b3Product/shared/config' import { getAccountFormFields, diff --git a/apps/storefront/src/pages/quote/components/AddToQuote.tsx b/apps/storefront/src/pages/quote/components/AddToQuote.tsx index 615cf3fd..845e80ee 100644 --- a/apps/storefront/src/pages/quote/components/AddToQuote.tsx +++ b/apps/storefront/src/pages/quote/components/AddToQuote.tsx @@ -39,7 +39,7 @@ import { import { conversionProductsList, -} from '../../shoppingListDetails/shared/config' +} from '../../../utils/b3Product/shared/config' import { SearchProduct, diff --git a/apps/storefront/src/pages/quote/components/QuoteTable.tsx b/apps/storefront/src/pages/quote/components/QuoteTable.tsx index a02484f2..5d373f64 100644 --- a/apps/storefront/src/pages/quote/components/QuoteTable.tsx +++ b/apps/storefront/src/pages/quote/components/QuoteTable.tsx @@ -46,7 +46,7 @@ import { import { getProductOptionsFields, -} from '../../shoppingListDetails/shared/config' +} from '../../../utils/b3Product/shared/config' interface ListItem { [key: string]: string diff --git a/apps/storefront/src/pages/quote/components/QuoteTableCard.tsx b/apps/storefront/src/pages/quote/components/QuoteTableCard.tsx index 75725a11..9e5641a1 100644 --- a/apps/storefront/src/pages/quote/components/QuoteTableCard.tsx +++ b/apps/storefront/src/pages/quote/components/QuoteTableCard.tsx @@ -20,7 +20,7 @@ import { import { getProductOptionsFields, -} from '../../shoppingListDetails/shared/config' +} from '../../../utils/b3Product/shared/config' interface QuoteTableCardProps { item: any, diff --git a/apps/storefront/src/pages/shoppingListDetails/ShoppingListDetails.tsx b/apps/storefront/src/pages/shoppingListDetails/ShoppingListDetails.tsx index 5fcd6eb3..54845dad 100644 --- a/apps/storefront/src/pages/shoppingListDetails/ShoppingListDetails.tsx +++ b/apps/storefront/src/pages/shoppingListDetails/ShoppingListDetails.tsx @@ -45,16 +45,16 @@ import { import { snackbar, getDefaultCurrencyInfo, + getNewProductsList, } from '@/utils' import { - conversionProductsList, ShoppingListInfoProps, CustomerInfoProps, ListItemProps, SearchProps, ProductsProps, -} from './shared/config' +} from '../../utils/b3Product/shared/config' import { ShoppingListDetailsContext, @@ -149,7 +149,6 @@ const ShoppingListDetails = ({ const isReadForApprove = shoppingListInfo?.status === 40 || shoppingListInfo?.status === 20 const { - currency_code: currencyCode, token: currencyToken, } = getDefaultCurrencyInfo() @@ -166,53 +165,6 @@ const ShoppingListDetails = ({ }) }, [id]) - const handleGetProductsById = async (listProducts: ListItemProps[]) => { - if (listProducts.length > 0) { - const productIds: number[] = [] - listProducts.forEach((item) => { - const { - node, - } = item - if (!productIds.includes(node.productId)) { - productIds.push(node.productId) - } - }) - const getProducts = isB2BUser ? searchB2BProducts : searchBcProducts - - try { - const { - productsSearch, - } = await getProducts({ - productIds, - currencyCode, - companyId: companyInfoId, - }) - - const newProductsSearch = conversionProductsList(productsSearch) - - listProducts.forEach((item) => { - const { - node, - } = item - - const productInfo = newProductsSearch.find((search: CustomFieldItems) => { - const { - id: productId, - } = search - - return node.productId === productId - }) - - node.productsSearch = productInfo || {} - }) - - return listProducts - } catch (err: any) { - snackbar.error(err) - } - } - } - const getShoppingListDetails = async (params: SearchProps) => { const getSLDetail = isB2BUser ? getB2BShoppingListDetails : getBcShoppingListDetails const infoKey = isB2BUser ? 'shoppingList' : 'customerShoppingList' @@ -231,7 +183,9 @@ const ShoppingListDetails = ({ }, } = shoppingListDetailInfo - const listProducts = await handleGetProductsById(edges) + const listProducts = await getNewProductsList(edges, isB2BUser, companyInfoId) + + console.log(listProducts, 'listProducts') if (isB2BUser) setCustomerInfo(shoppingListDetailInfo.customerInfo) setShoppingListInfo(shoppingListDetailInfo) diff --git a/apps/storefront/src/pages/shoppingListDetails/components/ChooseOptionsDialog.tsx b/apps/storefront/src/pages/shoppingListDetails/components/ChooseOptionsDialog.tsx index a4eaddf0..80c7f831 100644 --- a/apps/storefront/src/pages/shoppingListDetails/components/ChooseOptionsDialog.tsx +++ b/apps/storefront/src/pages/shoppingListDetails/components/ChooseOptionsDialog.tsx @@ -40,7 +40,7 @@ import { getProductOptionsFields, Base64, getOptionRequestData, -} from '../shared/config' +} from '../../../utils/b3Product/shared/config' import { searchB2BProducts, diff --git a/apps/storefront/src/pages/shoppingListDetails/components/QuickAdd.tsx b/apps/storefront/src/pages/shoppingListDetails/components/QuickAdd.tsx index bfaaf252..640b3a12 100644 --- a/apps/storefront/src/pages/shoppingListDetails/components/QuickAdd.tsx +++ b/apps/storefront/src/pages/shoppingListDetails/components/QuickAdd.tsx @@ -35,7 +35,7 @@ import { import { getQuickAddRowFields, -} from '../shared/config' +} from '../../../utils/b3Product/shared/config' import { SimpleObject, diff --git a/apps/storefront/src/pages/shoppingListDetails/components/ReAddToCart.tsx b/apps/storefront/src/pages/shoppingListDetails/components/ReAddToCart.tsx index f69b29c3..23a1d8d4 100644 --- a/apps/storefront/src/pages/shoppingListDetails/components/ReAddToCart.tsx +++ b/apps/storefront/src/pages/shoppingListDetails/components/ReAddToCart.tsx @@ -44,7 +44,7 @@ import { ProductsProps, addlineItems, getProductOptionsFields, -} from '../shared/config' +} from '../../../utils/b3Product/shared/config' interface successTipOptions{ message: string, diff --git a/apps/storefront/src/pages/shoppingListDetails/components/SearchProduct.tsx b/apps/storefront/src/pages/shoppingListDetails/components/SearchProduct.tsx index a446c248..90de8448 100644 --- a/apps/storefront/src/pages/shoppingListDetails/components/SearchProduct.tsx +++ b/apps/storefront/src/pages/shoppingListDetails/components/SearchProduct.tsx @@ -37,7 +37,7 @@ import { import { conversionProductsList, -} from '../shared/config' +} from '../../../utils/b3Product/shared/config' interface SearchProductProps { updateList: () => void, diff --git a/apps/storefront/src/pages/shoppingListDetails/components/ShoppingDetailCard.tsx b/apps/storefront/src/pages/shoppingListDetails/components/ShoppingDetailCard.tsx index 571d2a8f..c8190758 100644 --- a/apps/storefront/src/pages/shoppingListDetails/components/ShoppingDetailCard.tsx +++ b/apps/storefront/src/pages/shoppingListDetails/components/ShoppingDetailCard.tsx @@ -24,7 +24,7 @@ import { import { getProductOptionsFields, -} from '../shared/config' +} from '../../../utils/b3Product/shared/config' interface ShoppingDetailCardProps { item: any, diff --git a/apps/storefront/src/pages/shoppingListDetails/components/ShoppingDetailFooter.tsx b/apps/storefront/src/pages/shoppingListDetails/components/ShoppingDetailFooter.tsx index 6c5eb7d4..7142de27 100644 --- a/apps/storefront/src/pages/shoppingListDetails/components/ShoppingDetailFooter.tsx +++ b/apps/storefront/src/pages/shoppingListDetails/components/ShoppingDetailFooter.tsx @@ -34,7 +34,7 @@ import { import { ProductsProps, addlineItems, -} from '../shared/config' +} from '../../../utils/b3Product/shared/config' import { B3LinkTipContent, CustomButton, diff --git a/apps/storefront/src/pages/shoppingListDetails/components/ShoppingDetailTable.tsx b/apps/storefront/src/pages/shoppingListDetails/components/ShoppingDetailTable.tsx index 03ca7436..ebc4e70d 100644 --- a/apps/storefront/src/pages/shoppingListDetails/components/ShoppingDetailTable.tsx +++ b/apps/storefront/src/pages/shoppingListDetails/components/ShoppingDetailTable.tsx @@ -63,7 +63,7 @@ import ShoppingDetailCard from './ShoppingDetailCard' import { getProductOptionsFields, -} from '../shared/config' +} from '../../../utils/b3Product/shared/config' interface ListItem { [key: string]: string diff --git a/apps/storefront/src/pages/shoppingLists/ShoppingLists.tsx b/apps/storefront/src/pages/shoppingLists/ShoppingLists.tsx index 6f5ebf4c..230c9179 100644 --- a/apps/storefront/src/pages/shoppingLists/ShoppingLists.tsx +++ b/apps/storefront/src/pages/shoppingLists/ShoppingLists.tsx @@ -166,6 +166,7 @@ const shoppingLists = () => { edges, totalCount, }, } = await getShoppingLists(newParams) + return { edges, totalCount, diff --git a/apps/storefront/src/types/products.ts b/apps/storefront/src/types/products.ts index 584948f0..1b148b46 100644 --- a/apps/storefront/src/types/products.ts +++ b/apps/storefront/src/types/products.ts @@ -50,3 +50,129 @@ export interface ProductVariantSkuInfo{ variantId: string, variantSku: string, } + +interface OptionValue { + id: number; + label: string; + option_id: number; + option_display_name: string; +} + +interface Variant { + variant_id: number; + product_id: number; + sku: string; + option_values: OptionValue[]; + calculated_price: number; + image_url: string; + has_price_list: boolean; + bulk_prices: any[]; // not sure about the type + purchasing_disabled: boolean; + cost_price: number; + inventory_level: number; + bc_calculated_price: { + as_entered: number; + tax_inclusive: number; + tax_exclusive: number; + entered_inclusive: boolean; + }; +} + +interface Modifier { + id: number; + display_name: string; + type: string; + required: boolean; + config: any; // not sure about the type + option_values: any[]; // not sure about the type +} + +interface Option { + option_id: number; + display_name: string; + sort_order: number; + is_required: boolean; +} + +interface OptionV3 { + id: number; + product_id: number; + name: string; + display_name: string; + type: string; + sort_order: number; + option_values: OptionValue[]; +} + +interface ALlOptionValue { + id: number; + label: string; + sort_order: number; + value_data: { + colors?: string[]; + product_id?: number; + checked_value?: boolean; + } | null; + is_default: boolean; + adjusters?: { + price: { + adjuster: string; + adjuster_value: number; + } | null; + weight: { + adjuster: string; + adjuster_value: number; + } | null; + image_url: string; + purchasing_disabled: { + status: boolean; + message: string; + }; + } | null; +} + +export interface AllOptionProps { + id: number; + product_id?: number; + name: string; + display_name: string; + type: string; + sort_order: number; + option_values: ALlOptionValue[]; + config: { + product_list_adjusts_inventory?: boolean; + product_list_adjusts_pricing?: boolean; + product_list_shipping_calc?: string; + default_value?: string; + text_characters_limited?: boolean; + text_min_length?: number; + text_max_length?: number; + checkbox_label?: string; + checked_by_default?: boolean; + } | null; + required: boolean; + isVariantOption?: boolean; +} + +export interface Product { + id: number; + name: string; + sku: string; + costPrice: string; + channelId: number[], + inventoryLevel: number; + inventoryTracking: string; + availability: string; + orderQuantityMinimum: number; + orderQuantityMaximum: number; + variants: Variant[]; + currencyCode: string; + imageUrl: string; + modifiers: Modifier[]; + options: Option[]; + optionsV3: OptionV3[]; + allOptions: AllOptionProps + productUrl: string; + quantity: number; + [key:string]: any; +} diff --git a/apps/storefront/src/utils/b3AddToShoppingList.ts b/apps/storefront/src/utils/b3AddToShoppingList.ts index 07b6f41d..e5596bed 100644 --- a/apps/storefront/src/utils/b3AddToShoppingList.ts +++ b/apps/storefront/src/utils/b3AddToShoppingList.ts @@ -8,7 +8,7 @@ import { import { conversionProductsList, -} from '../pages/shoppingListDetails/shared/config' +} from './b3Product/shared/config' export const handleGetCurrentProductInfo = async (productId: number | string) => { const currencies = B3SStorage.get('currencies') diff --git a/apps/storefront/src/utils/b3Product.ts b/apps/storefront/src/utils/b3Product/b3Product.ts similarity index 60% rename from apps/storefront/src/utils/b3Product.ts rename to apps/storefront/src/utils/b3Product/b3Product.ts index cac6693d..b093259a 100644 --- a/apps/storefront/src/utils/b3Product.ts +++ b/apps/storefront/src/utils/b3Product/b3Product.ts @@ -1,5 +1,9 @@ +import { + Product, +} from '@/types/products' import { B3LStorage, + getDefaultCurrencyInfo, } from '@/utils' import { @@ -7,6 +11,11 @@ import { searchBcProducts, } from '@/shared/service/b2b' +import { + ListItemProps, + conversionProductsList, +} from './shared/config' + interface QuoteListitemProps { node: { id: number, @@ -153,10 +162,141 @@ const getQuickAddProductExtraPrice = (allOptions: CustomFieldItems[], newSelectO return additionalCalculatedPrices } +const getListModifierPrice = (allOptions, node) => { + const optionList = JSON.parse(node?.optionList || '[]') + console.log(optionList, 'optionList') + const modifierPrices: any = [] + if (optionList.length) { + optionList.forEach((option: any) => { + const itemOption = allOptions.find((item: any) => option.option_id.includes(item.id)) + if (itemOption && itemOption?.option_values && itemOption.option_values.length) { + const optionValues = itemOption.option_values.find((optionValue: any) => +optionValue.id === +option.option_value) + if (optionValues && optionValues?.adjusters && optionValues?.adjusters?.price) { + const { + price, + } = optionValues.adjusters + if (price) { + modifierPrices.push(price) + } + } + } + }) + } + + return modifierPrices +} + +const getNewProductsList = async (listProducts: ListItemProps[], isB2BUser: boolean, companyId: number | string) => { + const { + currency_code: currencyCode, + } = getDefaultCurrencyInfo() + if (listProducts.length > 0) { + const productIds: number[] = [] + listProducts.forEach((item) => { + const { + node, + } = item + if (!productIds.includes(node.productId)) { + productIds.push(node.productId) + } + }) + const getProducts = isB2BUser ? searchB2BProducts : searchBcProducts + + const { + productsSearch, + } = await getProducts({ + productIds, + currencyCode, + companyId, + }) + + const newProductsSearch: Partial[] = conversionProductsList(productsSearch) + + console.log(newProductsSearch, 'newProductsSearch') + + const picklistIds: number[] = [] + + listProducts.forEach((item) => { + const { + node, + } = item + + const productInfo = newProductsSearch.find((search: Product) => { + const { + id: productId, + } = search + + return node.productId === productId + }) + + // 获取相关的产品的id + const currentPicklistIds: number[] = [] + if (productInfo?.allOptions && productInfo?.allOptions.length) { + const picklist = productInfo.allOptions.find((item: any) => item.type === 'product_list_with_images') + if (picklist && picklist?.option_values?.length) { + picklist.option_values.forEach(((list: any) => { + const picklistProductId = list?.value_data?.product_id + if (picklistProductId) currentPicklistIds.push(picklistProductId) + if (!picklistIds.includes(list.productId) && picklistProductId) { + picklistIds.push(picklistProductId) + } + })) + } + } + // get modifier price + if (productInfo?.variants.length) { + const modifierPrices = getListModifierPrice(productInfo.allOptions, node) + node.modifierPrices = modifierPrices + } + + // get current price and tax price + const variantItem = productInfo?.variants.find((item: any) => item.sku === node.variantSku) + if (variantItem) { + node.currentProductPrices = variantItem.bc_calculated_price + } + + node.picklistIds = currentPicklistIds + + node.productsSearch = productInfo || {} + }) + + // 获取相关产品的集合 + if (picklistIds.length) { + const { + productsSearch: picklistProductsSearch, + } = await getProducts({ + productIds: picklistIds + currencyCode, + }) + const newpicklistProducts = conversionProductsList(picklistProductsSearch) + + listProducts.forEach((item) => { + const { + node, + } = item +· + const extraProductPrices: any = [] + if (node?.picklistIds?.length) { + node?.picklistIds.forEach((picklistId: number) => { + const picklistItem = newpicklistProducts.find((product: any) => +product.id === +picklistId) + if (picklistItem && picklistItem?.variants?.length) { + extraProductPrices.push(picklistItem.variants[0].bc_calculated_price) + } + }) + } + node.extraProductPrices = extraProductPrices + }) + } + + return listProducts + } +} + export { // getProductPrice, addQuoteDraftProduce, getModifiersPrice, getProductExtraPrice, getQuickAddProductExtraPrice, + getNewProductsList, } diff --git a/apps/storefront/src/pages/shoppingListDetails/shared/config.ts b/apps/storefront/src/utils/b3Product/shared/config.ts similarity index 97% rename from apps/storefront/src/pages/shoppingListDetails/shared/config.ts rename to apps/storefront/src/utils/b3Product/shared/config.ts index 9bdebc53..8002c83a 100644 --- a/apps/storefront/src/pages/shoppingListDetails/shared/config.ts +++ b/apps/storefront/src/utils/b3Product/shared/config.ts @@ -28,6 +28,18 @@ export interface CustomerInfoProps { userId: number | string; } +interface ProductPrices { + as_entered: number, + entered_inclusive: number, + tax_exclusive: number, + tax_inclusive: number, +} + +interface ModifierPrices { + adjuster: string, + adjuster_value: number +} + export interface ProductInfoProps { basePrice: number | string, baseSku: string, @@ -47,6 +59,11 @@ export interface ProductInfoProps { variantId: number, variantSku: string, productsSearch: CustomFieldItems, + picklistIds?: number[] + modifierPrices?: ModifierPrices[], + currentProductPrices?: ProductPrices, + extraProductPrices?: ProductPrices[], + [key: string]: any, } export interface ListItemProps { @@ -64,6 +81,7 @@ export interface SearchProps { first?: number, offset?: number, } + interface NodeProps { basePrice: number | string, baseSku: string, diff --git a/apps/storefront/src/utils/index.ts b/apps/storefront/src/utils/index.ts index 501cc11b..61ad60ad 100644 --- a/apps/storefront/src/utils/index.ts +++ b/apps/storefront/src/utils/index.ts @@ -78,7 +78,8 @@ export { getModifiersPrice, getProductExtraPrice, getQuickAddProductExtraPrice, -} from './b3Product' + getNewProductsList, +} from './b3Product/b3Product' export { getTemPlateConfig,