mirror of
https://github.com/danbee/chess
synced 2025-03-04 08:39:06 +00:00
Plumb in redux
This commit is contained in:
parent
dab37155e4
commit
a2fa4ec53b
@ -2,18 +2,26 @@
|
|||||||
|
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import ReactDOM from "react-dom";
|
import ReactDOM from "react-dom";
|
||||||
|
import { createStore } from "redux";
|
||||||
|
import { Provider } from "react-redux";
|
||||||
|
|
||||||
|
import chessBoardReducer from "reducers/chess-board";
|
||||||
|
|
||||||
|
const store = createStore(chessBoardReducer);
|
||||||
|
|
||||||
import ChessBoard from "components/chess-board";
|
import ChessBoard from "components/chess-board";
|
||||||
|
|
||||||
class App extends React.Component {
|
class App extends React.Component {
|
||||||
render() {
|
render() {
|
||||||
|
const { store } = this.props;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ChessBoard />
|
<ChessBoard store={store} />
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ReactDOM.render(
|
ReactDOM.render(
|
||||||
<App />,
|
<App store={store} />,
|
||||||
document.getElementById('app')
|
document.getElementById('app')
|
||||||
);
|
);
|
||||||
|
|||||||
@ -4,13 +4,17 @@ import classNames from "classnames";
|
|||||||
class ChessBoardSquare extends React.Component {
|
class ChessBoardSquare extends React.Component {
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props);
|
super(props);
|
||||||
|
|
||||||
|
this.selectSquare = this.selectSquare.bind(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
selectSquare() {
|
||||||
|
console.log(`Clicked: ${this.props.rank}, ${this.props.file}`);
|
||||||
|
};
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
if (this.props.square == undefined) {
|
if (this.props.square == undefined) {
|
||||||
var squareClass = classNames(
|
var squareClass = "board-square";
|
||||||
"board-square",
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
var squareClass = classNames(
|
var squareClass = classNames(
|
||||||
@ -20,7 +24,7 @@ class ChessBoardSquare extends React.Component {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
return <div className={squareClass} />
|
return <div className={squareClass} onClick={this.selectSquare} />
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,20 +1,24 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
|
|
||||||
import defaultState from "store/default_state";
|
|
||||||
|
|
||||||
import ChessBoardSquare from "components/chess-board-square";
|
import ChessBoardSquare from "components/chess-board-square";
|
||||||
|
|
||||||
class ChessBoard extends React.Component {
|
class ChessBoard extends React.Component {
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props);
|
super(props);
|
||||||
this.state = defaultState;
|
}
|
||||||
|
|
||||||
|
chessBoardRows() {
|
||||||
|
const { store } = this.props;
|
||||||
|
return store.getState().board;
|
||||||
}
|
}
|
||||||
|
|
||||||
chessBoardRow(row, i) {
|
chessBoardRow(row, i) {
|
||||||
return (
|
return (
|
||||||
<div className="board-row" key={i}>
|
<div className="board-rank" key={i}>
|
||||||
{row.map(
|
{row.map(
|
||||||
(square, j) => <ChessBoardSquare key={j} square={square} />
|
(square, j) => (
|
||||||
|
<ChessBoardSquare key={j} rank={i} file={j} square={square} />
|
||||||
|
)
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
@ -23,7 +27,7 @@ class ChessBoard extends React.Component {
|
|||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<div className="board">
|
<div className="board">
|
||||||
{this.state.board.map((row, i) => this.chessBoardRow(row, i))}
|
{this.chessBoardRows().map((row, i) => this.chessBoardRow(row, i))}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,9 +1,19 @@
|
|||||||
import { defaultState } from "store/default_state";
|
import defaultState from "store/default-state";
|
||||||
|
|
||||||
export const chessBoard = (state = defaultState, action) => {
|
const chessBoardReducer = (state = defaultState, action) => {
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
|
case "MOVE_PIECE":
|
||||||
|
var piece = state.board[from.rank][from.file];
|
||||||
|
state.board[to.rank][to.file] = piece;
|
||||||
|
state.board[from.rank][from.file] = null;
|
||||||
|
return state;
|
||||||
|
|
||||||
|
case "SELECT_PIECE":
|
||||||
|
return Object.assign({}, state, { selectedSquare: action.coords });
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default chessBoardReducer;
|
||||||
|
|||||||
17
app/store/actions.js
Normal file
17
app/store/actions.js
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
const SELECT_PIECE = "SELECT_PIECE";
|
||||||
|
const MOVE_PIECE = "MOVE_PIECE";
|
||||||
|
|
||||||
|
export const selectPiece = (coords) => {
|
||||||
|
return {
|
||||||
|
type: selectPiece,
|
||||||
|
coords: coords
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export const movePiece = (from, to) => {
|
||||||
|
return {
|
||||||
|
type: movePiece,
|
||||||
|
from: from,
|
||||||
|
to: to
|
||||||
|
};
|
||||||
|
}
|
||||||
@ -1,4 +1,6 @@
|
|||||||
const defaultState = {
|
const defaultState = {
|
||||||
|
selectedSquare: null,
|
||||||
|
|
||||||
board: [
|
board: [
|
||||||
[
|
[
|
||||||
{ type: "rook", colour: "black" },
|
{ type: "rook", colour: "black" },
|
||||||
@ -48,7 +48,7 @@ $pieces: king queen bishop knight rook pawn;
|
|||||||
background-color: $white-square-color;
|
background-color: $white-square-color;
|
||||||
}
|
}
|
||||||
|
|
||||||
.board-row:nth-child(odd) {
|
.board-rank:nth-child(odd) {
|
||||||
.board-square:nth-child(even) {
|
.board-square:nth-child(even) {
|
||||||
@extend %black-square;
|
@extend %black-square;
|
||||||
}
|
}
|
||||||
@ -57,7 +57,7 @@ $pieces: king queen bishop knight rook pawn;
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.board-row:nth-child(even) {
|
.board-rank:nth-child(even) {
|
||||||
.board-square:nth-child(even) {
|
.board-square:nth-child(even) {
|
||||||
@extend %white-square;
|
@extend %white-square;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -12,6 +12,7 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"classnames": "^2.2.5",
|
"classnames": "^2.2.5",
|
||||||
|
"immutable": "^3.8.1",
|
||||||
"lodash": "^4.17.2",
|
"lodash": "^4.17.2",
|
||||||
"react": "^15.4.1",
|
"react": "^15.4.1",
|
||||||
"react-dom": "^15.4.1",
|
"react-dom": "^15.4.1",
|
||||||
|
|||||||
@ -1669,6 +1669,10 @@ ieee754@^1.1.4:
|
|||||||
version "1.1.8"
|
version "1.1.8"
|
||||||
resolved "https://registry.yarnpkg.com/ieee754/-/ieee754-1.1.8.tgz#be33d40ac10ef1926701f6f08a2d86fbfd1ad3e4"
|
resolved "https://registry.yarnpkg.com/ieee754/-/ieee754-1.1.8.tgz#be33d40ac10ef1926701f6f08a2d86fbfd1ad3e4"
|
||||||
|
|
||||||
|
immutable@^3.8.1:
|
||||||
|
version "3.8.1"
|
||||||
|
resolved "https://registry.yarnpkg.com/immutable/-/immutable-3.8.1.tgz#200807f11ab0f72710ea485542de088075f68cd2"
|
||||||
|
|
||||||
in-publish@^2.0.0:
|
in-publish@^2.0.0:
|
||||||
version "2.0.0"
|
version "2.0.0"
|
||||||
resolved "https://registry.yarnpkg.com/in-publish/-/in-publish-2.0.0.tgz#e20ff5e3a2afc2690320b6dc552682a9c7fadf51"
|
resolved "https://registry.yarnpkg.com/in-publish/-/in-publish-2.0.0.tgz#e20ff5e3a2afc2690320b6dc552682a9c7fadf51"
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user