|
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' |
3 | 3 | import { ProductList } from '@components/product' |
4 | 4 | import { Layout } from '@components/common' |
5 | 5 |
|
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' |
9 | 11 |
|
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 |
14 | 13 |
|
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()) |
47 | 20 |
|
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 | + ) |
53 | 24 |
|
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 | +} |
59 | 38 |
|
| 39 | +const TaxonomyPage = ({ pages, taxon, taxons, products }) => { |
60 | 40 | return ( |
61 | 41 | <ProductList |
62 | 42 | products={products} |
|
0 commit comments