constructor(props) {
super(props);
this.gameController = new GameController();
- this.state = { tips: this.gameController.tips() };
+ this.state = {
+ tips: this.gameController.tips()
+ };
}
componentDidMount() {
});
}
+ askTip = () => {
+ this.gameController.getMoreTips().then(() => {
+ this.setState({
+ tips: this.gameController.tips()
+ })
+ });
+ };
+
render() {
return (
<AppLayout>
<h2 className="Game-tips-header">What you see:</h2>
{this.state.tips.length > 0 && (
<ul className="Game-tips-list">
- {this.state.tips.map(tip => <li>{tip}</li>)}
+ {this.state.tips.map(tip => <li key={tip}>{tip}</li>)}
</ul>
)}
- <button className="Get-tips">
+ <button className="Get-tips" onClick={this.askTip}>
Хочу узнать ещё
</button>
</div>
export class GameController {
createGame() {
return api.post('/games').then(json => {
- this.gameId = json.game_id;
+ this.gameId = json.data.game_id;
});
}
loadTips() {
return api.get(`/games/${this.gameId}/tips`).then(json => {
- this.tips = json.tips;
+ this._tips = json.data.tips;
});
}
tips() {
- if(!this.tips) {
+ if(!this._tips) {
return [];
}
- return this.tips;
+ return this._tips;
+ }
+
+ getMoreTips() {
+ return api.post(`/games/${this.gameId}/ask-tip`).then(json => {
+ this._tips = this._tips.concat(json.data.tips);
+ });
}
}
\ No newline at end of file