Skip to content

Commit

Permalink
🎉 Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
alik0211 committed Jul 6, 2017
0 parents commit d203238
Show file tree
Hide file tree
Showing 17 changed files with 2,510 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# See https://help.github.com/ignore-files/ for more about ignoring files.

# dependencies
/node_modules
package-lock.json

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
2,138 changes: 2,138 additions & 0 deletions README.md

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "pokedex",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^15.6.1",
"react-dom": "^15.6.1",
"react-paginate": "^4.4.3",
"react-redux": "^5.0.5",
"redux": "^3.7.1"
},
"devDependencies": {
"react-scripts": "1.0.10",
"redux-logger": "^3.0.6",
"redux-thunk": "^2.2.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
Binary file added public/favicon.ico
Binary file not shown.
41 changes: 41 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<!--
manifest.json provides metadata used when your web app is added to the
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<noscript>
You need to enable JavaScript to run this app.
</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
15 changes: 15 additions & 0 deletions public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "192x192",
"type": "image/png"
}
],
"start_url": "./index.html",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
29 changes: 29 additions & 0 deletions src/actions/PageActions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {
REQUEST_POKEMONS,
RECEIVE_POKEMONS
} from '../constants/Pokemon'

function requestPokemons(offset) {
return {
type: REQUEST_POKEMONS,
offset
}
}

function receivePokemons(offset, json) {
return {
type: RECEIVE_POKEMONS,
pokemons: json.results.map(pokemon => pokemon)
}
}

export function fetchPokemons(offset) {

return dispatch => {
dispatch(requestPokemons(offset))
return fetch(`https://pokeapi.co/api/v2/pokemon/?offset=${offset}`)
.then(response => response.json())
.then(json => dispatch(receivePokemons(offset, json)))
}

}
15 changes: 15 additions & 0 deletions src/components/Pokemon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react'

const Pokemon = ({ pokemon }) => (
<tr>
<td>
<img
src={`https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/${pokemon.id}.png`}
alt="Pokemon"
/>
</td>
<td>{pokemon.name}</td>
</tr>
)

export default Pokemon
16 changes: 16 additions & 0 deletions src/components/Search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react'

const Search = ({
onChange,
term
}) => (
<input
className="user-input form-control"
type="text"
onChange={onChange}
value={term}
placeholder="Enter pokemon name..."
/>
)

export default Search
17 changes: 17 additions & 0 deletions src/components/SelectPokemon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react'

const SelectPokemon = ({ pokemon }) => (
<div className="col-sm-4 col-md-3 col-lg-2">
<div className="thumbnail">
<img
src={`https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/${pokemon.id}.png`}
alt="Pokemon"
/>
<div className="thumbnail-caption">
<h3>{pokemon.name}</h3>
</div>
</div>
</div>
)

export default SelectPokemon
2 changes: 2 additions & 0 deletions src/constants/Pokemon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const REQUEST_POKEMONS = 'REQUEST_POKEMONS'
export const RECEIVE_POKEMONS = 'RECEIVE_POKEMONS'
103 changes: 103 additions & 0 deletions src/containers/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import React, { Component } from 'react'
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'
import ReactPaginate from 'react-paginate'
import Search from '../components/Search'
import Pokemon from '../components/Pokemon'
import * as pageActions from '../actions/PageActions'

class App extends Component {
constructor(props) {
super(props)

this.state = {
term: '',
offset: 0
}

this.changeSearchTerm = this.changeSearchTerm.bind(this)
this.handlePageChange = this.handlePageChange.bind(this)
}

fetchPokemons() {
this.props.pageActions.fetchPokemons(this.state.offset)
}

componentDidMount() {
this.fetchPokemons()
}

changeSearchTerm(e) {
this.setState({ term: e.target.value })
}

handlePageChange(data) {
this.setState({ offset: data.selected * 20 }, () => {
this.fetchPokemons()
})
}

render() {
let { pokemons } = this.props.page
let searchString = this.state.term.trim().toLowerCase()

if (searchString.length > 0) {
pokemons = pokemons.filter(pokemon => {
return pokemon.name.toLowerCase().match(searchString)
})
}

pokemons = pokemons.map((pokemon, index) => {
pokemon.id = parseInt(pokemon.url.replace('http://pokeapi.co/api/v2/pokemon/', ''), 10)
return <Pokemon pokemon={pokemon} key={index} />
})

return (
<div className="container-fluid">
<Search onChange={this.changeSearchTerm} term={this.state.value} />
<div className="row">
<div className="col-sm-8 col-md-9 col-lg-10">
<table className="user-list table table-striped">
<thead>
<tr>
<th>Image</th>
<th>Name</th>
</tr>
</thead>
<tbody>
{
this.props.page.isFetched
?
<tr><td>Загрузка...</td></tr>
:
pokemons
}
</tbody>
</table>
<ReactPaginate
pageCount={41}
pageRangeDisplayed={3}
marginPagesDisplayed={3}
onPageChange={this.handlePageChange}
containerClassName="pagination"
/>
</div>
</div>
</div>
)
}
}

function mapStateToProps(state) {
return {
page: state.page
}
}

function mapDispatchToProps(dispatch) {
return {
pageActions: bindActionCreators(pageActions, dispatch)
}
}

export default connect(mapStateToProps, mapDispatchToProps)(App)
15 changes: 15 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react'
import { render } from 'react-dom'
import { Provider } from 'react-redux'
import App from './containers/App'
import './style/main.css'
import configureStore from './store/configureStore'

const store = configureStore()

render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
)
6 changes: 6 additions & 0 deletions src/reducers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { combineReducers } from 'redux'
import page from './page'

export default combineReducers({
page
})
31 changes: 31 additions & 0 deletions src/reducers/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {
REQUEST_POKEMONS,
RECEIVE_POKEMONS
} from '../constants/Pokemon'

const initialState = {
isFetched: false,
pokemons: []
}

export default function pokemon(state = initialState, action) {

switch (action.type) {
case REQUEST_POKEMONS:
return {
...state,
isFetched: true
}

case RECEIVE_POKEMONS:
return {
...state,
pokemons: action.pokemons,
isFetched: false
}

default:
return state
}

}
17 changes: 17 additions & 0 deletions src/store/configureStore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { createStore, applyMiddleware } from 'redux'
import thunkMiddleware from 'redux-thunk'
import { createLogger } from 'redux-logger'
import rootReducer from '../reducers'

const loggerMiddleware = createLogger()

export default function configureStore() {
return createStore(
rootReducer,
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__(),
applyMiddleware(
thunkMiddleware,
loggerMiddleware
)
)
}
20 changes: 20 additions & 0 deletions src/style/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.user-input {
margin-top: 15px;
margin-bottom: 15px;
}

.user-list {
border: 1px solid #ddd;
}

.break {
position: relative;
float: left;
padding: 6px 12px;
margin-left: -1px;
line-height: 1.42857143;
color: #337ab7;
text-decoration: none;
background-color: #fff;
border: 1px solid #ddd;
}

0 comments on commit d203238

Please sign in to comment.