Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

weijia pull request #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16,984 changes: 16,984 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"test": "jest",
"eject": "react-scripts eject"
},
"dependencies": {
Expand All @@ -18,7 +18,14 @@
"redux": "^4.0.5"
},
"devDependencies": {
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.2",
"react-scripts": "3.4.0"
},
"browserslist": [">0.2%", "not dead", "not ie <= 11", "not op_mini all"]
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}
3 changes: 2 additions & 1 deletion src/actions/PlayersActions.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import * as types from '../constants/ActionTypes';

export function addPlayer(name) {
export function addPlayer(name,position) {
return {
type: types.ADD_PLAYER,
name,
position
};
}

Expand Down
38 changes: 25 additions & 13 deletions src/components/AddPlayerInput.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,51 @@
import React, { Component } from 'react';
import React, {Component} from 'react';
import classnames from 'classnames';
import PropTypes from 'prop-types';
import styles from './AddPlayerInput.css';
import PlayerPositionSelect from "./PlayerPositionSelect";

class AddPlayerInput extends Component {
render() {
return (
<input
type="text"
autoFocus={true}
className={classnames('form-control', styles.addPlayerInput)}
placeholder="Type the name of a player"
value={this.state.name}
onChange={this.handleChange.bind(this)}
onKeyDown={this.handleSubmit.bind(this)}
/>
<>
<input
type="text"
autoFocus={true}
className={classnames('form-control', styles.addPlayerInput)}
placeholder="Type the name of a player"
value={this.state.name}
onChange={this.handleChange.bind(this)}
onKeyDown={this.handleSubmit.bind(this)}
/>
<PlayerPositionSelect
value={this.state.position}
onChange={this.handleSelectChange}/>
</>
);
}

constructor(props, context) {
super(props, context);
this.state = {
name: this.props.name || '',
position: "SF"
};
}

handleSelectChange = (value) => {
this.setState({position: value});
};

handleChange(e) {
this.setState({ name: e.target.value });
this.setState({name: e.target.value});
}

handleSubmit(e) {
const name = e.target.value.trim();
if (e.which === 13) {
this.props.addPlayer(name);
this.setState({ name: '' });
const {position} = this.state;
this.props.addPlayer(name,position);
this.setState({name: ''});
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/PlayerList.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
:local(.playerList) {
padding-left: 0;
margin-bottom: 0;
}
}
63 changes: 47 additions & 16 deletions src/components/PlayerList.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,57 @@
import React, { Component } from 'react';
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import styles from './PlayerList.css';
import PlayerListItem from './PlayerListItem';

const PaginationBtn = ({text, onClick}) => {
return <button onClick={onClick}>{text}</button>
};

class PlayerList extends Component {
state = {
currentPage: 0,
showPlayers: []
};

static getDerivedStateFromProps(props, state) {
const showPlayers = props.players.slice(state.currentPage * 5, state.currentPage * 5 + 5);
return {
showPlayers
}
}

handleNextPage = () => {
const {currentPage} = this.state;
this.setState({currentPage: currentPage + 1})
};

handlePrevPage = () => {
const {currentPage} = this.state;
this.setState({currentPage: currentPage - 1})
};

render() {
const {currentPage, showPlayers} = this.state;
return (
<ul className={styles.playerList}>
{this.props.players.map((player, index) => {
return (
<PlayerListItem
key={index}
id={index}
name={player.name}
team={player.team}
position={player.position}
starred={player.starred}
{...this.props.actions}
/>
);
})}
</ul>
<>
<ul className={styles.playerList}>
{showPlayers.map((player, index) => {
return (
<PlayerListItem
key={index}
id={index}
name={player.name}
team={player.team}
position={player.position}
starred={player.starred}
{...this.props.actions}
/>
);
})}
</ul>
{currentPage > 0 ? <PaginationBtn text={`<`} onClick={this.handlePrevPage}/>:''}
{showPlayers.length === 5 && <PaginationBtn text={`>`} onClick={this.handleNextPage}/>}
</>
);
}
}
Expand Down
28 changes: 28 additions & 0 deletions src/components/PlayerPositionSelect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React, {Component} from "react";
import PropTypes from "prop-types";

const POSITION = ["C", "PG", "SG", "SF", "PF"];

class PlayerPositionSelect extends Component {
selectChange=(e)=>{
this.props.onChange(e.target.value);
};

render() {
return <select defaultValue={this.props.value} onChange={this.selectChange}>
{POSITION.map(item => {
return <option key={item} value={item}>{item}</option>
})}
</select>;
}
}

PlayerPositionSelect.propTypes = {
onChange:PropTypes.func,
value:PropTypes.string
};
PlayerPositionSelect.defaultProps = {
onChange:()=>{},
value:POSITION[0]
};
export default PlayerPositionSelect;
13 changes: 13 additions & 0 deletions src/components/PlayerPositionSelect.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from "react";
import Adapter from 'enzyme-adapter-react-16'
import { shallow, configure } from 'enzyme'
configure({ adapter: new Adapter() });

import PlayerPositionSelect from "./PlayerPositionSelect";

describe("<PlayerPositionSelect />", () => {
it("PlayerPositionSelect", () => {
const renderer = shallow(<PlayerPositionSelect />);
expect(renderer.text()).toEqual("CPGSGSFPF");
});
});
2 changes: 1 addition & 1 deletion src/reducers/playerlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export default function players(state = initialState, action) {
{
name: action.name,
team: 'LOS ANGELES LAKERS',
position: 'SF',
position: action.position,
},
],
};
Expand Down