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

              
                #root
              
            
!

CSS

              
                * {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

#root {
  min-height: 100vh;
  width: 100%;
  background: #dcdde1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

#clock {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  column-gap: 20px;
  background: #fff;
  max-width: 500px;
  padding: 20px;
}

.setup .btns {
  display: grid;
  grid-template-columns: 1fr 1fr;
}

#timer .btns {
  display: grid;
  grid-template-rows: 1fr 1fr;
}

.btns button {
  padding: 10px;
  font-size: 1.2em;
}

.label, .length {
  text-align: center;
}

#timer-label p {
  font-size: 0.85rem;
}

.label {
  font-size: 1.4em;
  padding: 10px 20px;
}

.length {
  font-size: 1.8em;
  padding: 10px 25px;
}

@media (max-width: 400px) {
  #root {
    column-gap: 7px;
  }
  .label {
    font-size: 1em;
    padding: 7px 10px;
  }
  .length {
    font-size: 1.3em;
    padding: 7px 14px;
  }
  .btns button {
    font-size: 0.8em;
    padding: 6px;
  }
}
              
            
!

JS

              
                // Const
// there is bug on session and break when timer running
// Break
const BREAK_DECREMENT = "BREAK DECREMENT"
const BREAK_INCREMENT = "BREAK INCREMENT"
const BREAK_RESET = "BREAK RESET"

// Session
const SESSION_DECREMENT = "SESSION DECREMENT"
const SESSION_INCREMENT = "SESSION INCREMENT"
const SESSION_RESET = "SESSION RESET"


// Timer
const SWITCH_TIMER_STATE = "SWITCH TIMER STATE"
const SWITCH_TIMER_MODE = "SWITCH TIMER MODE"
const SET_TIMER = "SET TIMER"
const RESET_TIMER = "RESET TIMER"
const UPDATE_TIMER = "UPDATE TIMER"
const SWITCH_UPDATE_STATE = "SWITCH_UPDATE_STATE"
const TIMER_MODE = {
  BREAK: "Break",
  SESSION: "Session",
}
const TIMER_STATE = {
  RUN: "run",
  PAUSE: "pause",
}


// Redux
// Break
function breakDecrement() {
  return {
    type: BREAK_DECREMENT,}
}

function breakIncrement() {
  return {
    type: BREAK_INCREMENT,}
}

function breakReset() {
  return {
    type: BREAK_RESET,
  }
}

const breakReducer = (state={break: 5}, action) => {
  switch (action.type) {
    case BREAK_DECREMENT:
      return {
        break: state.break - 1 }
    case BREAK_INCREMENT:
      return {
        break: state.break + 1 }
    case BREAK_RESET:
      return {break: 5,}
    default:
      return state
  }
}

function Break(props) {
  const cantSetTimer = props.timerState === TIMER_STATE.RUN || props.break === 1 || props.break === 60
  const onDecrementClick = () => {
    if (cantSetTimer) {return}
    props.breakDecrease()
    props.setTimer(props.break - 1)
  }

  const onIncrementClick = () => {
    if (cantSetTimer) {return}
    props.breakIncrease()
    props.setTimer(props.break + 1)
  }

  return (
    <div id="break" className="setup">
      <p id="break-label" className="label">Break</p>
      <p id="break-length" className="length">{props.break}</p>
      <div className="btns">
        <button 
          className="decrement"
          id="break-decrement" 
          onClick={onDecrementClick}>-</button>
        <button
          className="increment"
          id="break-increment"
          onClick={onIncrementClick}>+</button>
      </div>
    </div>)
}

const breakMapStateWithProps = state => {
  return {
    break: state.break.break,
    timerMode: state.timer.timerMode,
    timerState: state.timer.timerState,
  }
}

const breakMapDispatchWithProps = dispatch => {
  return {
    breakDecrease: () => dispatch(breakDecrement()),
    breakIncrease: () => dispatch(breakIncrement()),
    setTimer: (timerLeft) => dispatch(setTimer(timerLeft)),
  }
}

