-
Notifications
You must be signed in to change notification settings - Fork 9
/
[value].js
40 lines (33 loc) · 1.02 KB
/
[value].js
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
import { useRouter } from 'next/router'
import useSWR from 'swr'
import styled from 'styled-components'
import dynamic from 'next/dynamic'
import Head from 'next/head'
const SearchResult = dynamic(() => import('../../components/SearchResult'))
const fetcher = url => fetch(url).then(res => res.json())
export default function SearchResults() {
const router = useRouter()
const { value } = router.query
const { data, error, loading } = useSWR(`/api/search?value=${value}`, fetcher)
return (
<>
<Head>
<title>Search results for: "{value}" - Alimazon</title>
</Head>
<DivSearchResults>
{
loading || !data
? <div className="loader"></div>
: error
? "Error."
: data.data.products.data.map(result => <SearchResult key={result.id} result={result} />)
}
</DivSearchResults>
</>
)
}
const DivSearchResults = styled.div`
grid-area: 2 / 2 / 3 / 3;
display: flex;
flex-direction: column;
`