little fixes
This commit is contained in:
parent
e2c70835e2
commit
c1f35b8207
@ -99,9 +99,8 @@ const CardsField: React.FC<CardsFieldProps> = ({cards, isGameStarted = false, fi
|
|||||||
}
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (isAllCardsMatched(cardsState)) {
|
if (isAllCardsMatched(cardsState)) setTimeout(finishGameCallback, 800);
|
||||||
setTimeout(finishGameCallback, 800);
|
if (isGameStarted) localStorage.setItem('match-game:cards', JSON.stringify(cardsState));
|
||||||
}
|
|
||||||
}, [cardsState])
|
}, [cardsState])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -115,7 +114,7 @@ const CardsField: React.FC<CardsFieldProps> = ({cards, isGameStarted = false, fi
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setCardsState(prev => (prev.map(el => ({...el, disabled: !isGameStarted, toggled: !isGameStarted && isNewGame}))));
|
setCardsState(prev => (prev.map(el => ({...el, disabled: !isGameStarted, toggled: !isGameStarted && isNewGame}))));
|
||||||
if (isGameStarted) localStorage.setItem('match-game:cards', JSON.stringify(cardsState));
|
if (isGameStarted) localStorage.setItem('match-game:cards', JSON.stringify(cardsState));
|
||||||
}, [isGameStarted, isNewGame])
|
}, [isGameStarted])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (isNewGame) setCardsState(cards.map(el => createCard(el)))
|
if (isNewGame) setCardsState(cards.map(el => createCard(el)))
|
||||||
|
|||||||
@ -1,12 +1,11 @@
|
|||||||
import CardsField from 'components/CardsField/CardsField';
|
import React, { useEffect, useState } from 'react';
|
||||||
import FinishedField from 'components/FinishedField/FinishedField';
|
|
||||||
|
|
||||||
import BottomPanel from 'components/UI/BottomPanel/BottomPanel';
|
import BottomPanel from 'components/UI/BottomPanel/BottomPanel';
|
||||||
import Button from 'components/UI/Button/Button';
|
import Button from 'components/UI/Button/Button';
|
||||||
|
import CardsField from 'components/CardsField/CardsField';
|
||||||
|
import FinishedField from 'components/FinishedField/FinishedField';
|
||||||
import Timer from 'components/UI/Timer/Timer';
|
import Timer from 'components/UI/Timer/Timer';
|
||||||
import { useMatchGame } from 'context/MatchGameContext';
|
|
||||||
import React, { useEffect, useState } from 'react';
|
|
||||||
import { createCards, randomizeElementInArray } from 'src/utils';
|
import { createCards, randomizeElementInArray } from 'src/utils';
|
||||||
|
import { useMatchGame } from 'context/MatchGameContext';
|
||||||
|
|
||||||
interface MatchGameProps {
|
interface MatchGameProps {
|
||||||
isNewGame: boolean;
|
isNewGame: boolean;
|
||||||
@ -36,7 +35,7 @@ const MatchGame: React.FC<MatchGameProps> = ({isNewGame}) => {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (isNewGame) resetGameProgress?.();
|
if (isNewGame) resetGameProgress?.();
|
||||||
}, [isNewGame, resetGameProgress])
|
}, [isNewGame])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (isGameFinished) {
|
if (isGameFinished) {
|
||||||
|
|||||||
@ -0,0 +1,21 @@
|
|||||||
|
.MatchMenu {
|
||||||
|
color: #ffffff;
|
||||||
|
font-size: 40px;
|
||||||
|
|
||||||
|
.button {
|
||||||
|
width: 240px;
|
||||||
|
}
|
||||||
|
|
||||||
|
ul {
|
||||||
|
list-style: none;
|
||||||
|
margin: 0;
|
||||||
|
li {
|
||||||
|
text-align: center;
|
||||||
|
cursor: pointer;
|
||||||
|
|
||||||
|
&:focus, &:hover {
|
||||||
|
color: coral;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,13 +1,28 @@
|
|||||||
|
import Button from 'components/UI/Button/Button';
|
||||||
import { MenuItem, useMatchGame } from 'context/MatchGameContext';
|
import { MenuItem, useMatchGame } from 'context/MatchGameContext';
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
|
||||||
|
import cls from './MatchMenu.module.scss'
|
||||||
|
|
||||||
type GameMenuProps = {
|
type GameMenuProps = {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
type MenuItemType = {
|
||||||
|
itemName: MenuItem;
|
||||||
|
itemPlaceholder: string;
|
||||||
|
condition?: () => boolean;
|
||||||
|
}
|
||||||
|
|
||||||
const MatchMenu: React.FC<GameMenuProps> = () => {
|
const MatchMenu: React.FC<GameMenuProps> = () => {
|
||||||
const {setCurrentMenuItem} = useMatchGame();
|
const {setCurrentMenuItem} = useMatchGame();
|
||||||
|
|
||||||
|
const menuItems: MenuItemType[] = [
|
||||||
|
{itemName: 'continue-game', itemPlaceholder: 'Continue', condition: canContinueGame},
|
||||||
|
{itemName: 'new-game', itemPlaceholder: 'New game'},
|
||||||
|
{itemName: 'options', itemPlaceholder: 'Options'},
|
||||||
|
{itemName: 'exit', itemPlaceholder: 'Exit'},
|
||||||
|
]
|
||||||
|
|
||||||
function clickItemHandler(item: MenuItem) {
|
function clickItemHandler(item: MenuItem) {
|
||||||
setCurrentMenuItem?.(item);
|
setCurrentMenuItem?.(item);
|
||||||
}
|
}
|
||||||
@ -16,16 +31,23 @@ const MatchMenu: React.FC<GameMenuProps> = () => {
|
|||||||
return !!localStorage.getItem('match-game:cards');
|
return !!localStorage.getItem('match-game:cards');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function renderItem(itemName: MenuItem, itemText: string) {
|
||||||
|
return <Button className={cls.button} onClick={() => clickItemHandler(itemName)}>{itemText}</Button>
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderMenuItems() {
|
||||||
|
return menuItems.map(item => {
|
||||||
|
return item?.condition
|
||||||
|
? item?.condition() && <li key={item.itemName}>{renderItem(item.itemName, item.itemPlaceholder)}</li>
|
||||||
|
: <li key={item.itemName}>{renderItem(item.itemName, item.itemPlaceholder)}</li>;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div className={cls.MatchMenu}>
|
||||||
<h2>Match game menu</h2>
|
<h2>Match game menu</h2>
|
||||||
<ul>
|
<ul>
|
||||||
{
|
{renderMenuItems()}
|
||||||
canContinueGame() ? <li onClick={() => clickItemHandler('continue-game')}>Continue</li> : null
|
|
||||||
}
|
|
||||||
<li onClick={() => clickItemHandler('new-game')}>New game</li>
|
|
||||||
<li onClick={() => clickItemHandler('options')}>Options</li>
|
|
||||||
<li onClick={() => clickItemHandler('exit')}>Exit</li>
|
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user