1
0
mirror of https://github.com/danbee/chess synced 2025-03-04 08:39:06 +00:00

Separate out game state into its own component

This commit is contained in:
Daniel Barber 2018-04-07 18:19:46 -04:00
parent 5556de00b0
commit d86d94dc70
Signed by: danbarber
GPG Key ID: 931D8112E0103DD8
4 changed files with 41 additions and 19 deletions

View File

@ -6,6 +6,7 @@ import classNames from "classnames";
import ChessBoardSquare from "./chess-board-square";
import RankLabels from "./rank-labels";
import FileLabels from "./file-labels";
import GameState from "./game-state";
class ChessBoard extends React.Component {
componentWillMount() {
@ -74,12 +75,6 @@ class ChessBoard extends React.Component {
});
}
get gameState() {
const { store } = this.props;
console.log(store.getState().state);
return store.getState().state;
}
get boardClass() {
const turn = this.getTurn();
const player = this.getPlayer();
@ -88,6 +83,8 @@ class ChessBoard extends React.Component {
}
render() {
const { store } = this.props;
return (
<div className={this.boardClass}>
<RankLabels />
@ -97,9 +94,7 @@ class ChessBoard extends React.Component {
{this.renderSquares()}
</div>
<div className="board-game-state">
{this.gameState}
</div>
<GameState store={store} />
</div>
);
}

View File

@ -1,12 +1,7 @@
import React from "react";
import _ from "lodash";
import classNames from "classnames";
class FileLabels extends React.Component {
constructor(props) {
super(props);
}
get fileLabels() {
return ["a", "b", "c", "d", "e", "f", "g", "h"];
}

View File

@ -0,0 +1,37 @@
import React from "react";
import classNames from "classnames";
class GameState extends React.Component {
get gameStates() {
return {
"checkmate": "Checkmate!",
"stalemate": "Stalemate",
"check": "Check",
};
}
get gameState() {
const { store } = this.props;
return store.getState().state;
}
get friendlyGameState() {
return this.gameStates[this.gameState];
}
get gameStateClass() {
const state = this.gameState;
return classNames("board-game-state", state);
}
render() {
return (
<div className={this.gameStateClass}>
{this.friendlyGameState}
</div>
);
}
}
export default GameState;

View File

@ -1,12 +1,7 @@
import React from "react";
import _ from "lodash";
import classNames from "classnames";
class RankLabels extends React.Component {
constructor(props) {
super(props);
}
get rankLabels() {
return [1, 2, 3, 4, 5, 6, 7, 8];
}