-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
51 lines (41 loc) · 1.54 KB
/
index.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
import React, { useContext, useState } from 'react';
import Store from '../../context';
import API_CONFIG from '../../ApiConfig';
import Axios from 'axios';
import Item from './item';
export default function Search() {
const { state, dispatch } = useContext(Store);
const [searchResults, setSearchResults] = useState(state.search_results || []);
const [searchQuery, setSearchQuery] = useState(state.search_query || '');
function handleSearchQueryChange(e) {
setSearchQuery(e.target.value);
}
async function handleSearch() {
dispatch({ type: 'updateSearchQuery', payload: searchQuery });
const response = await Axios({
method: 'get',
url: API_CONFIG.yt_search_endpoint({ searchQuery }),
responseType: 'json'
});
const items = (response.data.items || []).filter(_ => _.id.kind === 'youtube#video');
setSearchResults(items);
dispatch({ type: 'updateSearchResults', payload: items });
}
function handleSubmit(e) {
if (e.keyCode === 13 && e.target.value.length > 3) handleSearch();
}
return <div className="playlist-content tab-pane fade" id="search" role="tabpanel" aria-labelledby="search-tab">
<div className="list-group search-control">
<div className="list-group-item search-field">
<input
type="text"
className="form-control"
placeholder="Search"
value={searchQuery}
onKeyUp={handleSubmit}
onChange={handleSearchQueryChange} />
</div>
{searchResults.map(result => <Item {...result} />)}
</div>
</div>;
}