-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathApp.js
85 lines (76 loc) · 2.05 KB
/
App.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
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
import React, { useState, useEffect } from 'react'
import { getResults } from './api'
const LIMITS = [6, 12, 18, 24, 30]
const App = () => {
const [inputValue, setInputValue] = useState('')
const [searchLimit, setSearchLimit] = useState(12)
const [results, setResults] = useState([])
useEffect(() => {
const fetchResults = async () => {
try {
const apiResponse = await getResults({
searchQuery: inputValue,
limit: searchLimit,
})
setResults(apiResponse.results)
} catch (err) {
console.error(err)
}
}
fetchResults()
}, [inputValue, searchLimit])
return (
<main>
<h1>Giphy Search!</h1>
<form>
<input
type="search"
placeholder="Search Giphy"
value={inputValue}
onChange={(e) => {
setInputValue(e.target.value)
}}
/>
<label>
# of Results
<select
onChange={(e) => setSearchLimit(e.target.value)}
value={searchLimit}
>
{LIMITS.map((limit) => (
<option key={limit} value={limit}>
{limit}
</option>
))}
</select>
</label>
</form>
{results.length > 0 && (
<section className="callout primary">
{results.map((item) => (
<section
key={item.id}
className="card"
style={{
width: '300px',
display: 'inline-block',
marginRight: '16px',
}}
>
<video src={item.previewUrl} alt={item.title} loop autoPlay />
<section className="card-section">
<h5>
<a href={item.url} target="_blank" rel="noopener noreferrer">
{item.title}
</a>{' '}
({item.rating})
</h5>
</section>
</section>
))}
</section>
)}
</main>
)
}
export default App