Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix currency bug #522

Merged
merged 1 commit into from
Oct 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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