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(frontend): fixing total sum for the cart page #633

Merged
merged 5 commits into from
Dec 14, 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
2 changes: 1 addition & 1 deletion src/frontend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ RUN npm run build

FROM node:18-alpine AS runner
WORKDIR /app
RUN apk add --no-cache protoc
RUN apk add --no-cache protobuf-dev protoc
puckpuck marked this conversation as resolved.
Show resolved Hide resolved

ENV NODE_ENV=production

Expand Down
34 changes: 20 additions & 14 deletions src/frontend/components/CartItems/CartItems.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,32 @@ interface IProps {
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",
streetAddress: '1600 Amphitheatre Parkway',
city: 'Mountain View',
state: 'CA',
country: 'United States',
zipCode: '94043',
};
const { data: shippingConst = { units: 0, currencyCode: 'USD', nanos: 0 } } = useQuery('shipping', () =>
ApiGateway.getShippingCost(productList, selectedCurrency, address)
);

const total = useMemo<Money>(
() => ({
units:
productList.reduce((acc, item) => acc + (item.product.priceUsd?.units || 0) * item.quantity, 0) +
(shippingConst?.units || 0),
const total = useMemo<Money>(() => {
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: 0,
}),
[productList, shippingConst?.units, selectedCurrency]
);
nanos: nanoSum % 1000000000,
};
}, [shippingConst?.units, shippingConst?.nanos, productList, selectedCurrency]);

return (
<S.CartItems>
Expand Down
4 changes: 3 additions & 1 deletion src/frontend/components/ProductPrice/ProductPrice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ const ProductPrice = ({ price: { units, currencyCode, nanos } }: IProps) => {
[currencyCode, selectedCurrency]
);

const total = units + nanos / 1000000000;

return (
<span data-cy={CypressFields.ProductPrice}>
{currencySymbol} {units}.{nanos.toString().slice(0, 2)}
{currencySymbol} {total.toFixed(2)}
</span>
);
};
Expand Down