Skip to content

Instantly share code, notes, and snippets.

@mannawar
Created April 3, 2020 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mannawar/a97f2e3cde2b68f4fc342fc7f621944c to your computer and use it in GitHub Desktop.
Save mannawar/a97f2e3cde2b68f4fc342fc7f621944c to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
import { connect } from 'react-redux';
import './App.css';
import Link from '../Components/Link';
import SearchBox from '../Components/SearchBox';
import Cardlist from '../Components/Cardlist';
import Scroll from '../Components/Scroll';
import ErrorBoundary from '../Components/ErrorBoundary';
import { setSearchField, requestRobots } from '../actions';
const mapStateToProps = state => {
return {
searchField: state.searchRobots.searchField,
robots: state.requestRobots.robots,
isPending: state.requestRobots.isPending,
error: state.requestRobots.error
}
}
const mapDispatchToProps = (dispatch) => {
return {
onSearchChange: (event) => dispatch(setSearchField(event.target.value)),
onRequestRobots: () => dispatch(requestRobots())
}
}
class App extends Component {
render() {
const { searchField, onSearchChange, robots, isPending } = this.props;
const filteredRobots = robots.filter(robot => {
return robot.name.toLowerCase().includes(searchField.toLowerCase())
})
return isPending ?
<h1>Loading</h1>:
(
<div className='tc'>
<h1 className='f1'>Robofriends</h1>
<SearchBox searchChange={onSearchChange}/>
<Scroll>
<ErrorBoundary>
<Cardlist robots={filteredRobots}/>
</ErrorBoundary>
</Scroll>
<Link />
</div>
);
}
componentDidMount() {
this.props.onRequestRobots();
}
}
export default connect(mapStateToProps, mapDispatchToProps)(App);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment