From 82e03978b89938219958032efb1448cc76baa181 Mon Sep 17 00:00:00 2001 From: Saumit Date: Sat, 27 Sep 2025 02:14:26 +0530 Subject: Initial snapshot - OpenTelemetry demo 2.1.3 -f --- src/frontend/components/CartItems/CartItems.tsx | 82 +++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 src/frontend/components/CartItems/CartItems.tsx (limited to 'src/frontend/components/CartItems/CartItems.tsx') diff --git a/src/frontend/components/CartItems/CartItems.tsx b/src/frontend/components/CartItems/CartItems.tsx new file mode 100644 index 0000000..05e0279 --- /dev/null +++ b/src/frontend/components/CartItems/CartItems.tsx @@ -0,0 +1,82 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +import { useMemo } from 'react'; +import { useQuery, UseQueryOptions } from '@tanstack/react-query'; +import ApiGateway from '../../gateways/Api.gateway'; +import { Address, Money } from '../../protos/demo'; +import { useCurrency } from '../../providers/Currency.provider'; +import { IProductCartItem } from '../../types/Cart'; +import ProductPrice from '../ProductPrice'; +import CartItem from './CartItem'; +import * as S from './CartItems.styled'; + +interface IProps { + productList: IProductCartItem[]; + shouldShowPrice?: boolean; +} + +const CartItems = ({ productList, shouldShowPrice = true }: IProps) => { + const { selectedCurrency } = useCurrency(); + const address: Address = { + streetAddress: '1600 Amphitheatre Parkway', + city: 'Mountain View', + state: 'CA', + country: 'United States', + zipCode: '94043', + }; + + const queryKey = ['shipping', productList, selectedCurrency, address]; + const queryFn = () => ApiGateway.getShippingCost(productList, selectedCurrency, address); + const queryOptions: UseQueryOptions = { + queryKey, + queryFn, + }; + const { data: shippingConst = { units: 0, currencyCode: 'USD', nanos: 0 } } = useQuery(queryOptions); + + const total = useMemo(() => { + const nanoSum = + productList.reduce((acc, { product: { priceUsd: { nanos = 0 } = {} } }) => acc + Number(nanos), 0) + + shippingConst?.nanos || 0; + const nanoExceed = Math.floor(nanoSum / 1000000000); + + const unitSum = + productList.reduce((acc, { product: { priceUsd: { units = 0 } = {} } }) => acc + Number(units), 0) + + (shippingConst?.units || 0) + nanoExceed; + + return { + units: unitSum, + currencyCode: selectedCurrency, + nanos: nanoSum % 1000000000, + }; + }, [shippingConst?.units, shippingConst?.nanos, productList, selectedCurrency]); + + return ( + + + + + + + {productList.map(({ productId, product, quantity }) => ( + + ))} + {shouldShowPrice && ( + <> + + Shipping + + + + Total + + + + + + )} + + ); +}; + +export default CartItems; -- cgit v1.2.3