-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathindex.tsx
100 lines (93 loc) · 3.34 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
import { NextPage } from 'next';
import Image from 'next/image';
import { useRouter } from 'next/router';
import { useCallback, useState } from 'react';
import { useQuery } from '@tanstack/react-query';
import Ad from '../../../components/Ad';
import Footer from '../../../components/Footer';
import Layout from '../../../components/Layout';
import ProductPrice from '../../../components/ProductPrice';
import Recommendations from '../../../components/Recommendations';
import Select from '../../../components/Select';
import { CypressFields } from '../../../utils/Cypress';
import ApiGateway from '../../../gateways/Api.gateway';
import { Product } from '../../../protos/demo';
import AdProvider from '../../../providers/Ad.provider';
import { useCart } from '../../../providers/Cart.provider';
import * as S from '../../../styles/ProductDetail.styled';
import { useCurrency } from '../../../providers/Currency.provider';
const quantityOptions = new Array(10).fill(0).map((_, i) => i + 1);
const ProductDetail: NextPage = () => {
const { push, query } = useRouter();
const [quantity, setQuantity] = useState(1);
const {
addItem,
cart: { items },
} = useCart();
const { selectedCurrency } = useCurrency();
const productId = query.productId as string;
const {
data: {
name,
picture,
description,
priceUsd = { units: 0, currencyCode: 'USD', nanos: 0 },
categories,
} = {} as Product,
} = useQuery(
['product', productId, 'selectedCurrency', selectedCurrency],
() => ApiGateway.getProduct(productId, selectedCurrency),
{
enabled: !!productId,
}
);
const onAddItem = useCallback(async () => {
await addItem({
productId,
quantity,
});
push('/cart');
}, [addItem, productId, quantity, push]);
return (
<AdProvider
productIds={[productId, ...items.map(({ productId }) => productId)]}
contextKeys={[...new Set(categories)]}
>
<Layout>
<S.ProductDetail data-cy={CypressFields.ProductDetail}>
<S.Container>
<S.Image $src={"/images/products/" + picture} data-cy={CypressFields.ProductPicture} />
<S.Details>
<S.Name data-cy={CypressFields.ProductName}>{name}</S.Name>
<S.Description data-cy={CypressFields.ProductDescription}>{description}</S.Description>
<S.ProductPrice>
<ProductPrice price={priceUsd} />
</S.ProductPrice>
<S.Text>Quantity</S.Text>
<Select
data-cy={CypressFields.ProductQuantity}
onChange={event => setQuantity(+event.target.value)}
value={quantity}
>
{quantityOptions.map(option => (
<option key={option} value={option}>
{option}
</option>
))}
</Select>
<S.AddToCart data-cy={CypressFields.ProductAddToCart} onClick={onAddItem}>
<Image src="/icons/Cart.svg" height="15px" width="15px" alt="cart" /> Add To Cart
</S.AddToCart>
</S.Details>
</S.Container>
<Recommendations />
</S.ProductDetail>
<Ad />
<Footer />
</Layout>
</AdProvider>
);
};
export default ProductDetail;