const BreakWrapper = ReactRedux.connect(breakMapStateWithProps, breakMapDispatchWithProps)(Break)



// Session

function sessionDecrement() {
  return {
    type: SESSION_DECREMENT,}
}

function sessionIncrement() {
  return {
    type: SESSION_INCREMENT,}
}

function sessionReset() {
  return {
    type: SESSION_RESET,
  }
}

const sessionReducer = (state={session: 25}, action) => {
  switch (action.type) {
    case SESSION_DECREMENT:
      return {
        session: state.session - 1}
    case SESSION_INCREMENT:
      return {
        session: state.session + 1 }
    case SESSION_RESET:
      return {session: 25,}
    default:
      return state
  }
}

function Session(props) {
  const cantSetTimer = props.session === 1 || props.session === 60 || props.timerState === TIMER_STATE.RUN
  const onDecrementClick = () => {
    if (cantSetTimer) {return}
    props.sessionDecrease()
    props.setTimer(props.session - 1)
  }
  const onIncrementClick = () => {
    if (cantSetTimer) {return}
    props.sessionIncrease()
    props.setTimer(props.session + 1)
  }

  return (<div id="session" className="setup">
    <p id="session-label" className="label">Session</p>
    <p id="session-length" className="length">{props.session}</p>
    <div className="btns">
      <button
        className="decrement"
        id="session-decrement"
        onClick={onDecrementClick}>-</button>
      <button
        className="increment"
        id="session-increment"
        onClick={onIncrementClick}>+</button>
    </div>
  </div>)
}

const sessionMapStateWithProps = state => {
  return {
    session: state.session.session,
    timerMode: state.timer.timerMode,
    timerState: state.timer.timerState,
  }
}

const sessionMapDispatchWithProps = dispatch => {
  return {
    sessionDecrease: () => dispatch(sessionDecrement()),
    sessionIncrease: () => dispatch(sessionIncrement()),
    setTimer: (timerLeft) => dispatch(setTimer(timerLeft)),
  }
}

const SessionWrapper = ReactRedux.connect(sessionMapStateWithProps, sessionMapDispatchWithProps)(Session)



// Timer
function switchTimerState() {
  return {
    type: SWITCH_TIMER_STATE,
  }
}

function switchTimerMode() {
  return {
    type: SWITCH_TIMER_MODE,
  }
}

function updateTimer() {
  return {
    type: UPDATE_TIMER,}
}

function setTimer(timeLeft) {
  return {
    type: SET_TIMER,
    payload: timeLeft,
  }
}

function switchTimerUpdateState() {
  return {
    type: SWITCH_UPDATE_STATE,
  }
}

function resetTimer() {
  return {
    type: RESET_TIMER,}
}

const timerInitialState = {
  timeLeft: 25 * 60,
  timerState: TIMER_STATE.PAUSE,
  timerMode: TIMER_MODE.SESSION,
  isUpdating: false,
}


const timerReducer = (state=timerInitialState, action) => {
  switch (action.type) {
    case SWITCH_TIMER_STATE:
      return {
        ...state,
        timerState: state.timerState === TIMER_STATE.PAUSE ? TIMER_STATE.RUN : TIMER_STATE.PAUSE
      }
    case SWITCH_TIMER_MODE:
      return {
        ...state,
        timerMode: state.timerMode === TIMER_MODE.SESSION ? TIMER_MODE.BREAK : TIMER_MODE.SESSION
      }
    case SET_TIMER:
      return {
        ...state,
        timeLeft: action.payload * 60,
      }
    case UPDATE_TIMER:
      return {
        ...state,
        timeLeft: state.timeLeft - 1,
      }
    case SWITCH_UPDATE_STATE:
      return {
        ...state,
        isUpdating: !state.isUpdating
        }
    case RESET_TIMER:
      return timerInitialState
    default:
      return state
    }
}


