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

              
                <!-- 

Hello Camper!

Please read the README below in the JS Editor before beginning. Feel free to delete this message once you have read it. Good luck and Happy Coding! 

- The freeCodeCamp Team 

-->

<div id="root"></div>

              
            
!

CSS

              
                html, body{  background-color: #2f4f4f; margin: 0; padding: 0;}

.App {
  height: 100vh;
  width: 100%;

  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;

  background-color: #2f4f4f;
  color: #fff;
}

.App h2 {
  text-align: center;
  font-size: 4rem;
}

.alarm-clock {
  height: auto;
  width: 50vw;

  display: flex;
  justify-content: space-evenly;
  align-items: center;
  flex-direction: column;
}

.length {
  height: auto;
  width: 70%;
  display: flex;
  justify-content: space-evenly;
  align-items: center;
  flex-direction: row;
  /* 
  border: 1px solid #fff; */
}

#break-label,
#session-label {
  height: auto;
  width: inherit;
  padding: 0;
  margin: 0;

  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}

#break-label div,
#session-label div {
  height: auto;
  width: inherit;
  padding: 5px 0 10px;
  margin: 0;

  display: flex;
  justify-content: space-evenly;
  align-items: center;
  flex-direction: row;
}

#break-label h3,
#session-label h3 {
  font-size: 2rem;
  padding: 10px 0;
  margin: 0;
}

#break-label h2,
#session-label h2 {
  font-size: 1.9rem;
  padding: 2px 0;
  margin: 0;
}

#break-label svg,
#session-label svg {
  color: #fff;
  font-size: 3.4rem;
}

.timer {
  height: auto;
  width: auto;
}

.timer h1 {
  text-align: center;
  font-size: 3rem;
  padding: 10px 0;
  margin: 0;
}

.timer h2 {
  text-transform: capitalize;
  font-size: 2.5rem;
  padding: 10px 0;
  margin: 0;
}

.control {
  height: auto;
  width: 100%;
  padding: 20px 0;
  margin: 0;
  display: flex;
  justify-content: center;
  align-items: center;
}

.control svg {
  margin: 0 17px;
  font-size: 1.8rem;
  color: #fff;
}

.alarmIcon svg {
  margin: 10px 17px;
  font-size: 1.8rem;
  color: #fff;
}

.btn {
  cursor: pointer;
  border: 0;
  outline: 0;
  outline-color: 0;
  background-color: transparent;
}

.author {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  flex-direction: column;
  padding: 0;
  margin: 0;

  line-height: 20%;
}


              
            
!

JS

              
                
const { useRef, useReducer, useEffect, forwardRef} = React;
import {   FaBell,
  FaRegBellSlash,
  FaCaretUp,
  FaCaretDown,
  FaUndoAlt,
  FaVolumeUp,
  FaVolumeMute,
  FaStop,
  FaPlay, } from "https://cdn.skypack.dev/react-icons@4.1.0/fa";

const UPDATE = 'UPDATE'
const STARTPAUSE = 'START'
const CHANGEOP = 'CHANGEOP'
const RESET = 'RESET'
const INCREASE = 'INCREASE'
const DECREASE = 'DECREASE'

const AlarmIcon = (props) => {
  const timerDone = props.timerDone

  if (timerDone === 0) {
    return setTimeout(() => {
      ;<button className='btn alarmIcon'>
        <AlarmOn />
      </button>
    }, 2000)
  } else {
    return (
      <button className='btn alarmIcon'>
        <AlarmOff />
      </button>
    )
  }
}

const StartIcon = () => {
  return <FaPlay />
}

const StopIcon = () => {
  return <FaStop />
}

const IncreaseIcon = () => {
  return <FaCaretUp />
}

const DecreaseIcon = () => {
  return <FaCaretDown />
}

const ResetIcon = () => {
  return <FaUndoAlt />
}

const AlarmOn = () => {
  return <FaVolumeUp />
}

const AlarmOff = () => {
  return <FaVolumeMute />
}

