Advertisement
dergachevm

Untitled

Jun 21st, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // Timer.js
  3. export default class Timer extends Component {
  4.  
  5.     constructor(props){
  6.         // TODO: Ensure props.duration is supplied
  7.         this.state = {
  8.             duration: props.duration;
  9.         }
  10.     }
  11.  
  12.     stop(){
  13.         if (this.timer){
  14.             clearInterval(this.timer);
  15.         }
  16.     }
  17.  
  18.     start() {
  19.         // TODO: Check if timer has been already started
  20.  
  21.         this.timer = setInterval(() => {
  22.             if (this.state.duration > 0) {
  23.                 this.setState({
  24.                     duration: duration + (now - Date.now())
  25.                 });
  26.             } else {
  27.                 this.setState({duration: 0});
  28.  
  29.                 clearInterval(this.timer);
  30.  
  31.                 this.props.onTimeout();
  32.             }
  33.         }, 10);
  34.     }
  35.  
  36.     render() {
  37.         return <div className="timer">
  38.             <div className="timerInner">Time left: <span>{this.state.duration.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ".")}</span></div>
  39.             </div>
  40.         }
  41.     }
  42.  
  43. // Beer.js
  44. export default class Beer extends Component {
  45.  
  46.     loop() {
  47.         ...
  48.  
  49.         if (direction === 1 && this.state.amount === this.state.maxAmount) {
  50.             this.setState({
  51.                 direction: -1
  52.             });
  53.  
  54.             // Inform that glass has been filled
  55.             this.props.didFill();
  56.         }
  57.         else if (direction === -1) {
  58.             // Report current fill
  59.             this.props.onFillChange(this.state.amount);
  60.         }
  61.  
  62.         ...
  63.     }
  64. }
  65.  
  66. // View2a/2b
  67.  
  68. export default class View2a extends Component {
  69.  
  70.     constructor(props){
  71.         super(props);
  72.         this.state = {
  73.             duration: (Math.floor(Math.random() * (30 - 10 + 1)) + 10) * 1000, // Random timer number from 10 to 30
  74.             fill: 100
  75.         }
  76.     }
  77.  
  78.     _onTimeout() {
  79.         this.setState({
  80.             result: "Time's up!";
  81.         });
  82.     }
  83.  
  84.     _didFill(){
  85.     // this.timer is reference to Timer component
  86.     this.timer.start();
  87.     }
  88.  
  89.     _onFillChange(fill: number){
  90.         let result;
  91.         if (fill < min) {
  92.             result = "Well Done!";
  93.             this.timer.stop();
  94.         }
  95.         this.setState({
  96.             fill: fill,
  97.             result: result
  98.         });
  99.     }
  100.  
  101.     render() {
  102.         return(
  103.             <div className="viewContainerBeerScreen">
  104.             <Beer degrees={this.state.degrees} onFillChange={this._onFillChange.bind(this)} didFill={this._didFill.bind(this)}/>
  105.             <Timer duration={this.state.duration} onTimeout={this._onTimeout.bind(this)} ref={(input) => { this.timer = input;}}/>
  106.             <Range onSlideRange={this._onSlideRange.bind(this)}/>
  107.             <Button title="View2a Done" onClick={() => this.props.onClick(this.state.output)}/>
  108.             <div className="result">{this.state.result}</div>
  109.             </div>
  110.             )
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement