mirror of
https://github.com/danbee/chess
synced 2025-03-04 08:39:06 +00:00
Render moves on the page
This commit is contained in:
parent
5cef37c529
commit
5a04ef47bf
@ -13,6 +13,7 @@ import chessBoardReducer from "./reducers/chess-board";
|
|||||||
import { setGameId } from "./store/actions";
|
import { setGameId } from "./store/actions";
|
||||||
|
|
||||||
import ChessBoard from "./components/chess-board";
|
import ChessBoard from "./components/chess-board";
|
||||||
|
import MoveList from "./components/move-list";
|
||||||
|
|
||||||
const store = createStore(chessBoardReducer);
|
const store = createStore(chessBoardReducer);
|
||||||
|
|
||||||
@ -25,15 +26,24 @@ class App extends React.Component {
|
|||||||
this.channel = new Channel(store, gameId);
|
this.channel = new Channel(store, gameId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get moves() {
|
||||||
|
const { store } = this.props;
|
||||||
|
return store.getState().moves;
|
||||||
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { store, gameId } = this.props;
|
const { store, gameId } = this.props;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
<div>
|
||||||
<ChessBoard
|
<ChessBoard
|
||||||
gameId={gameId}
|
gameId={gameId}
|
||||||
store={store}
|
store={store}
|
||||||
channel={this.channel}
|
channel={this.channel}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
<MoveList store={store} />
|
||||||
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
27
assets/js/components/move-list.js
Normal file
27
assets/js/components/move-list.js
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
import React from "react";
|
||||||
|
import { connect } from "react-redux";
|
||||||
|
import _ from "lodash";
|
||||||
|
|
||||||
|
const renderMoves = (moves) => {
|
||||||
|
return _.map(moves, (move) => {
|
||||||
|
return (
|
||||||
|
<li key={move}>{move}</li>
|
||||||
|
);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const MoveList = (props) => {
|
||||||
|
return (
|
||||||
|
<ol className="move-list">
|
||||||
|
{renderMoves(props.moves)}
|
||||||
|
</ol>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
function mapStateToProps(state) {
|
||||||
|
return {
|
||||||
|
moves: state.moves,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export default connect(mapStateToProps)(MoveList);
|
||||||
@ -15,7 +15,8 @@ const chessBoardReducer = (state = defaultState, action) => {
|
|||||||
.set("turn", action.turn)
|
.set("turn", action.turn)
|
||||||
.set("state", action.state)
|
.set("state", action.state)
|
||||||
.set("selectedSquare", null)
|
.set("selectedSquare", null)
|
||||||
.set("moves", [])
|
.set("availableMoves", [])
|
||||||
|
.set("moves", action.moves)
|
||||||
.toJS();
|
.toJS();
|
||||||
|
|
||||||
case "SET_AVAILABLE_MOVES":
|
case "SET_AVAILABLE_MOVES":
|
||||||
@ -31,7 +32,7 @@ const chessBoardReducer = (state = defaultState, action) => {
|
|||||||
case "SELECT_PIECE":
|
case "SELECT_PIECE":
|
||||||
return Immutable.fromJS(state)
|
return Immutable.fromJS(state)
|
||||||
.set("selectedSquare", action.coords)
|
.set("selectedSquare", action.coords)
|
||||||
.set("moves", [])
|
.set("availableMoves", [])
|
||||||
.toJS();
|
.toJS();
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import socket from "../socket";
|
import socket from "../socket";
|
||||||
import { setPlayer, setGame, setMoves } from "../store/actions";
|
import { setPlayer, setGame, setAvailableMoves } from "../store/actions";
|
||||||
|
|
||||||
class Channel {
|
class Channel {
|
||||||
constructor(store, gameId) {
|
constructor(store, gameId) {
|
||||||
|
|||||||
@ -16,6 +16,7 @@ export const setGame = (data) => {
|
|||||||
type: SET_GAME,
|
type: SET_GAME,
|
||||||
board: data.board,
|
board: data.board,
|
||||||
turn: data.turn,
|
turn: data.turn,
|
||||||
|
moves: data.moves,
|
||||||
state: data.state,
|
state: data.state,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@ -7,6 +7,8 @@ const defaultState = {
|
|||||||
|
|
||||||
availableMoves: [],
|
availableMoves: [],
|
||||||
|
|
||||||
|
moves: [],
|
||||||
|
|
||||||
board: {
|
board: {
|
||||||
8: { a: null, b: null, c: null, d: null, e: null, f: null, g: null, h: null },
|
8: { a: null, b: null, c: null, d: null, e: null, f: null, g: null, h: null },
|
||||||
7: { a: null, b: null, c: null, d: null, e: null, f: null, g: null, h: null },
|
7: { a: null, b: null, c: null, d: null, e: null, f: null, g: null, h: null },
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user