Skip to content

Commit 6d8cef2

Browse files
authored
Merge pull request #74 from DataDog/dash-workshop-fixes
adds fixes for workshops
2 parents 518fa7d + eca291a commit 6d8cef2

File tree

4 files changed

+186
-113
lines changed

4 files changed

+186
-113
lines changed

services/ads/java/src/main/java/adsjava/AdsJavaApplication.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ public String home() {
4747
public HashMap[] ads(@RequestHeader HashMap<String, String> headers) {
4848

4949
boolean errorFlag = false;
50-
if(headers.get("X-Throw-Error") != null) {
51-
errorFlag = Boolean.parseBoolean(headers.get("X-Throw-Error"));
50+
if(headers.get("x-throw-error") != null) {
51+
errorFlag = Boolean.parseBoolean(headers.get("x-throw-error"));
5252
}
5353

5454
// if x-error-rate is present, set to variable errorRate (if missing, set to 1)
5555
double errorRate = 1;
56-
if(headers.get("X-Error-Rate") != null) {
57-
errorRate = Double.parseDouble(headers.get("X-Error-Rate"));
56+
if(headers.get("x-error-rate") != null) {
57+
errorRate = Double.parseDouble(headers.get("x-error-rate"));
5858
}
5959

6060
if(errorFlag && Math.random() < errorRate) {

services/frontend/components/common/Ad/Ad.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ function Ad() {
2626
'X-Error-Rate': process.env.NEXT_PUBLIC_ADS_ERROR_RATE || '0.25',
2727
}
2828
fetch(`${adsPath}/ads`, { headers })
29+
.then((res) => {
30+
if (!res.ok) {
31+
throw new Error('Error fetching ad')
32+
}
33+
return res
34+
})
2935
.then((res) => res.json())
3036
.then((data) => {
3137
const index = getRandomArbitrary(0, data.length)

services/frontend/pages/taxonomies/[taxonomy]/[...slug].tsx

Lines changed: 31 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,42 @@
1-
import { useEffect, useState, useCallback } from 'react'
2-
import { useRouter } from 'next/router'
1+
import { Page } from '@customTypes/page'
2+
import { Taxon } from '@customTypes/taxons'
33
import { ProductList } from '@components/product'
44
import { Layout } from '@components/common'
55

6-
const TaxonomyPage = () => {
7-
const router = useRouter()
8-
const { taxonomy, slug } = router.query
6+
export const getServerSideProps = async ({ params }) => {
7+
const baseUrl =
8+
process.env.NODE_ENV === 'development'
9+
? 'http://localhost:3000/api'
10+
: '/api'
911

10-
const [products, setProducts] = useState([])
11-
const [pages, setPages] = useState([])
12-
const [taxons, setTaxons] = useState([])
13-
const [taxon, setTaxon] = useState<{ id?: string }>({})
12+
const { taxonomy, slug } = params
1413

15-
const fetchPages = useCallback(async () => {
16-
const pages = await fetch(`/api/pages`).then((res) => res.json())
17-
setPages(pages)
18-
}, [])
19-
20-
const fetchTaxons = useCallback(async () => {
21-
const taxons = await fetch(`/api/taxonomies`).then((res) => res.json())
22-
setTaxons(taxons)
23-
}, [])
24-
25-
const fetchTaxon = useCallback(async () => {
26-
if (!slug) return
27-
28-
const taxon = await fetch(`/api/taxonomies/${taxonomy}/${slug}`).then(
29-
(res) => res.json()
30-
)
31-
setTaxon(taxon)
32-
}, [slug, taxonomy])
33-
34-
const fetchProducts = useCallback(async (taxonId) => {
35-
if (!taxonId) return
36-
const products = await fetch(`/api/products?taxons=${taxonId}`).then(
37-
(res) => res.json()
38-
)
39-
40-
if (products?.length === 0 || products?.error) {
41-
console.log('No products found')
42-
return
43-
}
44-
45-
setProducts(products)
46-
}, [])
14+
const pages: Page[] = await fetch(`${baseUrl}/pages`).then((res) =>
15+
res.json()
16+
)
17+
const taxon: Taxon = await fetch(
18+
`${baseUrl}/taxonomies/${taxonomy}/${slug}`
19+
).then((res) => res.json())
4720

48-
useEffect(() => {
49-
fetchPages()
50-
fetchTaxons()
51-
fetchTaxon()
52-
}, [fetchPages, fetchTaxons, fetchTaxon])
21+
const taxons: any = await fetch(`${baseUrl}/taxonomies`).then((res) =>
22+
res.json()
23+
)
5324

54-
useEffect(() => {
55-
if (taxon.id) {
56-
fetchProducts(taxon.id)
57-
}
58-
}, [taxon, fetchProducts])
25+
const products: any = await fetch(
26+
`${baseUrl}/products?taxons=${taxon.id}`
27+
).then((res) => res.json())
28+
29+
return {
30+
props: {
31+
pages,
32+
taxon,
33+
taxons,
34+
products,
35+
},
36+
}
37+
}
5938

39+
const TaxonomyPage = ({ pages, taxon, taxons, products }) => {
6040
return (
6141
<ProductList
6242
products={products}

0 commit comments

Comments
 (0)