-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathApp.js
142 lines (132 loc) · 3.85 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import React, { Fragment, useState, useEffect } from 'react'
import { getResults } from './api'
const RATINGS = [
{ value: '', label: 'All' },
{ value: 'g', label: 'G' },
{ value: 'pg', label: 'PG' },
{ value: 'pg-13', label: 'PG-13' },
{ value: 'r', label: 'R' },
]
const LIMITS = [6, 12, 18, 24, 30]
const App = () => {
const [inputValue, setInputValue] = useState('')
const [searchQuery, setSearchQuery] = useState('')
const [showInstant, setShowInstant] = useState(false)
const [searchRating, setSearchRating] = useState('')
const [searchLimit, setSearchLimit] = useState(12)
const realSearchQuery = showInstant ? inputValue : searchQuery
const [results, setResults] = useState([])
useEffect(() => {
const fetchResults = async () => {
try {
const apiResponse = await getResults({
searchQuery: realSearchQuery,
limit: searchLimit,
rating: searchRating,
})
setResults(apiResponse.results)
} catch (err) {
console.error(err)
}
}
fetchResults()
}, [realSearchQuery, searchRating, searchLimit])
const handleSubmit = (e) => {
e.preventDefault()
setSearchQuery(inputValue)
}
return (
<main>
<h1>Giphy Search!</h1>
<form onSubmit={handleSubmit}>
<section className="input-group">
<input
type="search"
placeholder="Search Giphy"
value={inputValue}
onChange={(e) => {
setInputValue(e.target.value)
}}
className="input-group-field"
/>
<aside className="input-group-button">
<button type="submit" className="button">
Search
</button>
</aside>
</section>
<section>
<input
type="checkbox"
id="instant-results"
name="instant-results"
checked={showInstant}
onChange={(e) => {
setShowInstant(e.target.checked)
}}
/>
<label htmlFor="instant-results">Show instant results?</label>
</section>
<hr />
<fieldset>
<legend>Choose a rating</legend>
{RATINGS.map(({ value, label }) => (
<Fragment key={value}>
<input
type="radio"
name="rating"
value={value}
id={`rating-${value}`}
checked={value === searchRating}
onChange={() => {
setSearchRating(value)
}}
/>
<label htmlFor={`rating-${value}`}>{label}</label>
</Fragment>
))}
</fieldset>
<hr />
<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