summaryrefslogtreecommitdiff
path: root/frontend/src/components/game-browser/GameList.jsx
blob: b46cd14cd4b9aba2bf9ccfb3ae18bc78a811256d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// @flow
import type { List } from 'immutable';
import React from 'react';
import { connect } from 'react-redux';
import type { ApiLobby } from '../../api/model';
import type { GlobalState } from '../../reducers';
import { actions } from '../../redux/actions/lobby';
import { getAllGames } from '../../redux/games';
import { IconButton } from '../shared/IconButton';
import './GameList.css';
import { GameStatus } from './GameStatus';
import { PlayerCount } from './PlayerCount';

type GameListProps = {
  games: List<ApiLobby>,
  joinGame: (gameId: string) => void,
};

const GameListPresenter = ({ games, joinGame }: GameListProps) => (
        <table className='pt-html-table'>
          <thead>
          <GameListHeaderRow />
          </thead>
          <tbody>
          {games.map((game: ApiLobby) => <GameListItemRow key={game.id} game={game} joinGame={joinGame}/>)}
          </tbody>
        </table>
);

const GameListHeaderRow = () => (
  <tr>
    <th>Name</th>
    <th>Status</th>
    <th>Nb Players</th>
    <th>Join</th>
  </tr>
);

const GameListItemRow = ({game, joinGame}) => (
  <tr>
    <td>{game.name}</td>
    <td>
      <GameStatus state={game.state} />
    </td>
    <td>
      <PlayerCount nbPlayers={game.players.size} />
    </td>
    <td>
      <JoinButton game={game} joinGame={joinGame}/>
    </td>
  </tr>
);

const JoinButton = ({game, joinGame}) => {
  const disabled = game.state !== 'LOBBY';
  const onClick = () => joinGame(game.id);
  return <IconButton minimal disabled={disabled} icon='arrow-right' title='Join Game' onClick={onClick}/>;
};

const mapStateToProps = (state: GlobalState) => ({
  games: getAllGames(state),
});

const mapDispatchToProps = {
  joinGame: actions.requestJoinGame,
};

export const GameList = connect(mapStateToProps, mapDispatchToProps)(GameListPresenter);

bgstack15