Skip to content

Commit

Permalink
Handle errors
Browse files Browse the repository at this point in the history
  • Loading branch information
alik0211 committed May 9, 2019
1 parent 71695b2 commit 3275b44
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/reducers/pokemons.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default function(state = initialState, action) {
...state.collection,
...action.payload.results.reduce((accumulator, item) => {
const { url } = item
const id = +url.substring(34, url.length - 1)
const id = url.substring(34, url.length - 1)

return {
...accumulator,
Expand Down
23 changes: 18 additions & 5 deletions src/ui/page/cmp-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@ import Search from '../search/cmp-search'

class Page extends Component {
state = {
pokemonsIds: []
pokemonsIds: [],
error: null
}

componentDidMount() {
this.props.getPokemons().then(() => {
this.props.getPokemons().then(action => {
if (action.error) {
return this.setState({
error: action.payload.message
})
}

this.setState({
pokemonsIds: Object.keys(this.props.collection)
})
Expand All @@ -19,10 +26,16 @@ class Page extends Component {
const value = event.currentTarget.value.toLowerCase().trim()
const { collection } = this.props

if (value === '') {
return this.setState({
pokemonsIds: Object.keys(collection)
})
}

const pokemonsIds = Object.keys(collection).filter(pokemonId => {
const pokemon = collection[pokemonId]

return pokemon.name.includes(value) || value === ''
return pokemon.name.includes(value)
})

this.setState({
Expand All @@ -31,7 +44,7 @@ class Page extends Component {
}

render() {
const { pokemonsIds } = this.state
const { pokemonsIds, error } = this.state
const { collection, isFetched } = this.props

const pokemons = pokemonsIds.map(pokemonId => {
Expand All @@ -46,7 +59,7 @@ class Page extends Component {

return (
<div className="page">
{/* {error && <div className="page__error">{error}</div>} */}
{error && <div className="page__error">{error}</div>}
<div className="page__search">
<Search onChange={this.handleSearch} />
</div>
Expand Down

0 comments on commit 3275b44

Please sign in to comment.