Skip to content

Commit daa107b

Browse files
committed
Fix open-telemetry#586: Pass product's categories as an input for the Ad serice
1 parent 5a2c0ef commit daa107b

File tree

10 files changed

+38
-17
lines changed

10 files changed

+38
-17
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,5 @@ significant modifications will be credited to OpenTelemetry Authors.
142142
([#583](https://github.com/open-telemetry/opentelemetry-demo/pull/583))
143143
* Change ZipCode data type from int to string
144144
([#587](https://github.com/open-telemetry/opentelemetry-demo/pull/587))
145+
* Pass product's `categories` as an input for the Ad service
146+
([#600](https://github.com/open-telemetry/opentelemetry-demo/pull/600))

src/frontend/components/Ad/Ad.tsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ import { useAd } from '../../providers/Ad.provider';
33
import * as S from './Ad.styled';
44

55
const Ad = () => {
6-
const {
7-
adList: [{ text, redirectUrl } = { text: '', redirectUrl: '' }],
8-
} = useAd();
6+
const { adList } = useAd();
7+
const { text, redirectUrl } = adList[Math.floor(Math.random() * adList.length)] || { text: '', redirectUrl: '' };
98

109
return (
1110
<S.Ad data-cy={CypressFields.Ad}>

src/frontend/gateways/Api.gateway.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ const ApiGateway = () => ({
7878
},
7979
});
8080
},
81-
listAds(productIds: string[]) {
81+
listAds(contextKeys: string[]) {
8282
return request<Ad[]>({
8383
url: `${basePath}/data`,
8484
queryParams: {
85-
productIds,
85+
contextKeys,
8686
},
8787
});
8888
},

src/frontend/gateways/rpc/Ad.gateway.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ const { AD_SERVICE_ADDR = '' } = process.env;
66
const client = new AdServiceClient(AD_SERVICE_ADDR, ChannelCredentials.createInsecure());
77

88
const AdGateway = () => ({
9-
listAds(productIds: string[]) {
9+
listAds(contextKeys: string[]) {
1010
return new Promise<AdResponse>((resolve, reject) =>
11-
client.getAds({ contextKeys: productIds }, (error, response) => (error ? reject(error) : resolve(response)))
11+
client.getAds({ contextKeys: contextKeys }, (error, response) => (error ? reject(error) : resolve(response)))
1212
);
1313
},
1414
});

src/frontend/pages/api/data.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ type TResponse = Ad[] | Empty;
88
const handler = async ({ method, query }: NextApiRequest, res: NextApiResponse<TResponse>) => {
99
switch (method) {
1010
case 'GET': {
11-
const { productIds = [] } = query;
12-
const { ads: adList } = await AdGateway.listAds(productIds as string[]);
11+
const { contextKeys = [] } = query;
12+
const { ads: adList } = await AdGateway.listAds(Array.isArray(contextKeys) ? contextKeys : contextKeys.split(','));
1313

1414
return res.status(200).json(adList);
1515
}

src/frontend/pages/cart/checkout/[orderId]/index.tsx

+9-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ const Checkout: NextPage = () => {
1616
const { items = [], shippingAddress } = JSON.parse((query.order || '{}') as string) as IProductCheckout;
1717

1818
return (
19-
<AdProvider productIds={items.map(({ item }) => item?.productId || '')}>
19+
<AdProvider
20+
productIds={items.map(({ item }) => item?.productId || '')}
21+
contextKeys={[...new Set(items.flatMap(({ item }) => item.product.categories))]}
22+
>
2023
<Layout>
2124
<S.Checkout>
2225
<S.Container>
@@ -25,7 +28,11 @@ const Checkout: NextPage = () => {
2528

2629
<S.ItemList>
2730
{items.map(checkoutItem => (
28-
<CheckoutItem key={checkoutItem.item.productId} checkoutItem={checkoutItem} address={shippingAddress!} />
31+
<CheckoutItem
32+
key={checkoutItem.item.productId}
33+
checkoutItem={checkoutItem}
34+
address={shippingAddress!}
35+
/>
2936
))}
3037
</S.ItemList>
3138

src/frontend/pages/cart/index.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ const Cart: NextPage = () => {
1414
} = useCart();
1515

1616
return (
17-
<AdProvider productIds={items.map(({ productId }) => productId)}>
17+
<AdProvider
18+
productIds={items.map(({ productId }) => productId)}
19+
contextKeys={[...new Set(items.flatMap(({ product }) => product.categories))]}
20+
>
1821
<Layout>
1922
<S.Cart>
2023
{(!!items.length && <CartDetail />) || <EmptyCart />}

src/frontend/pages/product/[productId]/index.tsx

+11-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ const ProductDetail: NextPage = () => {
3030
const productId = query.productId as string;
3131

3232
const {
33-
data: { name, picture, description, priceUsd = { units: 0, currencyCode: 'USD', nanos: 0 } } = {} as Product,
33+
data: {
34+
name,
35+
picture,
36+
description,
37+
priceUsd = { units: 0, currencyCode: 'USD', nanos: 0 },
38+
categories,
39+
} = {} as Product,
3440
} = useQuery(
3541
['product', productId, 'selectedCurrency', selectedCurrency],
3642
() => ApiGateway.getProduct(productId, selectedCurrency),
@@ -48,7 +54,10 @@ const ProductDetail: NextPage = () => {
4854
}, [addItem, productId, quantity, push]);
4955

5056
return (
51-
<AdProvider productIds={[productId, ...items.map(({ productId }) => productId)]}>
57+
<AdProvider
58+
productIds={[productId, ...items.map(({ productId }) => productId)]}
59+
contextKeys={[...new Set(categories)]}
60+
>
5261
<Layout>
5362
<S.ProductDetail data-cy={CypressFields.ProductDetail}>
5463
<S.Container>

src/frontend/providers/Ad.provider.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ export const Context = createContext<IContext>({
1717
interface IProps {
1818
children: React.ReactNode;
1919
productIds: string[];
20+
contextKeys: string[];
2021
}
2122

2223
export const useAd = () => useContext(Context);
2324

24-
const AdProvider = ({ children, productIds }: IProps) => {
25+
const AdProvider = ({ children, productIds, contextKeys }: IProps) => {
2526
const { selectedCurrency } = useCurrency();
26-
const { data: adList = [] } = useQuery(['ads', productIds], () => ApiGateway.listAds(productIds), {
27+
const { data: adList = [] } = useQuery(['ads', contextKeys], () => ApiGateway.listAds(contextKeys), {
2728
refetchOnWindowFocus: false,
2829
});
2930
const { data: recommendedProductList = [] } = useQuery(

src/frontend/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"compilerOptions": {
3-
"target": "es5",
3+
"target": "ES2015",
44
"lib": ["dom", "dom.iterable", "esnext"],
55
"allowJs": true,
66
"skipLibCheck": true,

0 commit comments

Comments
 (0)