const Controls = ({ idName, icon, onClick }) => {
  const iconName = { icon }
  return (
    <button className='btn' id={idName} onClick={onClick}>
      {iconName.icon === 'pause' ? (
        <StopIcon />
      ) : iconName.icon === 'start' ? (
        <StartIcon />
      ) : (
        <ResetIcon />
      )}
    </button>
  )
}

const Increase = ({ idName, onClick }) => {
  return (
    <button className='btn' id={idName} onClick={onClick}>
      <IncreaseIcon />
    </button>
  )
}

const Decrease = ({ idName, onClick }) => {
  return (
    <button id={idName} className='btn' onClick={onClick}>
      <DecreaseIcon />
    </button>
  )
}

const Clock = ({ timeLeft, clockStatus }) => {
  return (
    <section className='timer'>
      <h2 id='timer-label'>{clockStatus}</h2>
      <h1 id='time-left'>{timeLeft}</h1>
    </section>
  )
}

const Audio = forwardRef((props, ref) => {
  return <audio id='beep' ref={ref} src={props.audioScr}></audio>
})

const BreakLength = ({ breakLen, onIncrease, onDecrease }) => {
  return (
    <section id='break-label'>
      <h3>Break-Length</h3>
      <div>
        <Decrease
          idName={'break-decrement'}
          onClick={() => onDecrease('break')}
        />

        <h2 id='break-length'>{breakLen}</h2>

        <Increase
          idName={'break-increment'}
          onClick={() => onIncrease('break')}
        />
      </div>
    </section>
  )
}

const SessionLength = ({ sessionLen, onIncrease, onDecrease }) => {
  return (
    <section id='session-label'>
      <h3>Session-Length</h3>
      <div>
        <Decrease
          idName={'session-decrement'}
          onClick={() => onDecrease('session')}
        />

        <h2 id='session-length'>{sessionLen}</h2>

        <Increase
          idName={'session-increment'}
          onClick={() => onIncrease('session')}
        />
      </div>
    </section>
  )
}

const initialState = {
  displayTime: 60 * 25,
  breakLength: 5 * 60,
  sessionLength: 60 * 25,
  intervalId: '',
  start: false,
  onBreak: false,
}

function clockReducer(state = initialState, action) {
  switch (action.type) {
    case UPDATE:
      const data = action.payload
      return {
        ...state,
        displayTime: data.displayTime,
        onBreak: data.onBreak,
        intervalId: data.intervalId,
      }

    case STARTPAUSE:
      return {
        ...state,
        start: !state.start,
      }

    case RESET:
      const audioData = action.payload
      audioData.audio.pause()
      audioData.audio.currentTime = 0
      return {
        ...state,
        start: false,
        onBreak: false,
        intervalId: state.intervalId,
        displayTime: 25 * 60,
        breakLength: 5 * 60,
        sessionLength: 25 * 60,
      }

    case INCREASE:
      const incData = action.payload
      return {
        ...state,
        displayTime:
          incData.data === 'session' &&
          !state.start &&
          state.displayTime !== 3600
            ? state.sessionLength + 60
            : state.sessionLength,
        sessionLength:
          state.sessionLength === 3600
            ? 3600
            : incData.data === 'session' && !state.start
            ? state.sessionLength + 60
            : state.sessionLength,
        breakLength:
          state.breakLength === 3600
            ? 3600
            : incData.data === 'break' && !state.start
            ? state.breakLength + 60
            : state.breakLength,
      }

    case DECREASE:
      const decData = action.payload
      return {
        ...state,
        displayTime:
          decData.data === 'session' && !state.start && state.displayTime !== 60
            ? state.sessionLength - 60
            : state.sessionLength,

        sessionLength:
          state.sessionLength === 60
            ? 60
            : decData.data === 'session' && !state.start
            ? state.sessionLength - 60
            : state.sessionLength,
        breakLength:
          state.breakLength === 60
            ? 60
            : decData.data === 'break' && !state.start
            ? state.breakLength - 60
            : state.breakLength,
      }

    default:
      return initialState
  }
}

