summaryrefslogtreecommitdiff
path: root/frontend/src/scenes/GameBrowser/index.js
blob: 5a94e290f0a20cf35397a59d671041559344fbff (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
70
71
72
73
// @flow
import { Button, Classes, InputGroup, Intent, Text } from '@blueprintjs/core';
import type { List } from 'immutable';
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { Flex } from 'reflexbox';
import { GameList } from '../../components/gameList';
import type { Game } from '../../models/games';
import type { Player } from '../../models/players';
import { actions, getAllGames } from '../../redux/games';
import { getCurrentPlayer } from '../../redux/players';

export type GameBrowserProps = {
  currentPlayer: Player,
  games: List<Game>,
  createGame: (gameName: string) => void,
  joinGame: (gameId: string) => void
}

class GameBrowserPresenter extends Component<GameBrowserProps> {
  props: {
    currentPlayer: Player,
    games: List<Game>,
    createGame: (gameName: string) => void,
    joinGame: (gameId: string) => void
  };

  _gameName: string | void = undefined;

  createGame = (e: SyntheticEvent<*>): void => {
    e.preventDefault();
    if (this._gameName !== undefined) {
      this.props.createGame(this._gameName);
    }
  };

  render() {
    return (
      <div>
        <Flex align="center" p={1}>
          <InputGroup
                  placeholder="Game name"
                  name="game_name"
                  onChange={(e: SyntheticInputEvent<*>) => (this._gameName = e.target.value)}
                  rightElement={<CreateGameButton onClick={this.createGame}/>}
          />
          <Text>
            <b>Username:</b>
            {' '}
            {this.props.currentPlayer && this.props.currentPlayer.displayName}
          </Text>
        </Flex>
        <GameList games={this.props.games} joinGame={this.props.joinGame} />
      </div>
    );
  }
}

const CreateGameButton = ({onClick}) => (
  <Button className={Classes.MINIMAL} onClick={onClick} intent={Intent.PRIMARY}>Create Game</Button>
);

const mapStateToProps = state => ({
  currentPlayer: getCurrentPlayer(state.get('players')),
  games: getAllGames(state.get('games')),
});

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

export const GameBrowser = connect(mapStateToProps, mapDispatchToProps)(GameBrowserPresenter);
bgstack15