Skip to content

Commit

Permalink
Increment number of displayed gifs on each failed attempt. Fixes #8
Browse files Browse the repository at this point in the history
  • Loading branch information
emillumine committed Sep 22, 2020
1 parent 5806853 commit 884e4a4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
18 changes: 15 additions & 3 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class App extends React.Component {
this.state = {
gifs: [],
mysteryWord: "",
attempt: 0,
};
}

Expand All @@ -38,18 +39,29 @@ class App extends React.Component {
this.setState({
mysteryWord,
gifs,
attempt: 0,
});
};

handlePlayerAnswer = (playerAnswer) => {
const { mysteryWord, attempt } = this.state;
if (playerAnswer === mysteryWord) {
alert("you win!");
} else {
this.setState({ attempt: attempt + 1 });
alert("you lose...");
}
};

render() {
const { gifs, mysteryWord } = this.state;
const { gifs, attempt } = this.state;
return (
<div className="App">
<PlayerInput mysteryWord={mysteryWord} />
<PlayerInput handlePlayerAnswer={this.handlePlayerAnswer} />
<button type="button" onClick={this.play}>
Reset
</button>
<GifList gifs={gifs} />
<GifList gifs={gifs} length={attempt + 1} />
</div>
);
}
Expand Down
28 changes: 15 additions & 13 deletions src/components/gif_list.jsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import React from 'react'
import Gif from './gif'
import React from "react";
import Gif from "./gif";

class GifList extends React.Component {
render() {
const {gifs} = this.props;
return (
<ul>
{gifs.map(({url, id}) => <li key={id}><Gif url={url} key={id} /></li>)}
</ul>
)
}
}
const GifList = (props) => {
const { gifs, length } = props;
return (
<ul>
{gifs.slice(0, length).map(({ url, id }) => (
<li key={id}>
<Gif url={url} key={id} />
</li>
))}
</ul>
);
};

export default GifList
export default GifList;
5 changes: 2 additions & 3 deletions src/components/player_input.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ class PlayerInput extends React.Component {
};

handleSubmit = (event) => {
const { mysteryWord } = this.props;
const { handlePlayerAnswer } = this.props;
const { playerAnswer } = this.state;
const message = mysteryWord === playerAnswer ? "you win!" : "you lose...";
alert(message);
handlePlayerAnswer(playerAnswer);
event.preventDefault();
};

Expand Down

0 comments on commit 884e4a4

Please sign in to comment.