Pen Settings

HTML

CSS

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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id='root'>
</div>

              
            
!

CSS

              
                #timerBox {
  border: 2px solid black;
  height: 450px;
  margin-top: 50px;
  margin-bottom: 100px;
  
}


#clockTab {
  border: 2px solid black;
  height: 100px;
  margin-top: 50px;
  text-align: center;
}

.controls {
  border: 2px solid black;
  height: 125px;
  text-align: center;
  margin-top: 25px;
}

h1 {
  text-align: center;
  margin-top: 25px;
}
              
            
!

JS

              
                


class App extends React.Component {
  constructor(props){
    super(props)
    this.sessionRef = React.createRef();
    this.breakRef = React.createRef();
    this.state = {
      timer: 1500,
      sessionLength: 1500,
      breakLength: 300,
      sessionRunning: false,
      startStop: 'Start',
      label: 'Session',
      breakRunning: false,
      ticker: false,
      starter: false,
      remove: false
    }
    this.breakIncrement = this.breakIncrement.bind(this);
    this.breakDecrement = this.breakDecrement.bind(this);
    this.sessionIncrement = this.sessionIncrement.bind(this);
    this.sessionDecrement = this.sessionDecrement.bind(this);
    this.handleStart = this.handleStart.bind(this);
    this.handleReset = this.handleReset.bind(this);
    
  }
  

 handleStart(){
  
   if(this.state.sessionRunning !== true && this.state.ticker !== true){
     console.log(this.sessionRef.current + 'start sessionRef');

      this.sessionRef.current = setInterval(()=>{
          this.setState((prev)=>{
            return {
              timer: prev.timer - 1, 
              sessionLength: this.state.sessionLength, 
              sessionRunning: true, 
              startStop: 'Stop',
              starter: true,
             
            }
            })
            }, 1000) 
   } else if (this.state.breakRunning === true){
     console.log(this.breakRef.current + 'pause breakRef')
     clearInterval(this.breakRef.current);
     this.setState({
       startStop: 'Start',
       breakRunning: false
     })
     
   } else if (this.state.ticker === true && this.state.breakRunning !== true){
     console.log(this.breakRef.current + 'continue breakRef')
     this.breakRef.current = setInterval(()=>{
          this.setState((prev)=>{
           return { timer: prev.timer - 1,
                    breakRunning: true,
                   
                  }})
     },1000)
   }  else {
        console.log(this.sessionRef.current + 'clear session Ref')
        clearInterval(this.sessionRef.current);
        this.setState({
          sessionRunning: false, 
          startStop: 'Start',
          }) 
        }
        }
    
    componentDidUpdate(prevProps, prevState){
      if(prevState.timer === 0 && prevState.sessionRunning == true){
        console.log('clear session and start breakRef');
        this.audioBeep.play();
        this.audioBeep.currentTime = 0
        this.setState(()=>{
          return {
          timer: this.state.breakLength,
          label: 'Break'}});
        clearInterval(this.sessionRef.current);
          this.breakRef.current = setInterval(()=>{
          this.setState((prev)=>{
           return { timer: prev.timer - 1,
                    breakRunning: true,
                    sessionRunning: false,
                    ticker: true}
          })
        }, 1000);
        
      }else if (prevState.timer === 0 && prevState.ticker == true){
             console.log(this.breakRef.current + 'BINGO')
     this.audioBeep.play();
     this.audioBeep.currentTime = 0
     clearInterval(this.breakRef.current);
     this.setState({
       breakRunning: false,
       ticker: false,
       timer: this.state.sessionLength,
       label: 'Session'
     });
        console.log(this.sessionRef.current + 'REstart sessionRef');
      this.sessionRef.current = setInterval(()=>{
          this.setState((prev)=>{
            return {
              timer: prev.timer - 1, 
              sessionLength: prev.sessionLength, 
              sessionRunning: true, 
              startStop: 'Stop',
              }
            })
            }, 1000) 
      } else if (prevState.timer < -1){
        console.log('below zero')
        clearInterval(this.breakRef.current);
        clearInterval(this.sessionRef.current);
        
      }
    }
  
    handleReset(){
    clearInterval(this.sessionRef.current);
    clearInterval(this.breakRef.current);
      this.audioBeep.pause()
      this.audioBeep.currentTime = 0
    this.setState({
      timer: 1500,
      sessionLength: 1500,
      sessionRunning: false, 
      startStop: 'Start',
      label: 'Session',
      breakLength: 300,
      ticker: false,
      breakRunning: false
    })
  }
  
