import React from "react";
import _ from "lodash";
import $ from "jquery";
import { connect } from "react-redux";
import classNames from "classnames";
import socket from "../socket";
import { setPlayer, setGame, 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(setPlayer(data.player));
store.dispatch(setGame(data));
});
this.channel = socket.channel(`game:${gameId}`, {});
this.channel.join()
.receive("error", resp => {
console.log("Unable to join", resp);
});
this.channel.on("game_update", data => {
store.dispatch(setGame(data));
});
}
getTurn() {
const { store } = this.props;
return store.getState().turn;
}
getBoard() {
const { store } = this.props;
return store.getState().board;
}
getPlayer() {
const { store } = this.props;
return store.getState().player;
}
renderFiles(rankId) {
const { store } = this.props;
const rank = this.getBoard()[rankId];
return _.map(this.files(rank), fileId => {
return (