const ClockContainer = () => {
  const [state, dispatch] = useReducer(clockReducer, initialState)
  const audioRef = useRef(null)
  // audio source
  const audioScr =
    'https://raw.githubusercontent.com/freeCodeCamp/cdn/master/build/testable-projects-fcc/audio/BeepSound.wav'

  // Format display time to clock format
  function formatTime(time) {
    let minutes = Math.floor(time / 60)
    let seconds = time % 60

    return (
      (minutes < 10 ? '0' + minutes : minutes) +
      ':' +
      (seconds < 10 ? '0' + seconds : seconds)
    )
  }

  // Format session and break length to clock format
  function formatBreakNSessionTime(time) {
    let minutes = Math.floor(time / 60)

    return minutes
  }

  // start clock
  function startClock(
    displayTime,
    onBreak,
    audio,
    sessionLength,
    breakLength,
    intervalId
  ) {
    let seconds = 1
    let date = new Date().getTime()
    let nextDate = new Date().getTime() + seconds

    intervalId = setInterval(() => {
      date = new Date().getTime()
      if (date > nextDate) {
        if (displayTime <= 0 && !onBreak) {
          audio.play()
          onBreak = true
          displayTime = breakLength
        } else if (displayTime <= 0 && onBreak) {
          audio.play()
          onBreak = false
          displayTime = sessionLength
        }

        displayTime = displayTime - 1

        nextDate += seconds
        // distch to update the current values of the state
        updateTime(displayTime, onBreak, intervalId)
      }
    }, 1000)
  }

  useEffect(() => {
    if (state.start) {
      startClock(
        state.displayTime,
        state.onBreak,
        audioRef.current,
        state.sessionLength,
        state.breakLength,
        state.intervalId
      )
    }

    if (!state.start) {
      clearInterval(state.intervalId)
    }
  }, [state.start])

  function updateTime(displayTime, onBreak, intervalId) {
    dispatch({
      type: UPDATE,
      payload: {
        displayTime: displayTime,
        onBreak: onBreak,
        intervalId: intervalId,
      },
    })
  }

  function startPauseTimer() {
    dispatch({
      type: STARTPAUSE,
      payload: {
        operation: 'timer',
      },
    })
  }

  function resetTimer(audioData) {
    dispatch({
      type: RESET,
      payload: {
        operation: 'timer',
        audio: audioData,
      },
    })
  }

  function increaseTimer(data) {
    dispatch({
      type: INCREASE,
      payload: {
        data: data,
        operation: 'increase',
      },
    })
  }

  function decreaseTimer(data) {
    dispatch({
      type: DECREASE,
      payload: {
        data: data,
        operation: 'decrease',
      },
    })
  }

  return (
    <section className='alarm-clock'>
      <div className='length'>
        <BreakLength
          breakLen={formatBreakNSessionTime(state.breakLength)}
          onDecrease={decreaseTimer}
          onIncrease={increaseTimer}
        />
        <SessionLength
          sessionLen={formatBreakNSessionTime(state.sessionLength)}
          onIncrease={increaseTimer}
          onDecrease={decreaseTimer}
        />
      </div>
      <div>
        <Clock
          timeLeft={formatTime(state.displayTime)}
          clockStatus={state.onBreak ? 'Break' : 'Session'}
        />
      </div>
      <div className='control'>
        {state.start === false ? (
          <Controls
            idName={'start_stop'}
            icon={'start'}
            onClick={startPauseTimer}
          />
        ) : (
          <Controls
            idName={'start_stop'}
            icon={'pause'}
            onClick={startPauseTimer}
          />
        )}
        <Controls
          idName={'reset'}
          icon={'reset'}
          onClick={() => resetTimer(audioRef.current)}
        />
      </div>

      <Audio ref={audioRef} audioScr={audioScr} />

    </section>
  )
}

function App() {
  return (
    <main className='App'>
      <h2>25 + 5 Clock</h2>
      <ClockContainer />
      <section className='author'>
        <h4>Designed and Coded By</h4>
        <i>Lee Daniel</i>
      </section>
    </main>
  )
}

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>, document.getElementById('root')
)

              
            
!
999px

Console