function switchTimerUpdateStateAsync(isUpdating) {
  return dispatch => {
    //dispatch(switchTimerUpdateState(isUpdating))
    setTimeout(() => {
      dispatch(switchTimerUpdateState())}, 1000)
  }
}


function Timer(props) {
  const beepAudioRef = React.useRef(null)
  const getTimeDisplay = () => {
    let minute = Math.floor(props.timeLeft / 60)
    let second = props.timeLeft - minute*60
    second = second < 10 ? '0' + second : second
    minute = minute < 10 ? '0' + minute : minute
    return `${minute}:${second}`
  }
  
  const reset = () => {
    props.resetTimer()
    props.resetSession()
    props.resetBreak()
    beepAudioRef.current.pause()
    beepAudioRef.current.currentTime = 0
  }
  
  const timeLeftToSet = props.timerMode === TIMER_MODE.BREAK ? props.sessionLength : props.breakLength
  
  if (props.timerState === TIMER_STATE.RUN) {
    if (!props.isUpdating) {
      if (props.timeLeft >= 0) {
        props.switchTimerUpdateStateAsync()
      } 
    } else {
      props.updateTimer()
      props.switchTimerUpdateState()
      if (props.timeLeft === 0) {
        props.switchTimerMode()
        props.setTimer(timeLeftToSet)
        beepAudioRef.current.play()
      }
    }
  } 
  
  return (
    <div id="timer">
      <audio id="beep"
        src="https://raw.githubusercontent.com/freeCodeCamp/cdn/master/build/testable-projects-fcc/audio/BeepSound.wav" 
        ref={beepAudioRef} preload="auto" />
      <div id="timer-label" className="label">
        <p>Mode</p>
        {props.timerMode}</div>
      <div id="time-left" className="length">{getTimeDisplay()}</div>
      
      <div class="btns">
        <button 
          id="start_stop" 
          onClick={props.switchTimerState}>Start-Stop</button>
        <button id="reset" onClick={reset}>Reset</button>
      </div>
    </div>
  )
}

const timerMapStateWithProps = state => {
  return{
    timeLeft: state.timer.timeLeft,
    timerState: state.timer.timerState,
    timerMode: state.timer.timerMode,
    isUpdating: state.timer.isUpdating,
    breakLength: state.break.break,
    sessionLength: state.session.session,
  }
}

const timerMapDispatchWithProps = dispatch => {
  return {
    switchTimerState: () => dispatch(switchTimerState()),
    switchTimerMode:() => dispatch(switchTimerMode()),
    setTimer: (newVal) => dispatch(setTimer(newVal)),
    resetTimer: () => dispatch(resetTimer()),
    resetBreak: () => dispatch(breakReset()),
    resetSession: () => dispatch(sessionReset()),
    updateTimer: () => dispatch(updateTimer()),
    switchTimerUpdateState: () => dispatch(switchTimerUpdateState()),
    switchTimerUpdateStateAsync: () => dispatch(switchTimerUpdateStateAsync()),
  }
}

const TimerWrapper = ReactRedux.connect(timerMapStateWithProps, timerMapDispatchWithProps)(Timer)





// Setup redux app
const reducers = Redux.combineReducers({
  session: sessionReducer,
  break: breakReducer,
  timer: timerReducer,
})

const store = Redux.createStore(reducers, 
                                Redux.applyMiddleware(ReduxThunk.default))

// App setup
function App(props) {
  return (
    <div id="clock">
      <SessionWrapper />
      <TimerWrapper />
      <BreakWrapper />
    </div>
  )
}

const Provider = ReactRedux.Provider 
function AppWrapper() {
  return (
    <Provider store={store}>
      <App />
    </Provider>
  )
}

ReactDOM.render(<AppWrapper />, document.getElementById("root"))
              
            
!
999px

Console