From cf4bbbfebfc1c8b0003c6558b3caf858ebb8378a Mon Sep 17 00:00:00 2001 From: Carter Socha <43380952+cartersocha@users.noreply.github.com> Date: Sun, 23 Oct 2022 12:23:41 -0700 Subject: [PATCH] fix currency bug (#522) --- src/frontend/components/CartItems/CartItems.tsx | 8 +++++--- src/frontend/gateways/Api.gateway.ts | 3 ++- src/frontend/pages/api/shipping.ts | 6 ++++-- src/frontend/services/ProductCatalog.service.ts | 2 +- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/frontend/components/CartItems/CartItems.tsx b/src/frontend/components/CartItems/CartItems.tsx index 2ff2624177..aee57995ab 100644 --- a/src/frontend/components/CartItems/CartItems.tsx +++ b/src/frontend/components/CartItems/CartItems.tsx @@ -2,6 +2,7 @@ import { useMemo } from 'react'; import { useQuery } from 'react-query'; import ApiGateway from '../../gateways/Api.gateway'; import { Money } from '../../protos/demo'; +import { useCurrency } from '../../providers/Currency.provider'; import { IProductCartItem } from '../../types/Cart'; import ProductPrice from '../ProductPrice'; import CartItem from './CartItem'; @@ -13,8 +14,9 @@ interface IProps { } const CartItems = ({ productList, shouldShowPrice = true }: IProps) => { + const { selectedCurrency } = useCurrency(); const { data: shippingConst = { units: 0, currencyCode: 'USD', nanos: 0 } } = useQuery('shipping', () => - ApiGateway.getShippingCost(productList) + ApiGateway.getShippingCost(productList, selectedCurrency) ); const total = useMemo( @@ -22,10 +24,10 @@ const CartItems = ({ productList, shouldShowPrice = true }: IProps) => { units: productList.reduce((acc, item) => acc + (item.product.priceUsd?.units || 0) * item.quantity, 0) + (shippingConst?.units || 0), - currencyCode: 'USD', + currencyCode: selectedCurrency, nanos: 0, }), - [shippingConst, productList] + [productList, shippingConst?.units, selectedCurrency] ); return ( diff --git a/src/frontend/gateways/Api.gateway.ts b/src/frontend/gateways/Api.gateway.ts index 8bf78ee865..5d495d4b8e 100644 --- a/src/frontend/gateways/Api.gateway.ts +++ b/src/frontend/gateways/Api.gateway.ts @@ -36,11 +36,12 @@ const ApiGateway = () => ({ }); }, - getShippingCost(itemList: IProductCartItem[]) { + getShippingCost(itemList: IProductCartItem[], currencyCode: string) { return request({ url: `${basePath}/shipping`, queryParams: { itemList: JSON.stringify(itemList.map(({ productId, quantity }) => ({ productId, quantity }))), + currencyCode, }, }); }, diff --git a/src/frontend/pages/api/shipping.ts b/src/frontend/pages/api/shipping.ts index 194a7a5213..280d666f88 100644 --- a/src/frontend/pages/api/shipping.ts +++ b/src/frontend/pages/api/shipping.ts @@ -2,16 +2,18 @@ import type { NextApiRequest, NextApiResponse } from 'next'; import InstrumentationMiddleware from '../../utils/telemetry/InstrumentationMiddleware'; import ShippingGateway from '../../gateways/rpc/Shipping.gateway'; import { CartItem, Empty, Money } from '../../protos/demo'; +import CurrencyGateway from '../../gateways/rpc/Currency.gateway'; type TResponse = Money | Empty; const handler = async ({ method, query }: NextApiRequest, res: NextApiResponse) => { switch (method) { case 'GET': { - const { itemList = '' } = query; + const { itemList = '', currencyCode = 'USD' } = query; const { costUsd } = await ShippingGateway.getShippingCost(JSON.parse(itemList as string) as CartItem[]); + const cost = await CurrencyGateway.convert(costUsd!, currencyCode as string); - return res.status(200).json(costUsd!); + return res.status(200).json(cost!); } default: { diff --git a/src/frontend/services/ProductCatalog.service.ts b/src/frontend/services/ProductCatalog.service.ts index 2b49d73e3f..bc08b795e3 100644 --- a/src/frontend/services/ProductCatalog.service.ts +++ b/src/frontend/services/ProductCatalog.service.ts @@ -29,7 +29,7 @@ const ProductCatalogService = () => ({ return { ...product, - price: await this.getProductPrice(product.priceUsd!, currencyCode), + priceUsd: await this.getProductPrice(product.priceUsd!, currencyCode), }; }, });