import React from 'react'; import { connect } from 'react-redux'; import { appReducers } from './index'; import * as ActionsPokemonExplorer from './components/PokemonExplorer/actions'; import * as ActionsPokemonSelectList from './components/PokemonSelectList/actions'; import { ThunkDispatchPokemonSelectList } from './types'; import { PokemonExplorer } from './components/PokemonExplorer/PokemonExplorer'; import { PokemonSelectList } from './components/PokemonSelectList/PokemonSelectList'; type PokemonAppProps = ReturnType; interface IConnectedPokemonAppProps extends PokemonAppProps { dispatch : ThunkDispatchPokemonSelectList; } class PokemonApp extends React.Component { constructor(props : IConnectedPokemonAppProps) { super(props); } public componentWillMount() { this.props.dispatch(ActionsPokemonSelectList.fetchPokemonList()) .then(() => this.props.dispatch(ActionsPokemonSelectList.setIsLoading(false))); } public render() { const { activePokemonIndex, pokemonList, } = this.props.pokemonSelectListState; const { leaguePokemon, } = this.props.pokemonExplorerState; return (
{ leaguePokemon !== null && }
); } private readonly onActivatePokemon = (pokemonIndex : number) => { const { dispatch, pokemonSelectListState } = this.props; const pokemonId = pokemonSelectListState.pokemonList[pokemonIndex].id; dispatch(ActionsPokemonSelectList.fetchPokemonLeagueStats(pokemonId)) .then((leaguePokemon) => { dispatch(ActionsPokemonSelectList.setActivePokemonIndex(pokemonIndex)); dispatch(ActionsPokemonExplorer.setLeaguePokemon(leaguePokemon)); }) .catch((error) => { // tslint:disable-next-line:no-console console.error(error); dispatch(ActionsPokemonExplorer.setLeaguePokemon(null)); }) .then(() => dispatch(ActionsPokemonExplorer.setIsLoading(false))); } } const mapStateToProps = (state : ReturnType) : PokemonAppProps => { return { pokemonExplorerState: state.pokemonExplorerState, pokemonSelectListState: state.pokemonSelectListState, }; }; export const ConnectedPokemonApp = connect(mapStateToProps)(PokemonApp);