-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathApp.js
57 lines (50 loc) · 1.56 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
import React from 'react'
import './App.css';
import CharacterCard from './components/CharacterCard';
import Loading from './components/Loading';
class App extends React.Component {
constructor(props){
super(props);
this.state={
characters:[],
filteredCharacters:[],
isLoading:true,
search:''
}
}
getCharacters = async () => {
const endpoint = 'https://rickandmortyapi.com/api/character';
const {results} = await fetch(endpoint).then(response => response.json())
this.setState({isLoading:false, characters:results, filteredCharacters:results})
}
componentDidMount(){
this.getCharacters()
}
handleOnChange = ({target:{value}}) => {
this.setState({search:value})
}
handleFilterCharacters = (e) => {
e.preventDefault()
const { characters, search} = this.state;
const filteredArray = characters.filter(({name})=> name.toUpperCase().includes(search.toUpperCase()))
this.setState({filteredCharacters:filteredArray})
}
render(){
const {filteredCharacters, isLoading} = this.state
return (
<div className="App">
<form onSubmit={this.handleFilterCharacters}>
<input type='text' placeholder='Rick Sanchez...' onChange={this.handleOnChange}/>
<button type='submit' >Buscar</button>
</form>
{
isLoading ? <Loading/> :
<section className='card-list'>
{filteredCharacters.map(item=> <CharacterCard character={item} key={Math.random()} />)}
</section>
}
</div>
);
}
}
export default App;