From ac6356dd1c273cbf2534412208f9aa21c43a9307 Mon Sep 17 00:00:00 2001 From: "a.soldatof" Date: Sat, 18 May 2019 23:09:08 +0300 Subject: [PATCH] ask more tips --- src/Game.js | 16 +++++++++++++--- src/GameController.js | 14 ++++++++++---- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/Game.js b/src/Game.js index 211edf7..6c88e81 100644 --- a/src/Game.js +++ b/src/Game.js @@ -9,7 +9,9 @@ export class Game extends Component { constructor(props) { super(props); this.gameController = new GameController(); - this.state = { tips: this.gameController.tips() }; + this.state = { + tips: this.gameController.tips() + }; } componentDidMount() { @@ -21,6 +23,14 @@ export class Game extends Component { }); } + askTip = () => { + this.gameController.getMoreTips().then(() => { + this.setState({ + tips: this.gameController.tips() + }) + }); + }; + render() { return ( @@ -31,10 +41,10 @@ export class Game extends Component {

What you see:

{this.state.tips.length > 0 && ( )} - diff --git a/src/GameController.js b/src/GameController.js index 75aa5ec..67dc357 100644 --- a/src/GameController.js +++ b/src/GameController.js @@ -3,20 +3,26 @@ import { api } from './api'; 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 -- 2.50.1