Go back button and go ahead button implemented
TODO - Improve the styiling of the GameHistory component
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import React, { useContext } from 'react'
|
||||
import { ChessGameContext } from '../context/chess-game-context'
|
||||
import { Button, Flex, Text } from '@mantine/core';
|
||||
import { Button, Flex, Group, Text } from '@mantine/core';
|
||||
|
||||
const GameHistory = () => {
|
||||
const { gameHistory, jumpTo } = useContext(ChessGameContext)
|
||||
const { gameHistory, jumpTo, currentIndex,goBack,goAhead } = useContext(ChessGameContext)
|
||||
|
||||
let gameHistoryJSX = [];
|
||||
for (let i = 0; i < gameHistory.length;) {
|
||||
@@ -21,10 +21,14 @@ const GameHistory = () => {
|
||||
</Flex>
|
||||
)
|
||||
}
|
||||
|
||||
console.log(currentIndex)
|
||||
return (
|
||||
<div>
|
||||
{gameHistoryJSX}
|
||||
<Group>
|
||||
<Button onClick={() => {goBack()}} disabled={currentIndex <= 0 ? true : false}>Go Back</Button>
|
||||
<Button onClick={() => {goAhead()}} disabled={currentIndex + 1 === gameHistory.length ? true : false}>Next</Button>
|
||||
</Group>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ const reducer = (state, action) => {
|
||||
let updatedGameHistory = state.gameHistory;
|
||||
let move = newChessObj.move(action.val);
|
||||
updatedGameHistory.push({ move: move.san, fen: newChessObj.fen() });
|
||||
return { ...state, chess: newChessObj, chessBoard: newChessObj.getBoard(localStorage.getItem('myColor')), moveHints: [], selected: null, gameHistory: updatedGameHistory, currentIndex: -1 };
|
||||
return { ...state, chess: newChessObj, chessBoard: newChessObj.getBoard(localStorage.getItem('myColor')), moveHints: [], selected: null, gameHistory: updatedGameHistory, currentIndex: updatedGameHistory.length - 1 };
|
||||
}
|
||||
case CAPTURE_PIECE:
|
||||
{
|
||||
@@ -31,7 +31,7 @@ const reducer = (state, action) => {
|
||||
let updatedGameHistory = state.gameHistory;
|
||||
let move = newChessObj.move(action.val);
|
||||
updatedGameHistory.push({ move: move.san, fen: newChessObj.fen() });
|
||||
return { ...state, chess: newChessObj, chessBoard: newChessObj.getBoard(localStorage.getItem('myColor')), moveHints: [], selected: null, gameHistory: updatedGameHistory, currentIndex: -1 };
|
||||
return { ...state, chess: newChessObj, chessBoard: newChessObj.getBoard(localStorage.getItem('myColor')), moveHints: [], selected: null, gameHistory: updatedGameHistory, currentIndex: updatedGameHistory.length - 1 };
|
||||
}
|
||||
case JUMP_TO:
|
||||
{
|
||||
@@ -153,9 +153,21 @@ const ChessGameContextProvider = ({ children }) => {
|
||||
}
|
||||
}
|
||||
|
||||
function goBack() {
|
||||
if (currentIndex > 0) {
|
||||
jumpTo(currentIndex - 1);
|
||||
}
|
||||
}
|
||||
|
||||
function goAhead() {
|
||||
if (currentIndex < gameHistory.length - 1) {
|
||||
jumpTo(currentIndex + 1);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<ChessGameContext.Provider value={{
|
||||
myColor, chessBoard, moveHints, selected, handleOpponentMove, handleSquareClick, getSquareColor, isSquareMarked, selectPiece, handleDrop, gameHistory, jumpTo, getChessBoard
|
||||
myColor, chessBoard, moveHints, selected, handleOpponentMove, handleSquareClick, getSquareColor, isSquareMarked, selectPiece, handleDrop, gameHistory, jumpTo, getChessBoard, currentIndex, goAhead, goBack
|
||||
}}>
|
||||
{children}
|
||||
<audio src='/src/assets/move-self.mp3' ref={moveAudioRef} />
|
||||
|
||||
Reference in New Issue
Block a user