generated from edenia/full-stack-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
525 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import React from 'react' | ||
import { makeStyles } from '@material-ui/styles' | ||
import PropTypes from 'prop-types' | ||
|
||
import Card from './Card' | ||
|
||
import styles from './styles' | ||
|
||
const useStyles = makeStyles(styles) | ||
|
||
const Board = ({ deck, pairSelected, selectCard }) => { | ||
const classes = useStyles() | ||
|
||
return ( | ||
<div className={classes.board}> | ||
{(deck || []).map((card, index) => { | ||
const isCardCompared = pairSelected.indexOf(card) > -1 | ||
|
||
return ( | ||
<Card | ||
key={index} | ||
image={card.image} | ||
isCardCompared={isCardCompared} | ||
selectCard={() => selectCard(card)} | ||
guessedRight={card.guessedRight} | ||
/> | ||
) | ||
})} | ||
</div> | ||
) | ||
} | ||
|
||
Board.propTypes = { | ||
deck: PropTypes.array, | ||
pairSelected: PropTypes.array, | ||
selectCard: PropTypes.func | ||
} | ||
|
||
export default Board |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React from 'react' | ||
import { makeStyles } from '@material-ui/styles' | ||
import PropTypes from 'prop-types' | ||
import ReactCardFlip from 'react-card-flip' | ||
|
||
import styles from './styles' | ||
|
||
const useStyles = makeStyles(styles) | ||
|
||
const Card = ({ image, isCardCompared, selectCard, guessedRight }) => { | ||
const classes = useStyles() | ||
|
||
return ( | ||
<div className={classes.card} onClick={selectCard}> | ||
<ReactCardFlip | ||
isFlipped={isCardCompared || guessedRight} | ||
flipDirection="vertical" | ||
> | ||
<div className={classes.cover}></div> | ||
<div className={classes.content}> | ||
<span>{image}</span> | ||
</div> | ||
</ReactCardFlip> | ||
</div> | ||
) | ||
} | ||
|
||
Card.propTypes = { | ||
image: PropTypes.any, | ||
isCardCompared: PropTypes.array, | ||
selectCard: PropTypes.func, | ||
guessedRight: PropTypes.bool | ||
} | ||
|
||
export default Card |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React from 'react' | ||
import PropTypes from 'prop-types' | ||
import { makeStyles } from '@material-ui/styles' | ||
|
||
import styles from './styles' | ||
|
||
const useStyles = makeStyles(styles) | ||
|
||
const Header = ({ attempts, resetGame }) => { | ||
const classes = useStyles() | ||
|
||
return ( | ||
<header> | ||
<div className={classes.title}>React-Memory</div> | ||
<div> | ||
<button className={classes.resetBtn} onClick={resetGame}> | ||
reset | ||
</button> | ||
</div> | ||
<div className={classes.title}>Attempts: {attempts}</div> | ||
</header> | ||
) | ||
} | ||
|
||
Header.propTypes = { | ||
attempts: PropTypes.number, | ||
resetGame: PropTypes.func | ||
} | ||
|
||
export default Header |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import React, { useState, useEffect } from 'react' | ||
import { makeStyles } from '@material-ui/styles' | ||
|
||
import { buildDeck } from '../../utils' | ||
|
||
import Header from './Header' | ||
import Board from './Board' | ||
import styles from './styles' | ||
|
||
const useStyles = makeStyles(styles) | ||
|
||
const Memory = () => { | ||
const classes = useStyles() | ||
const [deck, setDeck] = useState() | ||
const [pairSelected, setPairSelected] = useState() | ||
const [pairCompared, setPairCompared] = useState() | ||
const [attempts, setAttempts] = useState() | ||
|
||
const selectCard = card => { | ||
if (pairCompared || pairSelected.indexOf(card) > -1 || card.guessedRight) { | ||
return | ||
} | ||
|
||
const newPairSelected = [...pairSelected, card] | ||
|
||
setPairSelected(newPairSelected) | ||
|
||
if (newPairSelected.length === 2) { | ||
comparePair(newPairSelected) | ||
} | ||
} | ||
|
||
const comparePair = pair => { | ||
setPairCompared(true) | ||
|
||
setTimeout(() => { | ||
const [firstCard, secondCard] = pair | ||
let newDeck = deck | ||
|
||
if (firstCard.image === secondCard.image) { | ||
newDeck = newDeck.map(card => { | ||
if (card.image !== firstCard.image) return card | ||
|
||
return { ...card, guessedRight: true } | ||
}) | ||
} | ||
|
||
verifyGame(deck) | ||
|
||
setDeck(newDeck) | ||
setPairSelected([]) | ||
setPairCompared(false) | ||
setAttempts(attempts + 1) | ||
}, 1000) | ||
} | ||
|
||
const verifyGame = deck => { | ||
if (deck.filter(card => !card.guessedRight).length === 0) { | ||
console.log(`Win with ${attempts} attempts!`) | ||
} | ||
} | ||
|
||
const resetGame = () => { | ||
setDeck(buildDeck()) | ||
setPairSelected([]) | ||
setPairCompared(false) | ||
setAttempts(0) | ||
} | ||
|
||
useEffect(() => { | ||
resetGame() | ||
}, []) | ||
|
||
return ( | ||
<div className={classes.app}> | ||
<Header attempts={attempts} resetGame={resetGame} /> | ||
<Board deck={deck} pairSelected={pairSelected} selectCard={selectCard} /> | ||
</div> | ||
) | ||
} | ||
|
||
export default Memory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
export default () => ({ | ||
card: { | ||
width: 125, | ||
height: 125 | ||
}, | ||
cover: { | ||
width: 125, | ||
height: 125, | ||
backgroundColor: '#ffb300' | ||
}, | ||
content: { | ||
width: 125, | ||
height: 125, | ||
display: 'flex', | ||
justifyContent: 'center', | ||
alignItems: 'center', | ||
backgroundColor: 'rgb(3, 220, 244)' | ||
}, | ||
header: { | ||
height: 50, | ||
border: '1px solid black', | ||
marginBottom: 10, | ||
display: 'flex', | ||
justifyContent: 'space-between', | ||
alignContent: 'center' | ||
}, | ||
title: { | ||
fontSize: 25, | ||
padding: 10, | ||
color: '#00' | ||
}, | ||
resetBtn: { | ||
left: 12, | ||
top: 58 | ||
}, | ||
board: { | ||
display: 'flex', | ||
flexWrap: 'wrap', | ||
width: 700, | ||
height: 600, | ||
margin: '0 auto', | ||
justifyContent: 'space-around' | ||
} | ||
}) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.