]> git.xn--bdkaa.com Git - where-are-you.py.git/commitdiff
ask more tips
authora.soldatof <a.soldatof@gmail.com>
Sat, 18 May 2019 20:09:08 +0000 (23:09 +0300)
committera.soldatof <a.soldatof@gmail.com>
Sat, 18 May 2019 20:41:23 +0000 (23:41 +0300)
src/Game.js
src/GameController.js

index 211edf762b88eabee43843508505bbca32d1536c..6c88e81f56b8889800d451e5612d17123d16be05 100644 (file)
@@ -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 (
       <AppLayout>
@@ -31,10 +41,10 @@ export class Game extends Component {
               <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>
index 75aa5ecf80136668db12e9b54e4f62af784fccdb..67dc357d8126a7830c0076cde1305a8f36aeb6d3 100644 (file)
@@ -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