mirror of
https://github.com/danbee/chess
synced 2025-03-04 08:39:06 +00:00
Push changes to the API
This commit is contained in:
parent
1598932832
commit
0c78341efe
@ -7,4 +7,16 @@ defmodule Chess.Api.GameController do
|
|||||||
game = Repo.get!(Game, id)
|
game = Repo.get!(Game, id)
|
||||||
render conn, "show.json", game: game
|
render conn, "show.json", game: game
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def update(conn, %{"id" => id, "game" => game_params}) do
|
||||||
|
game = Repo.get!(Game, id)
|
||||||
|
changeset = Game.changeset(game, game_params)
|
||||||
|
|
||||||
|
case Repo.update(changeset) do
|
||||||
|
{:ok, game} ->
|
||||||
|
render conn, "show.json", game: game
|
||||||
|
{:error, changeset} ->
|
||||||
|
render(conn, "edit.html", game: game, changeset: changeset)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -5,7 +5,7 @@ defmodule Chess.Router do
|
|||||||
plug :accepts, ["html"]
|
plug :accepts, ["html"]
|
||||||
plug :fetch_session
|
plug :fetch_session
|
||||||
plug :fetch_flash
|
plug :fetch_flash
|
||||||
plug :protect_from_forgery
|
# plug :protect_from_forgery
|
||||||
plug :put_secure_browser_headers
|
plug :put_secure_browser_headers
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -24,6 +24,6 @@ defmodule Chess.Router do
|
|||||||
scope "/api", Chess do
|
scope "/api", Chess do
|
||||||
pipe_through :api
|
pipe_through :api
|
||||||
|
|
||||||
resources "/games", Api.GameController, only: [:show]
|
resources "/games", Api.GameController, only: [:show, :update]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
|
import $ from "jquery";
|
||||||
import classNames from "classnames";
|
import classNames from "classnames";
|
||||||
|
|
||||||
import { movePiece, selectPiece } from "../store/actions";
|
import { movePiece, selectPiece } from "../store/actions";
|
||||||
@ -19,6 +20,7 @@ class ChessBoardSquare extends React.Component {
|
|||||||
|
|
||||||
if (store.getState().selectedSquare != null) {
|
if (store.getState().selectedSquare != null) {
|
||||||
store.dispatch(movePiece(store.getState().selectedSquare, this.squareCoords()));
|
store.dispatch(movePiece(store.getState().selectedSquare, this.squareCoords()));
|
||||||
|
$.ajax({ method: "PATCH", url: "/api/games/" + store.getState().gameId, data: { game: { board: store.getState().board } }});
|
||||||
}
|
}
|
||||||
else if (this.props.piece != undefined) {
|
else if (this.props.piece != undefined) {
|
||||||
store.dispatch(selectPiece(this.squareCoords()));
|
store.dispatch(selectPiece(this.squareCoords()));
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import $ from "jquery";
|
import $ from "jquery";
|
||||||
import { connect } from "react-redux";
|
import { connect } from "react-redux";
|
||||||
import { setBoard } from "../store/actions";
|
import { setBoard, setGameId } from "../store/actions";
|
||||||
|
|
||||||
import ChessBoardSquare from "./chess-board-square";
|
import ChessBoardSquare from "./chess-board-square";
|
||||||
|
|
||||||
@ -9,6 +9,8 @@ class ChessBoard extends React.Component {
|
|||||||
componentWillMount() {
|
componentWillMount() {
|
||||||
const { gameId, store } = this.props;
|
const { gameId, store } = this.props;
|
||||||
|
|
||||||
|
store.dispatch(setGameId(gameId));
|
||||||
|
|
||||||
$.ajax({ method: "GET", url: "/api/games/" + gameId })
|
$.ajax({ method: "GET", url: "/api/games/" + gameId })
|
||||||
.then((data) => store.dispatch(setBoard(data)));
|
.then((data) => store.dispatch(setBoard(data)));
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,6 +6,9 @@ const chessBoardReducer = (state = defaultState, action) => {
|
|||||||
case "SET_BOARD":
|
case "SET_BOARD":
|
||||||
return Object.assign({}, state, { board: action.board });
|
return Object.assign({}, state, { board: action.board });
|
||||||
|
|
||||||
|
case "SET_GAME_ID":
|
||||||
|
return Object.assign({}, state, { gameId: action.gameId });
|
||||||
|
|
||||||
case "MOVE_PIECE":
|
case "MOVE_PIECE":
|
||||||
const newState = {
|
const newState = {
|
||||||
board: movePiece(state.board, action.from, action.to),
|
board: movePiece(state.board, action.from, action.to),
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
const SET_BOARD = "SET_BOARD";
|
const SET_BOARD = "SET_BOARD";
|
||||||
|
const SET_GAME_ID = "SET_GAME_ID";
|
||||||
const SELECT_PIECE = "SELECT_PIECE";
|
const SELECT_PIECE = "SELECT_PIECE";
|
||||||
const MOVE_PIECE = "MOVE_PIECE";
|
const MOVE_PIECE = "MOVE_PIECE";
|
||||||
|
|
||||||
@ -9,6 +10,13 @@ export const setBoard = (board) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const setGameId = (gameId) => {
|
||||||
|
return {
|
||||||
|
type: SET_GAME_ID,
|
||||||
|
gameId: gameId
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export const selectPiece = (coords) => {
|
export const selectPiece = (coords) => {
|
||||||
return {
|
return {
|
||||||
type: SELECT_PIECE,
|
type: SELECT_PIECE,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user