11import React , { Component } from 'react' ;
2- import { GamesController } from '../api/controllers/gamesController.js ' ;
2+ import GameHeader from './GameHeader.jsx ' ;
33import { Game , GameStatuses } from '../api/models/game.js' ;
44import { newGame , userJoinGame , userLeaveGame } from '../api/methods/games.js' ;
55
@@ -12,71 +12,100 @@ export default class GameList extends Component {
1212 userLeaveGame . call ( { gameId : gameId } ) ;
1313 }
1414
15- handleJoinGame ( gameId ) {
15+ handleJoinGame ( gameId ) {
1616 userJoinGame . call ( { gameId : gameId } ) ;
1717 }
1818
19- handleEnterGame ( gameId ) {
19+ handleEnterGame ( gameId ) {
2020 this . props . enterGameHandler ( gameId ) ;
2121 }
2222
23- activeGames ( ) {
23+ activeGames ( ) {
2424 return _ . filter ( this . props . games , ( game ) => {
2525 return game . status === GameStatuses . WAITING || game . status === GameStatuses . STARTED ;
2626 } ) ;
2727 }
2828
29- myCurrentGameId ( ) {
29+ myCurrentGameId ( ) {
3030 let game = _ . find ( this . activeGames ( ) , ( game ) => {
3131 return game . userIndex ( this . props . user ) !== null ;
3232 } ) ;
3333 return game === undefined ? null : game . _id ;
3434 }
3535
36- renderPlayers ( game ) {
37- let player1 = game . players . length > 0 ? game . players [ 0 ] . username : '' ;
38- let player2 = game . players . length > 1 ? game . players [ 1 ] . username : '' ;
36+ renderPlayers ( game ) {
37+ let player1 = game . players . length > 0 ? game . players [ 0 ] . username : '(waiting) ' ;
38+ let player2 = game . players . length > 1 ? game . players [ 1 ] . username : '(waiting) ' ;
3939 return (
40- < span > [{ player1 } ] vs [{ player2 } ]</ span >
40+ < div >
41+ < div >
42+ < i className = "user icon" > </ i > { player1 }
43+ </ div >
44+ < div >
45+ < i className = "user icon" > </ i > { player2 }
46+ </ div >
47+ </ div >
4148 )
4249 }
4350
44- render ( ) {
51+ render ( ) {
4552 return (
46- < div >
47- < div >
48- < h1 > List of games</ h1 >
49- { this . activeGames ( ) . map ( ( game , index ) => {
50- return (
51- < div key = { game . _id } >
52- < span > Game { index + 1 } </ span >
53- { this . renderPlayers ( game ) }
53+ < div className = "ui container" >
54+ < GameHeader user = { this . props . user } />
5455
55- { /* can leave only if user is in the game, and the game is not started */ }
56- { this . myCurrentGameId ( ) === game . _id && game . status === GameStatuses . WAITING ? (
57- < button onClick = { this . handleLeaveGame . bind ( this , game . _id ) } > Leave</ button >
58- ) : null }
56+ < h1 className = "ui top attached header" > List of games</ h1 >
57+ < div className = "ui attached segment" >
58+ < div className = "ui three cards" >
59+ { this . activeGames ( ) . map ( ( game , index ) => {
60+ return (
61+ < div key = { game . _id } className = "ui card" >
62+ < div className = "content" >
63+ < div className = "header" >
64+ { game . status === GameStatuses . WAITING ? (
65+ < span className = "ui right yellow corner label" >
66+ < i className = "idea icon" />
67+ </ span >
68+ ) : null }
69+ Game { index + 1 }
70+ </ div >
71+ </ div >
72+ < div className = "content" >
73+ { this . renderPlayers ( game ) }
74+ </ div >
5975
60- { /* can join only if user is not in any game, and the game is not started */ }
61- { this . myCurrentGameId ( ) === null && game . status === GameStatuses . WAITING ? (
62- < button onClick = { this . handleJoinGame . bind ( this , game . _id ) } > Join</ button >
63- ) : null }
76+ < div className = "extra content" >
77+ { /* can leave only if user is in the game, and the game is not started */ }
78+ { this . myCurrentGameId ( ) === game . _id && game . status === GameStatuses . WAITING ? (
79+ < button className = "ui red button" onClick = { this . handleLeaveGame . bind ( this , game . _id ) } > Leave</ button >
80+ ) : null }
6481
65- { /* can enter only if the game is started */ }
66- { game . status === GameStatuses . STARTED ? (
67- < button onClick = { this . handleEnterGame . bind ( this , game . _id ) } > Enter</ button >
68- ) : null }
69- </ div >
70- )
71- } ) }
72- </ div >
82+ { /* can join only if user is not in any game, and the game is not started */ }
83+ { this . myCurrentGameId ( ) === null && game . status === GameStatuses . WAITING ? (
84+ < button className = "ui green button" onClick = { this . handleJoinGame . bind ( this , game . _id ) } > Join</ button >
85+ ) : null }
7386
74- { /* Only show new game button if player is not in any room */ }
75- { this . myCurrentGameId ( ) === null ? (
76- < div >
77- < button onClick = { this . handleNewGame . bind ( this ) } > New Game</ button >
87+ { /* can enter only if the game is started */ }
88+ { game . status === GameStatuses . STARTED ? (
89+ < button className = "ui blue button" onClick = { this . handleEnterGame . bind ( this , game . _id ) } > Enter</ button >
90+ ) : null }
91+
92+ { /* just a invisible dummy button to make up the space */ }
93+ < button className = "ui button" style = { { visibility : "hidden" } } > Dummy</ button >
94+ </ div >
95+ </ div >
96+ )
97+ } ) }
7898 </ div >
79- ) : null }
99+
100+ { /* Only show new game button if player is not in any room */ }
101+ </ div >
102+ < div className = "ui attached segment" >
103+ { this . myCurrentGameId ( ) === null ? (
104+ < div >
105+ < button className = "ui green button" onClick = { this . handleNewGame . bind ( this ) } > New Game</ button >
106+ </ div >
107+ ) : null }
108+ </ div >
80109 </ div >
81110 )
82111 }
0 commit comments