|
| 1 | +import * as React from "react"; |
| 2 | +import PropTypes from "prop-types"; |
| 3 | + |
| 4 | +function Instructions() { |
| 5 | + return ( |
| 6 | + <section className="instructions-container"> |
| 7 | + <h2>Instructions</h2> |
| 8 | + <ol> |
| 9 | + <li>Enter 2 Github users</li> |
| 10 | + <li>Battle</li> |
| 11 | + <li>See the winners</li> |
| 12 | + </ol> |
| 13 | + </section> |
| 14 | + ); |
| 15 | +} |
| 16 | + |
| 17 | +class PlayerInput extends React.Component { |
| 18 | + constructor(props) { |
| 19 | + super(props); |
| 20 | + |
| 21 | + this.state = { |
| 22 | + username: "", |
| 23 | + }; |
| 24 | + |
| 25 | + this.handleSubmit = this.handleSubmit.bind(this); |
| 26 | + this.handleChange = this.handleChange.bind(this); |
| 27 | + } |
| 28 | + handleSubmit(event) { |
| 29 | + event.preventDefault(); |
| 30 | + |
| 31 | + this.props.onSubmit(this.state.username); |
| 32 | + } |
| 33 | + handleChange(event) { |
| 34 | + this.setState({ |
| 35 | + username: event.target.value, |
| 36 | + }); |
| 37 | + } |
| 38 | + render() { |
| 39 | + return ( |
| 40 | + <form className="card" onSubmit={this.handleSubmit}> |
| 41 | + <label htmlFor="username" className="player-label"> |
| 42 | + {this.props.label} |
| 43 | + </label> |
| 44 | + <div className="input-row"> |
| 45 | + <input |
| 46 | + type="text" |
| 47 | + id="username" |
| 48 | + placeholder="github username" |
| 49 | + autoComplete="off" |
| 50 | + value={this.state.username} |
| 51 | + onChange={this.handleChange} |
| 52 | + /> |
| 53 | + <button |
| 54 | + className="btn link" |
| 55 | + type="submit" |
| 56 | + disabled={!this.state.username} |
| 57 | + > |
| 58 | + Submit |
| 59 | + </button> |
| 60 | + </div> |
| 61 | + </form> |
| 62 | + ); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +export default class Battle extends React.Component { |
| 67 | + constructor(props) { |
| 68 | + super(props); |
| 69 | + |
| 70 | + this.state = { |
| 71 | + playerOne: null, |
| 72 | + playerTwo: null, |
| 73 | + }; |
| 74 | + |
| 75 | + this.handleSubmit = this.handleSubmit.bind(this); |
| 76 | + } |
| 77 | + handleSubmit(id, player) { |
| 78 | + this.setState({ |
| 79 | + [id]: player, |
| 80 | + }); |
| 81 | + } |
| 82 | + render() { |
| 83 | + const { playerOne, playerTwo } = this.state; |
| 84 | + const disabled = !playerOne || !playerTwo; |
| 85 | + |
| 86 | + return ( |
| 87 | + <main className="stack main-stack animate-in"> |
| 88 | + <div className="split"> |
| 89 | + <h1>Players</h1> |
| 90 | + <pre>{JSON.stringify(this.state, null, 2)}</pre> |
| 91 | + <a href="#" className={`btn primary ${disabled ? "disabled" : ""}`}> |
| 92 | + Battle |
| 93 | + </a> |
| 94 | + </div> |
| 95 | + <section className="grid"> |
| 96 | + {playerOne === null ? ( |
| 97 | + <PlayerInput |
| 98 | + label="Player One" |
| 99 | + onSubmit={(player) => this.handleSubmit("playerOne", player)} |
| 100 | + /> |
| 101 | + ) : null} |
| 102 | + {playerTwo === null ? ( |
| 103 | + <PlayerInput |
| 104 | + label="Player Two" |
| 105 | + onSubmit={(player) => this.handleSubmit("playerTwo", player)} |
| 106 | + /> |
| 107 | + ) : null} |
| 108 | + </section> |
| 109 | + <Instructions /> |
| 110 | + </main> |
| 111 | + ); |
| 112 | + } |
| 113 | +} |
0 commit comments