Skip to content

Commit

Permalink
fix currency bug (#522)
Browse files Browse the repository at this point in the history
  • Loading branch information
cartersocha authored Oct 23, 2022
1 parent ce3a8d7 commit cf4bbbf
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
8 changes: 5 additions & 3 deletions src/frontend/components/CartItems/CartItems.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -13,19 +14,20 @@ 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<Money>(
() => ({
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 (
Expand Down
3 changes: 2 additions & 1 deletion src/frontend/gateways/Api.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ const ApiGateway = () => ({
});
},

getShippingCost(itemList: IProductCartItem[]) {
getShippingCost(itemList: IProductCartItem[], currencyCode: string) {
return request<Money>({
url: `${basePath}/shipping`,
queryParams: {
itemList: JSON.stringify(itemList.map(({ productId, quantity }) => ({ productId, quantity }))),
currencyCode,
},
});
},
Expand Down
6 changes: 4 additions & 2 deletions src/frontend/pages/api/shipping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TResponse>) => {
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: {
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/services/ProductCatalog.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const ProductCatalogService = () => ({

return {
...product,
price: await this.getProductPrice(product.priceUsd!, currencyCode),
priceUsd: await this.getProductPrice(product.priceUsd!, currencyCode),
};
},
});
Expand Down

0 comments on commit cf4bbbf

Please sign in to comment.