-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
137 lines (111 loc) · 3.4 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
import React, {Component} from 'react';
import loader from './images/loader.svg';
import Gif from './Gif';
import clearButton from './images/close-icon.svg';
const randomChoice = arr => {
const randIndex = Math.floor(Math.random() * arr.length);
return arr[randIndex];
}
const Header = ({clearSearch, hasResults}) => (
<div className="header grid">
{hasResults ? <button onClick={clearSearch}><img src={clearButton} onClick={clearSearch} /> </button> : <h1 className="title">Jiffy</h1> }
</div>
)
const UserHint = ({loading, hintText}) => (
<div className="user-hint">
{loading ? <img className="blokc mx-auto" src={loader}/> : hintText}
</div>
)
class App extends Component {
constructor(props){
super(props)
this.state = {
loading: false,
searchTerm: '',
hintText: '',
gifs: []
}
}
//function that searcher feor api giphy fetch search term puts search tern into queryURL
searchGiphy = async (searchTerm) => {
this.setState({
loading: true
})
try{
const response = await fetch(`https://api.giphy.com/v1/gifs/search?api_key=W4K2wJAYolMWrcIDHHYzDV9DoEsApezH&q=${searchTerm}&limit=40&offset=0&rating=PG&lang=en`);
const {data} = await response.json();
if(!data.length) {
throw `Nothing found for ${searchTerm}`
}
// get random gify from fetched array
const randomGif = randomChoice(data)
console.log({randomGif})
this.setState((prevState, props) =>({
...prevState,
gif: randomGif,
gifs: [...prevState.gifs, randomGif],
loading: false,
hintText: `Hit enter to see more ${searchTerm}`
}))
} catch(error) {
// it there is no result for a adkfjhaldkjfh search WHAT TO DO
this.setState((prevState, props) => ({
...prevState,
hintText: error,
loading: false
}))
console.log(error);
}
}
handleChange = event => {
// same as const value = event.target.value
const {value} = event.target
this.setState((prevState, props) => ({
...prevState,
searchTerm: value,
hintText: value.length > 2 ? `Hit Enter to search ${value}` : ""
}))
}
handleKeyPress = event => {
const {value} = event.target
if(value.length>2 && event.key === 'Enter'){
this.searchGiphy(value)
}
}
// when we have 2 or more chars and pressed enter run a search
//here we reset out state
clearSearch = () => {
this.setState((prevState, props) => ({
...prevState,
searchTerm: '',
hintText: '',
gifs: []
}))
//focus cursor back into search
this.textInput.focus();
}
render(){
const {searchTerm, gifs} = this.state
const hasResults = gifs.length
return(
<div className="page">
<Header clearSearch={this.clearSearch} hasResults={hasResults}/>
<div className="search grid">
{this.state.gifs.map(gif =>
<Gif {...gif} />
)}
<input className="input grid-item" placeholder="Type something"
onChange={this.handleChange}
onKeyPress={this.handleKeyPress}
value={searchTerm}
ref={input => {
this.textInput = input;
}}
/>
</div>
<UserHint {...this.state}/>
</div>
);
}
}
export default App;