1+ import React from 'react'
2+ import PropTypes from 'prop-types'
3+ import { ThemeConsumer } from '../contexts/theme'
4+ import { Link } from 'react-router-dom'
5+
6+ import { FaUserFriends , FaFighterJet , FaTrophy , FaTimesCircle } from 'react-icons/fa'
7+
8+ function Instructions ( ) {
9+ return (
10+ < ThemeConsumer >
11+ { ( { theme } ) => (
12+ < div className = 'instructions-container' >
13+ < h1 className = 'center-text header-lg' >
14+ Instructions
15+ </ h1 >
16+ < ol className = 'container-sm grid center-text battle-instructions' >
17+ < li >
18+ < h3 className = 'header-sm' > Enter two Github users</ h3 >
19+ < FaUserFriends className = { `bg-${ theme } ` } color = 'rgb(255, 191, 116)' size = { 140 } />
20+ </ li >
21+ < li >
22+ < h3 className = 'header-sm' > Battle</ h3 >
23+ < FaFighterJet className = { `bg-${ theme } ` } color = '#727272' size = { 140 } />
24+ </ li >
25+ < li >
26+ < h3 className = 'header-sm' > See the winners</ h3 >
27+ < FaTrophy className = { `bg-${ theme } ` } color = 'rgb(255, 215, 0)' size = { 140 } />
28+ </ li >
29+ </ ol >
30+ </ div >
31+ ) }
32+ </ ThemeConsumer >
33+ )
34+ }
35+
36+ class PlayerInput extends React . Component {
37+ state = {
38+ username : ''
39+ }
40+
41+ handleSubmit = ( e ) => {
42+ e . preventDefault ( )
43+ this . props . onSubmit ( this . state . username )
44+ }
45+
46+ handleChange = ( e ) => {
47+ this . setState ( {
48+ username : e . target . value
49+ } )
50+ }
51+
52+ render ( ) {
53+ return (
54+ < ThemeConsumer >
55+ { ( { theme } ) => (
56+ < form className = 'column player' onSubmit = { this . handleSubmit } >
57+ < label htmlFor = 'username' className = 'player-label' >
58+ { this . props . label }
59+ </ label >
60+ < div className = 'row player-inputs' >
61+ < input
62+ type = 'text'
63+ id = 'username'
64+ className = { `input-${ theme } ` }
65+ placeholder = 'github username'
66+ autoComplete = 'off'
67+ value = { this . state . username }
68+ onChange = { this . handleChange }
69+ />
70+ < button
71+ className = { `btn ${ theme } === 'dark' ? light-btn : dark-btn` }
72+ type = 'submit'
73+ disabled = { ! this . state . username }
74+ > Submit</ button >
75+ </ div >
76+ </ form >
77+ ) }
78+ </ ThemeConsumer >
79+ )
80+ }
81+ }
82+
83+ PlayerInput . propTypes = {
84+ onSubmit : PropTypes . func . isRequired ,
85+ label : PropTypes . string . isRequired
86+ }
87+
88+ function PlayerPreview ( { username, onReset, label} ) {
89+ return (
90+ < ThemeConsumer >
91+ { ( { theme } ) => (
92+ < div className = 'column player' >
93+ < h3 className = 'player-label' > { label } </ h3 >
94+ < div className = { `row bg-${ theme } ` } >
95+ < div className = 'player-info' >
96+ < img
97+ className = 'avatar-small'
98+ src = { `https://github.com/${ username } .png?size=200` }
99+ alt = { `Avatar for ${ username } ` }
100+ />
101+ < a
102+ href = { `https://github.com/${ username } ` }
103+ className = 'link' >
104+ { username }
105+ </ a >
106+ </ div >
107+ < button className = 'btn-clear flex-center' onClick = { onReset } >
108+ < FaTimesCircle color = 'rgb(194, 57, 42)' size = { 26 } />
109+ </ button >
110+ </ div >
111+ </ div >
112+ ) }
113+ </ ThemeConsumer >
114+ )
115+ }
116+
117+ PlayerPreview . propTypes = {
118+ username : PropTypes . string . isRequired ,
119+ onReset : PropTypes . func . isRequired ,
120+ label : PropTypes . string . isRequired
121+ }
122+
123+ // BATTLE COMPONENT
124+ export default class Battle extends React . Component {
125+ state = {
126+ playerOne : null ,
127+ playerTwo : null ,
128+ }
129+
130+ handleSubmit = ( id , player ) => {
131+ this . setState ( {
132+ [ id ] : player
133+ } )
134+ }
135+
136+ handleReset = ( id ) => {
137+ this . setState ( {
138+ [ id ] : null
139+ } )
140+ }
141+
142+ render ( ) {
143+ const { playerOne, playerTwo } = this . state
144+
145+ return (
146+ < React . Fragment >
147+ < Instructions />
148+ < div className = 'players-container' >
149+ < h1 className = 'center-text header-lg' > Players</ h1 >
150+ < div className = 'row space-around' >
151+ { playerOne === null
152+ ? < PlayerInput
153+ label = 'Player One'
154+ onSubmit = { ( player ) => this . handleSubmit ( 'playerOne' , player ) }
155+ />
156+ : < PlayerPreview username = { playerOne } label = 'Player One' onReset = { ( ) => this . handleReset ( 'playerOne' ) } />
157+
158+ }
159+ { playerTwo === null
160+ ? < PlayerInput
161+ label = 'Player Two'
162+ onSubmit = { ( player ) => this . handleSubmit ( 'playerTwo' , player ) }
163+ />
164+ : < PlayerPreview username = { playerTwo } label = 'Player Two' onReset = { ( ) => this . handleReset ( 'playerTwo' ) } />
165+ }
166+ </ div >
167+ { playerOne && playerTwo && (
168+ < Link
169+ className = 'btn dark-btn btn-space'
170+ to = { {
171+ pathname : '/battle/results' ,
172+ search : `playerOne=${ playerOne } &playerTwo=${ playerTwo } `
173+ } }
174+ > Battle
175+ </ Link >
176+ ) }
177+ </ div >
178+ </ React . Fragment >
179+ )
180+ }
181+ }
0 commit comments