diff --git a/src/components/Pager.js b/src/components/Pager.js new file mode 100644 index 0000000..b5aacad --- /dev/null +++ b/src/components/Pager.js @@ -0,0 +1,44 @@ +import React from 'react'; +import PropTypes from 'prop-types'; +// import { useState } from "react"; +import styles from './Pager.module.css'; + +const Pager = (props) => { + const { total, current, onChange } = props; + // const [page, setPage] = useState(); + + const pagespan = new Array(Math.ceil(total / 5)).fill(1).map((_, index) => index + 1 + ""); + return ( +
+
+ +
+
+ { + pagespan.map((item, index) => ( +
onChange(item)}> + + {item} + +
+ )) + } +
+
+ +
+
+ ); +}; + +Pager.propTypes = { + total: PropTypes.number.isRequired, + current: PropTypes.number.isRequired, + onChange: PropTypes.func.isRequired, +}; + +export default React.memo(Pager); \ No newline at end of file diff --git a/src/components/Pager.module.css b/src/components/Pager.module.css new file mode 100644 index 0000000..6e9b337 --- /dev/null +++ b/src/components/Pager.module.css @@ -0,0 +1,22 @@ +:local(.pager-bar) { + display: flex; +} + +.page-list { + display: flex; + margin: 0 10px; +} + +.page-item { + width: 30px; + height: 30px; + display: flex; + align-items: center; + justify-content: center; + cursor: pointer; + +} + +.page-item:hover { + color: #1890FF; +} \ No newline at end of file diff --git a/src/components/PlayerList.js b/src/components/PlayerList.js index 7b40246..abd23fd 100755 --- a/src/components/PlayerList.js +++ b/src/components/PlayerList.js @@ -2,25 +2,70 @@ import React, { Component } from 'react'; import PropTypes from 'prop-types'; import styles from './PlayerList.css'; import PlayerListItem from './PlayerListItem'; +import Selector from './Selector'; +import Pager from './Pager'; +const pageLimit = 5; class PlayerList extends Component { + constructor(props) { + super(props); + this.state = { + pageindex: 0, + position: "" + }; + + this.handlePageChange = this.handlePageChange.bind(this); + this.handlePositionChange = this.handlePositionChange.bind(this); + } + + handlePageChange(page) { + + const players = this.props.players; + const total = players.length; + if (page > 0 && Math.ceil(total / pageLimit) >= page) { + this.setState(() => ({pageindex: page - 1})); + } + } + + handlePositionChange(event) { + const { value } = event.target; + this.setState(() => ({ + position: value, + pageindex: 0, + })); + } + render() { + const { pageindex, position } = this.state; + const players = position === "" ? this.props.players : this.props.players.filter((item) => item.position === position); + const total = players.length; + + const start = pageindex * pageLimit; + const end = start + pageLimit > total ? total : start + pageLimit; + const partPlayers = players.slice(start, end); + return ( - + <> + +
+ + +
+ ); } } diff --git a/src/components/Selector.js b/src/components/Selector.js new file mode 100644 index 0000000..a769363 --- /dev/null +++ b/src/components/Selector.js @@ -0,0 +1,23 @@ +import React from 'react'; +import PropTypes from 'prop-types'; +import styles from './Selector.module.css'; + +const Selector = (props) => { + const { handlePositionChange } = props; + + return ( +
+ +
+ ) +} + +Selector.propTypes = { + handlePositionChange: PropTypes.func.isRequired, +}; + +export default Selector; \ No newline at end of file diff --git a/src/components/Selector.module.css b/src/components/Selector.module.css new file mode 100644 index 0000000..c5d84fe --- /dev/null +++ b/src/components/Selector.module.css @@ -0,0 +1,3 @@ +.selector-panel { + margin: 20px 0; +} \ No newline at end of file diff --git a/src/components/index.js b/src/components/index.js index 12ce117..77958e2 100755 --- a/src/components/index.js +++ b/src/components/index.js @@ -1,3 +1,4 @@ export { default as AddPlayerInput } from './AddPlayerInput'; export { default as PlayerList } from './PlayerList'; export { default as PlayerListItem } from './PlayerListItem'; +export { default as Selector } from './Selector'; diff --git a/src/containers/PlayerListApp.js b/src/containers/PlayerListApp.js index 0e4bfa5..5698db3 100755 --- a/src/containers/PlayerListApp.js +++ b/src/containers/PlayerListApp.js @@ -1,4 +1,5 @@ import React, { Component } from 'react'; +import classnames from 'classnames'; import styles from './PlayerListApp.css'; import { connect } from 'react-redux'; @@ -6,23 +7,49 @@ import { addPlayer, deletePlayer, starPlayer } from '../actions/PlayersActions'; import { PlayerList, AddPlayerInput } from '../components'; class PlayerListApp extends Component { + constructor(props) { + super(props); + this.state = { + displayTip: false + }; + } + render() { + const { displayTip } = this.state; const { playerlist: { playersById }, } = this.props; + const enhancedDeletePlayer = (id) => { + this.setState(() => ({displayTip: true}), () => { + setTimeout(() => { + this.setState(() => ({displayTip: false})); + }, 2e3); + }); + this.props.deletePlayer(id); + } + const actions = { addPlayer: this.props.addPlayer, - deletePlayer: this.props.deletePlayer, + deletePlayer: enhancedDeletePlayer, starPlayer: this.props.starPlayer, }; return ( -
-

NBA Players

- - -
+ <> +
+

NBA Players

+ + +
+ { + displayTip ? ( +
+ Delete complete +
+ ) : null + } + ); } }