Skip to content

Commit

Permalink
fix filter pokemon by type
Browse files Browse the repository at this point in the history
  • Loading branch information
dwicao committed Apr 16, 2017
1 parent 08f7863 commit 68a262e
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/actions/actionTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export const ADD_POKEMON = 'ADD_POKEMON';
export const ADD_POKEMON_BY_TYPE = 'ADD_POKEMON_BY_TYPE';
export const ADD_POKEMON_DETAIL = 'ADD_POKEMON_DETAIL';
export const FILTER_POKEMON_BY_TYPE = 'FILTER_POKEMON_BY_TYPE';
export const CLEAR_POKEMON_LIST = 'CLEAR_POKEMON_LIST';
export const CLEAR_POKEMON_LIST_BY_TYPE = 'CLEAR_POKEMON_LIST_BY_TYPE';
6 changes: 3 additions & 3 deletions src/actions/mainActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ export const filterPokemonByType = payload => ({
payload,
});

export const clearPokemonList = payload => ({
type: types.CLEAR_POKEMON_LIST,
payload,
export const clearPokemonListByType = payload => ({
type: types.CLEAR_POKEMON_LIST_BY_TYPE,
payload: [],
});

export const fetchPokemon = offset => dispatch => {
Expand Down
54 changes: 54 additions & 0 deletions src/components/CardList/CardListByType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import * as mainActions from '../../actions/mainActions';
import Card from './Card';
import { getPokemonId } from '../../utils';

class CardListByType extends Component {
render() {
if (this.props.pokelisttype.length === 0) return this.renderLoader();

return (
<div className="card-list">
{this.renderCard()}
</div>
);
}

renderLoader(key) {
return <div key={key} className="loader">Loading...</div>;
}

renderCard() {
const { pokelisttype } = this.props;

return pokelisttype.map(({ pokemon }, index) => {
if (!pokemon) return this.renderLoader(index);

const pokemonId = parseFloat(getPokemonId(pokemon.url));
if (pokemonId > 10000) return;

return (
<Card key={index}
name={pokemon.name}
url={pokemon.url}
pokemonId={getPokemonId(pokemon.url)}
/>
);
});
}
}

const mapStateToProps = (state) => ({
pokedetail: state.pokedetail,
pokelist: state.pokelist,
pokefilter: state.pokefilter,
pokelisttype: state.pokelisttype,
});

const mapDispatchToProps = (dispatch) => ({
actions: bindActionCreators(mainActions, dispatch)
});

export default connect(mapStateToProps, mapDispatchToProps)(CardListByType);
9 changes: 6 additions & 3 deletions src/components/CardList/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { bindActionCreators } from 'redux';
import * as mainActions from '../../actions/mainActions';
import InfiniteScroll from 'react-infinite-scroller'
import Card from './Card';
import CardListByType from './CardListByType';
import { getPokemonId } from '../../utils';
import './index.css';

Expand All @@ -17,13 +18,15 @@ class CardList extends Component {
}

loadMore() {
if (this.section < 750 && this.props.pokefilter.isFilterByPokemonType === false) {
if (this.section < 750) {
this.props.actions.fetchPokemon(this.section);
this.section = this.section + 20;
}
}

render() {
if (this.props.pokefilter.isFilterByPokemonType) return <CardListByType/>;

return (
<InfiniteScroll
className="card-list"
Expand All @@ -47,7 +50,7 @@ class CardList extends Component {
renderCard() {
const { pokelist, pokefilter } = this.props;

if (pokefilter.isFilterByPokemonType) {
/*if (pokefilter.isFilterByPokemonType) {
return pokelist.map(({ pokemon }, index) => {
if (!pokemon) return this.renderLoader(index);
Expand All @@ -62,7 +65,7 @@ class CardList extends Component {
/>
);
});
}
}*/

return pokelist.map((section, index) => {
if (!section.map) return this.renderLoader(index);
Expand Down
6 changes: 1 addition & 5 deletions src/components/TopBar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,15 @@ class TopBar extends Component {
}

filterPokemon(type) {
this.props.actions.clearPokemonListByType();
this.props.actions.fetchPokemonByType(type);

this.props.actions.filterPokemonByType({
isFilterByPokemonType: true,
type,
});
}

clearFilter() {
this.props.actions.clearPokemonList([]);

this.props.actions.fetchPokemon();

this.props.actions.filterPokemonByType({
isFilterByPokemonType: false,
});
Expand Down
2 changes: 2 additions & 0 deletions src/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { combineReducers } from 'redux';
import pokelist from './pokelistReducer';
import pokedetail from './pokedetailReducer';
import pokefilter from './pokefilterReducer';
import pokelisttype from './pokelisttypeReducer';

const rootReducer = combineReducers({
pokelist,
pokedetail,
pokefilter,
pokelisttype,
});

export default rootReducer;
6 changes: 0 additions & 6 deletions src/reducers/pokelistReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ export default function pokelistReducer(state = [], action) {
action.payload
];

case types.ADD_POKEMON_BY_TYPE:
return action.payload;

case types.CLEAR_POKEMON_LIST:
return action.payload;

default:
return state;
}
Expand Down
17 changes: 17 additions & 0 deletions src/reducers/pokelisttypeReducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as types from '../actions/actionTypes';

export default function pokelisttypeReducer(state = [], action) {
switch(action.type) {

case types.ADD_POKEMON_BY_TYPE:
return action.payload;


case types.CLEAR_POKEMON_LIST_BY_TYPE:
return action.payload;

default:
return state;
}

}

0 comments on commit 68a262e

Please sign in to comment.