  breakIncrement(){
  if(this.state.breakLength === 3600){
    this.setState({
      breakLength: 3600
    })
  } else{
    this.setState((prev)=>{
      return {breakLength: prev.breakLength + 60}
    })
      }
     }
      
  
  breakDecrement(){
    if(this.state.breakLength == 60){
      this.setState({
        breakLength: 60
      })
    } else {
    this.setState((prev)=>{
      return {breakLength: prev.breakLength - 60}
    })
      }
       }
  
    sessionIncrement(){
   if(this.state.sessionLength === 3600){
     this.setState({
       sessionLength: 3600,
       timer: 3600
     })
   } else {
    this.setState((prev)=>{
      return {sessionLength: prev.sessionLength + 60, 
              timer: prev.sessionLength + 60
             }
        })
      }
    
      }
      
  sessionDecrement(){
    if(this.state.sessionLength == 60){
      this.setState({
        sessionLength: 60,
        timer: 60
      })
    } else {
    this.setState((prev)=>{
      return {sessionLength: prev.sessionLength - 60, 
              timer: prev.sessionLength - 60}
    })
      }
    
       }
  
  
  render(){
    
    return (
    <div id='container' className='d-flex justify-content-center'>
        <div id='timerBox' className='col-5'>
          <h1>25 + 5 Timer</h1>
          <Control 
            sessionLength={this.state.sessionLength}
            breakLength={this.state.breakLength}
            breakIncrement={this.breakIncrement}
            breakDecrement={this.breakDecrement}
            sessionIncrement={this.sessionIncrement}
            sessionDecrement={this.sessionDecrement}
            timer={this.state.timer}
            remove={this.state.remove}
            />
          <Clock 
            timer={this.state.timer}
            label={this.state.label}
            />
          <StartStop
            handleStart={this.handleStart}
            handleReset={this.handleReset}
            startStop={this.state.startStop} />
          <audio
          id="beep"
          preload="auto"
          ref={(audio) => {
            this.audioBeep = audio;
          }}
          src="https://raw.githubusercontent.com/freeCodeCamp/cdn/master/build/testable-projects-fcc/audio/BeepSound.wav"
        />
        </div>
    </div>
          
    )
  }
}


class Clock extends React.Component {
  constructor(props){
    super(props)
    this.clockify = this.clockify.bind(this);
    
  }
 clockify(val){
   let minutes = Math.floor(val / 60);
   let seconds = val % 60;
   if(seconds < 10 && minutes >= 10){
     return minutes + ':' + '0' + seconds
   } else if (seconds >= 10 && minutes >= 10){
   return minutes + ':' + seconds
 } else if (minutes < 10 && seconds >= 10){
   return '0' + minutes + ':' + seconds
 } else {
   return '0' + minutes + ':' + '0' + seconds
 }
 }
  
  render(){
    return (
    <div id='clockBox' className='d-flex justify-content-center'>
        <div id='clockTab' className='col-4'>
          <h2 id='timer-label'> {this.props.label}</h2>
          <div id='time-left'>{this.clockify(this.props.timer)}</div>
        </div>
    </div>
          
    )
  }
}


class Control extends React.Component {
  constructor(props){
    super(props)
    this.clockify = this.clockify.bind(this)
  }
   clockify(val){
      let minutes = Math.floor(val / 60);
      let seconds = val % 60;
      if(seconds < 10){
          return minutes 
          } else {
            return minutes 
               }
               }

  render(){
    return (
    <div id='controlBox' className='d-flex justify-content-center'>
        <div class='col-4 controls'>
          <h5 id='break-label'>Break Length</h5>
          <div>
          <button id='break-increment' onClick={this.props.breakIncrement}>+</button>
          <span id='break-length' className='p-2'>{this.clockify(this.props.breakLength)}</span>
          <button id='break-decrement' onClick={this.props.breakDecrement}>-</button>
          </div>
        </div>
        <div class='col-4 controls'>
          <h5 id='session-label'>Session Length</h5>
          <div>
          <button id='session-increment' onClick={this.props.sessionIncrement}>+</button>
          <span id='session-length' className='p-2'>{this.clockify(this.props.sessionLength)}</span>
          <button id='session-decrement' onClick={this.props.sessionDecrement} >-</button>
          </div>
        </div>
    </div>
          
    )
  }
}


class StartStop extends React.Component {
  constructor(props){
    super(props)
  }
  render(){
    return (
    <div className='d-flex justify-content-center p-2'>
        <button id='start_stop' className='' onClick={this.props.handleStart}>{this.props.startStop}</button>
        <button id='reset' className='' onClick={this.props.handleReset}>Reset</button>
    </div>
    )
  }
}

      
      
ReactDOM.render(<App />, document.getElementById('root'))
              
            
!
999px

Console