1
0
mirror of https://github.com/danbee/chess synced 2025-03-04 08:39:06 +00:00
chess/web/static/js/components/chess-board.js
Dan Barber d888ab8d83
Make sure the JS works in PhantomJS
PhantomJS does not implement `Object.assign` which is how we were
updating the state immutably. I've switched it over to use the
ImmutableJS library instead.
2016-12-22 10:28:33 +00:00

66 lines
1.4 KiB
JavaScript

import React from "react";
import _ from "lodash";
import $ from "jquery";
import { connect } from "react-redux";
import { setBoard, setGameId } from "../store/actions";
import ChessBoardSquare from "./chess-board-square";
class ChessBoard extends React.Component {
componentWillMount() {
const { gameId, store } = this.props;
store.dispatch(setGameId(gameId));
$.ajax({ method: "GET", url: "/api/games/" + gameId })
.then((data) => store.dispatch(setBoard(data)));
}
getBoard() {
const { store } = this.props;
return store.getState().board;
}
renderFiles(rankId) {
const { store } = this.props;
const rank = this.getBoard()[rankId];
return _.map(Object.keys(rank).sort(), (fileId) => {
return (
<ChessBoardSquare
file={fileId}
key={fileId}
rank={rankId}
piece={rank[fileId]}
store={store}
/>
);
});
}
renderRanks() {
const board = this.getBoard();
return _.map(Object.keys(board).reverse(), (rankId) => {
return (
<div className="board-rank" key={rankId}>
{this.renderFiles(rankId)}
</div>
);
});
}
render() {
return <div className="board">{this.renderRanks()}</div>;
}
}
function mapStateToProps(state) {
return {
board: state.board,
selectedSquare: state.selectedSquare
}
}
export default connect(mapStateToProps)(ChessBoard);