From 6045fbc2d438550a2937bbe328bb46d8471bdf58 Mon Sep 17 00:00:00 2001 From: Thomas Hintz Date: Tue, 7 Apr 2020 20:04:08 -0700 Subject: [PATCH] Moving currentPlayer in to one place. --- src/components/farm/Board.jsx | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/components/farm/Board.jsx b/src/components/farm/Board.jsx index ebbdd59..bf5e34a 100644 --- a/src/components/farm/Board.jsx +++ b/src/components/farm/Board.jsx @@ -1456,9 +1456,7 @@ class Moving extends React.Component { } else { buttons = (); } - const currentPlayer = this.props.player.name === this.props.game.currentPlayer ? - this.props.player : this.props.game.otherPlayers - .find(p => p.player.name === this.props.game.currentPlayer).player; + const { currentPlayer } = this.props; return ( @@ -1481,10 +1479,7 @@ class Moving extends React.Component { class Action extends React.Component { render() { let view, buttons; - const currentPlayer = (this.props.player.name === this.props.game.currentPlayer || - this.props.game.currentPlayer === '') ? - this.props.player : this.props.game.otherPlayers - .find(p => p.player.name === this.props.game.currentPlayer).player; + const { currentPlayer } = this.props; switch (this.props.ui.action) { case 'otb': if (this.props.player.name === this.props.game.currentPlayer) { @@ -1884,7 +1879,8 @@ class BoardApp extends React.Component { this.state = { screen: SCREENS.summary, card: props.ui.card, - timerId: false + timerId: false, + currentPlayer: this.props.player }; this.myRef = React.createRef(); this.actionRef = React.createRef(); @@ -1906,7 +1902,7 @@ class BoardApp extends React.Component { this.setState({ card }); } - componentDidUpdate() { + componentDidUpdate(prevProps) { if (this.state.card.type === 'no-card' && this.props.ui.cards.length > 0) { this.setState({ card: this.props.ui.cards[0] }); @@ -1925,9 +1921,20 @@ class BoardApp extends React.Component { this.actionShowing()) { this.props.alertHandled(this.props.ui.unhandledAlert.id); } + + if (prevProps.game.currentPlayer !== this.props.game.currentPlayer) { + const currentPlayer = this.props.player.name === this.props.game.currentPlayer ? + this.props.player : this.props.game.otherPlayers + .find(p => p.player.name === this.props.game.currentPlayer).player; + this.setState({ currentPlayer }); + } } componentDidMount() { + const currentPlayer = this.props.player.name === this.props.game.currentPlayer ? + this.props.player : this.props.game.otherPlayers + .find(p => p.player.name === this.props.game.currentPlayer).player; + this.setState({ currentPlayer }); // const midColHeight = (window.innerHeight - // (document.getElementsByClassName('flex-row')[0].offsetHeight * 2)) + 'px'; // const midCols = document.getElementsByClassName('mid-col'); @@ -2076,6 +2083,7 @@ class BoardApp extends React.Component { } const actionComponent = ( this.actionRef = x}