123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              body {
  background: #effafc;
}

.container-fluid {
  margin-top: 30px;
}

h1, .label {
  font-family: 'Poppins';
  font-weight: bold; 
  margin: 10px;
  color: #404040;
}

.sub-section {
  background-color: #404040;
  display: inline-block;
  margin: 10px;
  width: 200px;
  height: 100px;
  color: #effafc;
  padding: 25px 0px 25px 0px;
}

.small-button {
  background: #404040;
  display: inline-block;
  border: 0;
  color: #effafc;
  font-size: 30px
}

.timer {
  font-family: 'Poppins';
  font-size: 35px;
  margin-top: 10px;
}

.md-button {
  background: #404040;
  font-family: 'Poppins';
  font-size: 22px;
  margin: 10px;
  width: 200px;
  height: 40px;
  color: #effafc;
  border: 0;
}

.red {
  background-color: red;
}
            
          
!
            
              function timeToString(time) {
  var minutes = Math.floor(time / 60);
  var seconds = time - minutes * 60;
  var timeString = minutes + ":" + ("0" + seconds).slice(-2);
  return timeString;
};

class App extends React.Component {
  constructor() {
    super();
    this.state = {
      restTimer: 300,
      restTimerSet: 300,
      workTimer: 1500,
      workTimerSet: 1500,
      startStopKey: Math.random()
    }
  }
  
  adjustWorkTimer(num) {
    var newWorkTimer = this.state.workTimerSet + num;
    this.setState({
      workTimer: newWorkTimer,
      workTimerSet: newWorkTimer
    });
  }
  
  adjustRestTimer(num) {
    var newRestTimer = this.state.restTimerSet + num;
    this.setState({
      restTimer: newRestTimer,
      restTimerSet: newRestTimer
    });
  }
  
  resetWorkTimer() {
    clearInterval(this.state.workCountDown);
    this.setState({
      workTimer: this.state.workTimerSet
    });
  }
  
  resetRestTimer() {
    clearInterval(this.state.restCountDown);
    this.setState({
      restTimer: this.state.restTimerSet
    });
  }
  
  resetBothTimers() {
    this.resetWorkTimer();
    this.resetRestTimer();
    this.setState({
      startStopKey: Math.random()
    })
  }
  
  countdownWorkTimer() {
    this.state.workCountDown = setInterval(() => {
      if (this.state.workTimer === 1) clearInterval(this.state.workCountDown);
      var newTime = this.state.workTimer - 1;
        this.setState({
          workTimer: newTime
        });
      if (this.state.workTimer === 0) {
        this.countdownRestTimer();
        this.resetWorkTimer();
      };
    } , 1000);
  }
  
  countdownRestTimer() {
    this.state.restCountDown = setInterval(() => {
      if (this.state.restTimer === 1) clearInterval(this.state.restCountDown);
      var newTime = this.state.restTimer - 1;
        this.setState({
          restTimer: newTime
        });
      if (this.state.restTimer === 0) {
        this.countdownWorkTimer();
        this.resetRestTimer();
      };
    } , 1000);
  }
  
  stopBothTimers() {
    clearInterval(this.state.restCountDown);
    clearInterval(this.state.workCountDown);
  }
  
  render() {
    return (
      <div className="container-fluid text-center">
        <h1>Pomodoral Clock</h1>
        <span className="label">Work Timer:</span>
        <WorkTimer time={this.state.workTimer} adjustWorkTimer={this.adjustWorkTimer.bind(this)}/>
        <br />
        <span className="label">Rest Timer: </span>
        <RestTimer time={this.state.restTimer} adjustRestTimer={this.adjustRestTimer.bind(this)}/>
        <br />
        <StartStop key={this.state.startStopKey} countdownWorkTimer={this.countdownWorkTimer.bind(this)} stopBothTimers={this.stopBothTimers.bind(this)}/>
        <br />
        <Reset resetBothTimers={this.resetBothTimers.bind(this)}/>
      </div>
    );
  }
}

class WorkTimer extends React.Component {
  addTime() {
    this.props.adjustWorkTimer(60);
  }
  
  subtractTime() {
    this.props.adjustWorkTimer(-60);
  }
  
  render() {
    return (
      <div className="sub-section">
        <button className="small-button" onClick={this.addTime.bind(this)}>+</button>
        <span className="timer">{timeToString(this.props.time)}</span>
        <button className="small-button" onClick={this.subtractTime.bind(this)}>-</button>
      </div>
    );
  }
}

class RestTimer extends React.Component {
  addTime() {
    this.props.adjustRestTimer(60);
  }
  
  subtractTime() {
    this.props.adjustRestTimer(-60);
  }
  
  render() {
    return (
      <div className="sub-section">
        <button className="small-button" onClick={this.addTime.bind(this)}>+</button>
        <span className="timer">{timeToString(this.props.time)}</span>
        <button className="small-button" onClick={this.subtractTime.bind(this)}>-</button>
      </div>
    );
  }
}

class StartStop extends React.Component {
  constructor() {
    super();
    this.state = {
      buttonLabel: "Start",
      color: ""
    }
  }
  
  startStop() {
    if (this.state.buttonLabel == "Start") {
      this.props.countdownWorkTimer();
      this.setState({
        buttonLabel: "Stop",
        color: "red"
      });
    } else {
      this.props.stopBothTimers();
      this.setState({
        buttonLabel: "Start",
        color: ""
      });
    }
  }
  
  render() {
    return (
      <button key={this.props.key} className={"md-button " + this.state.color} onClick={this.startStop.bind(this)}>{this.state.buttonLabel}</button>
    );
  }
}

class Reset extends React.Component {
  resetBothTimers() {
    this.props.resetBothTimers();
    StartStop.setState({
      buttonLabel: "Start",
      color: ""
    });
  }
  
  render() {
    return (
      <button className="md-button" onClick={this.resetBothTimers.bind(this)}>Reset</button>
    );
  }
}

React.render(<App />, document.body);
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console