diff --git a/react/Summary.tsx b/react/Summary.tsx index 6b6d3d2b..0e070dfa 100644 --- a/react/Summary.tsx +++ b/react/Summary.tsx @@ -19,6 +19,7 @@ const Summary: FC = ({ classes }) => { } = useOrderForm() const [packagesSkuIds, setPackagesSkuIds] = useState([]) + const [sgrSkuIds, setSgrSkuIds] = useState([]) useEffect(() => { let isSubscribed = true @@ -27,7 +28,19 @@ const Summary: FC = ({ classes }) => { (res: PackagesSkuIds) => { if (res && isSubscribed) { try { - setPackagesSkuIds(Object.values(res.data)) + const { bagsSettings, sgrSettings } = res?.data ?? {} + + setPackagesSkuIds(Object.values(bagsSettings)) + + const allSkuIds: string[] = [] + + Object.values(sgrSettings).forEach(sgrType => { + if (sgrType?.skuIds) { + allSkuIds.push(...sgrType.skuIds) + } + }) + + setSgrSkuIds(allSkuIds) } catch (error) { console.error('Error in packages feature.', error) } @@ -54,13 +67,26 @@ const Summary: FC = ({ classes }) => { }, 0) }, [items, packagesSkuIds]) - let newTotalizers = totalizers + const sgrValue = useMemo(() => { + if (!sgrSkuIds.length) { + return + } + return items.reduce((total: number, item: OrderFormItem) => { + if (sgrSkuIds.includes(item.id)) { + return ( + total + ((item?.listPrice as number) ?? 0) * (item?.quantity ?? 1) + ) + } + return total + }, 0) + }, [items, sgrSkuIds]) - if (flegValue && typeof flegValue === 'number') { - newTotalizers = JSON.parse(JSON.stringify(totalizers)) + let newTotalizers = totalizers - const totalizerItems = newTotalizers.find((t: any) => t.id === 'Items') + newTotalizers = JSON.parse(JSON.stringify(totalizers)) + const totalizerItems = newTotalizers.find((t: any) => t.id === 'Items') + if (flegValue && typeof flegValue === 'number') { newTotalizers.push({ id: 'Packaging', name: 'Taxa ambalare', @@ -71,6 +97,16 @@ const Summary: FC = ({ classes }) => { totalizerItems.value -= flegValue ?? 0 } + if (sgrValue && typeof sgrValue === 'number') { + newTotalizers.push({ + id: 'SGR', + name: 'Garantie', + value: sgrValue, + __typename: 'Totalizer', + }) + totalizerItems.value -= sgrValue ?? 0 + } + const { handles } = useCssHandles(CSS_HANDLES, { classes }) const originalValue = diff --git a/react/components/MinicartIconButton.tsx b/react/components/MinicartIconButton.tsx index 50432586..efa4a5be 100644 --- a/react/components/MinicartIconButton.tsx +++ b/react/components/MinicartIconButton.tsx @@ -24,14 +24,16 @@ interface Props { const countCartItems = ( countMode: MinicartTotalItemsType, allItems: OrderFormItem[], - packagesSkuIds: string[] + packagesSkuIds: string[], + sgrSkuIds: string[] ) => { // Filter only main products, remove assembly items from the count const items = allItems.filter( item => item.parentItemIndex === null && item.productId && - !packagesSkuIds.includes(item.productId) + !packagesSkuIds.includes(item.productId) && + !sgrSkuIds.includes(item.productId) ) if (countMode === 'distinctAvailable') { @@ -69,6 +71,7 @@ const MinicartIconButton: React.FC = props => { const { open, openBehavior, openOnHoverProp } = useMinicartState() const dispatch = useMinicartDispatch() const [packagesSkuIds, setPackagesSkuIds] = useState([]) + const [sgrSkuIds, setSgrSkuIds] = useState([]) useEffect(() => { let isSubscribed = true @@ -77,7 +80,19 @@ const MinicartIconButton: React.FC = props => { (res: PackagesSkuIds) => { if (res && isSubscribed) { try { - setPackagesSkuIds(Object.values(res.data)) + const { bagsSettings, sgrSettings } = res?.data ?? {} + + setPackagesSkuIds(Object.values(bagsSettings)) + + const allSkuIds: string[] = [] + + Object.values(sgrSettings).forEach(sgrType => { + if (sgrType?.skuIds) { + allSkuIds.push(...sgrType.skuIds) + } + }) + + setSgrSkuIds(allSkuIds) } catch (error) { console.error('Error in packages feature.', error) } @@ -92,7 +107,8 @@ const MinicartIconButton: React.FC = props => { const quantity = countCartItems( itemCountMode, orderForm.items, - packagesSkuIds + packagesSkuIds, + sgrSkuIds ) const itemQuantity = loading ? 0 : quantity const { url: checkoutUrl } = useCheckoutURL() diff --git a/react/package.json b/react/package.json index 2fd0ccca..c4031c5d 100644 --- a/react/package.json +++ b/react/package.json @@ -17,24 +17,26 @@ "@vtex/test-tools": "^3.1.0", "@vtex/tsconfig": "^0.4.4", "apollo-cache-inmemory": "^1.6.5", + "auchan.checkout-summary": "https://narcissgr--auchanqa.myvtex.com/_v/private/typings/linked/v1/auchan.checkout-summary@0.0.1+build1702379716/public/@types/auchan.checkout-summary", + "auchan.minicart": "https://narcissgr--auchanqa.myvtex.com/_v/private/typings/linked/v1/auchan.minicart@0.0.1+build1702379665/public/@types/auchan.minicart", + "auchan.product-list": "https://narcissgr--auchanqa.myvtex.com/_v/private/typings/linked/v1/auchan.product-list@0.0.1+build1702379804/public/@types/auchan.product-list", "babel-eslint": "^10.1.0", "typescript": "3.9.7", "vtex.checkout-graphql": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.checkout-graphql@0.66.3/public/@types/vtex.checkout-graphql", "vtex.checkout-resources": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.checkout-resources@0.49.0/public/@types/vtex.checkout-resources", - "vtex.checkout-summary": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.checkout-summary@0.22.0/public/@types/vtex.checkout-summary", "vtex.css-handles": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.css-handles@1.0.0/public/@types/vtex.css-handles", "vtex.device-detector": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.device-detector@0.2.6/public/@types/vtex.device-detector", "vtex.flex-layout": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.flex-layout@0.21.3/public/@types/vtex.flex-layout", "vtex.order-items": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.order-items@0.13.23/public/@types/vtex.order-items", "vtex.order-manager": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.order-manager@0.12.0/public/@types/vtex.order-manager", "vtex.pixel-manager": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.pixel-manager@1.9.0/public/@types/vtex.pixel-manager", - "vtex.product-summary": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.product-summary@2.85.0/public/@types/vtex.product-summary", + "vtex.product-summary": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.product-summary@2.87.0/public/@types/vtex.product-summary", "vtex.react-portal": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.react-portal@0.4.1/public/@types/vtex.react-portal", "vtex.render-runtime": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.render-runtime@8.134.2/public/@types/vtex.render-runtime", "vtex.responsive-values": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.responsive-values@0.4.2/public/@types/vtex.responsive-values", "vtex.rich-text": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.rich-text@0.16.0/public/@types/vtex.rich-text", "vtex.sandbox": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.sandbox@0.5.1/public/@types/vtex.sandbox", - "vtex.store-components": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.store-components@3.169.5/public/@types/vtex.store-components", + "vtex.store-components": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.store-components@3.170.0/public/@types/vtex.store-components", "vtex.store-drawer": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.store-drawer@0.16.3/public/@types/vtex.store-drawer", "vtex.store-graphql": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.store-graphql@2.170.1/public/@types/vtex.store-graphql", "vtex.store-icons": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.store-icons@0.18.0/public/@types/vtex.store-icons", diff --git a/react/typings/packages-sku-ids.d.ts b/react/typings/packages-sku-ids.d.ts index e2d30422..ff982f0b 100644 --- a/react/typings/packages-sku-ids.d.ts +++ b/react/typings/packages-sku-ids.d.ts @@ -1,9 +1,25 @@ interface PackagesSkuIds { data: { - bioFlegBagId: string - insulationBagId: string - plasticBagId: string - paperBagId: string + bagsSettings: { + bioFlegBagId: string + insulationBagId: string + plasticBagId: string + paperBagId: string + } + sgrSettings: { + aluminumCanProducts: { + label: string + skuIds: string[] + } + glassBottleProducts: { + label: string + skuIds: string[] + } + plasticBottleProducts: { + label: string + skuIds: string[] + } + } } error: null | string }