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

              
                <script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>
<div id='root'></div>
              
            
!

CSS

              
                body {
  display: flex;
  height: 100vh;
  justify-content: center;
  align-items: center;
  background-image: linear-gradient(180deg, #fab1a7, #ffe7e5);
}

.container-main {
  width: 50vw;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  border-radius: 20px;
  background-image: linear-gradient(45deg, #a5e7ff, white);
  box-shadow: 2px 2px 5px black;
}

.container-main img {
  width: 25%;
  border-radius: 20px;
  box-shadow: 2px 2px 5px black;
  margin-top: 3vw;
}

.title {
  font-size: 3.5vw;
}

.container-length {
  margin-top: -2vw;
  width: 100%;
  display: flex;
  justify-content: space-evenly;
  align-items: center;
}

.wrapper-length {
  width: 33%;
  display: flex;
  flex-direction: column;
  align-items: center;
}

#break-label {
}
#session-label {
}

.arrow {
  font-size: 2.5vw;
  outline: none;
  background: none;
  border: none;
  cursor: pointer;
}

.arrow:hover {
  transform: scale(1.05);
}

.increment:active {
  color: #0040ff;
}
.decrement:active {
  color: red;
}

#break-decrement {
}
#session-decrement {
}
#break-increment {
}
#session-increment {
}
#break-length {
  font-size: 1.5vw;
}
#session-length {
  font-size: 1.5vw;
}

.container-timer {
  margin: -4vw 0 4vw 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  border: inset;
  width: 33%;
  border-radius: 10px;
  color: black;
  background: white;
}
#timer-label {
  font-size: 3.25vw;
}
#time-left {
  font-size: 3.25vw;
  margin-top: -1vw;
}

.container-btns {
  width: 33%;
  display: flex;
  justify-content: space-evenly;
  margin-bottom: 1vw;
}

.container-btns button {
  background: none;
  border: 1px solid black;
  border-radius: 10px;
  background: #ffe7e5;
  outline: none;
}

.container-btns button:hover {
  transform: scale(1.05);
}

.container-btns button:active {
  transform: translateY(4px);
}
#start_stop {
  cursor: pointer;
  font-size: 3vw;
}
#reset {
  cursor: pointer;
  font-size: 3vw;
}
#beep {
}
              
            
!

JS

              
                
let myTimer;

class App extends React.Component {
  state = {
    breakTime: 300,
    sessionTime: 1500,
    activeTime: 1500,
    sessionOn: false,
    breakOn: false,
    isPaused: "",
  };

  handleReset = () => {
    clearInterval(myTimer);
    this.timesUpReset();
    this.setState({
      breakTime: 300,
      sessionTime: 1500,
      activeTime: 1500,
      sessionOn: false,
      breakOn: false,
      isPaused: "",
    });
  };

  countDown = () => {
    myTimer = setInterval(() => {
      this.setState({ activeTime: this.state.activeTime - 1 });
    }, 1000);
  };

  handleTimer = () => {
    const { sessionTime, sessionOn, breakOn, isPaused } = this.state;

    if (!isPaused) {
      //starting from initial state
      if (!sessionOn && !breakOn) {
        //turn session on & set timer to active state
        this.setState({
          sessionOn: true,
          activeTime: sessionTime,
          isPaused: false,
        });
        this.countDown();
      }

      //pause a session
      if (sessionOn && !isPaused) {
        this.setState({
          isPaused: true,
        });
        clearInterval(myTimer);
      }

      //pause a break
      if (breakOn) {
        this.setState({
          isPaused: true,
        });
        clearInterval(myTimer);
      }
    }

    if (isPaused) {
      this.setState({ isPaused: false });
      this.countDown();
    }
  };

  handleIncrementBreak = () => {
    if (this.state.breakTime >= 3600) return;
    this.setState({ breakTime: this.state.breakTime + 60 });
  };
  handleDecrementBreak = () => {
    if (this.state.breakTime <= 60) return;
    this.setState({ breakTime: this.state.breakTime - 60 });
  };

  handleIncrementSession = () => {
    if (this.state.sessionTime >= 3600) return;
    this.setState({ sessionTime: this.state.sessionTime + 60 });
  };
  handleDecrementSession = () => {
    if (this.state.sessionTime <= 60) return;
    this.setState({ sessionTime: this.state.sessionTime - 60 });
  };

  convertTime = (seconds) => {
    let minutes = Math.floor(seconds / 60);
    let remainderSeconds = seconds % 60;
    let display = `${minutes < 10 ? "0" + minutes : minutes}:${
      remainderSeconds < 10 ? "0" : ""
    }${remainderSeconds}`;
    return display;
  };

  timesUp = () => {
    let beep = document.querySelector("#beep");
    beep.play();
  };

  timesUpReset = () => {
    let beep = document.querySelector("#beep");
    beep.pause();
    beep.currentTime = 0;
  };

  componentWillUpdate = () => {
    if (this.state.sessionOn) {
      if (this.state.activeTime <= 0) {
        clearInterval(myTimer);
        this.setState(
          {
            sessionOn: !this.state.sessionOn,
            breakOn: !this.state.breakOn,
            activeTime: this.state.breakTime,
          },
          () => this.timesUp()
        );
        this.countDown();
      }
    } else if (this.state.breakOn) {
      if (this.state.activeTime <= 0) {
        clearInterval(myTimer);
        this.setState(
          {
            sessionOn: !this.state.sessionOn,
            breakOn: !this.state.breakOn,
            activeTime: this.state.sessionTime,
          },
          () => this.timesUp()
        );
        this.countDown();
      }
    }
  };

  //Unsure if necessary, implemented this as a means to pass one of the tests.
  componentWillUnmount = () => {
    clearInterval(myTimer);
  };

  render() {
    const {
      breakTime,
      sessionTime,
      activeTime,
      breakOn,
      sessionOn,
      isPaused,
    } = this.state;

    return (
      <div className="container-main">
        <img src="https://i0.wp.com/post.healthline.com/wp-content/uploads/2019/10/Tomato_1296x728-header-1296x728.jpg?w=1155&h=1528"></img>
        <h1 className="title">Pomodoro Clock</h1>
        <div className="container-length">
          <SetLength
            name="break"
            length={breakTime / 60}
            increment={this.handleIncrementBreak}
            decrement={this.handleDecrementBreak}
          />
          <Initializer
            reset={this.handleReset}
            timerInit={this.handleTimer}
            isPaused={isPaused}
          />
          <SetLength
            name="session"
            length={sessionTime / 60}
            increment={this.handleIncrementSession}
            decrement={this.handleDecrementSession}
          />
        </div>
        <Timer
          timeLeft={
            !sessionOn && !breakOn
              ? this.convertTime(sessionTime)
              : this.convertTime(activeTime)
          }
          sessionOn={sessionOn}
          breakOn={breakOn}
        />
      </div>
    );
  }
}

class SetLength extends React.Component {
  state = {};
  render() {
    return (
      <div className="wrapper-length">
        <h2 id={`${this.props.name}-label`}>
          {this.props.name.charAt(0).toUpperCase() +
            this.props.name.slice(1) +
            " length"}
        </h2>
        <button
          id={`${this.props.name}-increment`}
          className="arrow increment"
          onClick={() => this.props.increment(this.props.name)}
        >
          ↑
        </button>
        <p id={`${this.props.name}-length`}>{this.props.length}</p>
        <button
          id={`${this.props.name}-decrement`}
          className="arrow decrement"
          onClick={() => this.props.decrement(this.props.name)}
        >
          ↓
        </button>
      </div>
    );
  }
}

const Timer = ({ sessionOn, breakOn, timeLeft, children }) => {
  return (
    <div className="container-timer">
      <h2 id="timer-label">
        {(!sessionOn && !breakOn) || (sessionOn && !breakOn)
          ? "Session"
          : "Break"}
      </h2>
      <h1 id="time-left">{timeLeft}</h1>
      <audio
        id="beep"
        src="http://www.superluigibros.com/downloads/sounds/GAMECUBE/SUPERMARIOSUNSHINE/WAV/switchtimer.wav"
      ></audio>
      {children}
    </div>
  );
};

const Initializer = (props) => {
  return (
    <div className="container-btns">
      <button id="start_stop" onClick={props.timerInit}>
        {props.isPaused === true || props.isPaused === "" ? "▶️" : "⏸"}
      </button>
      <button id="reset" onClick={props.reset}>
        🔄
      </button>
    </div>
  );
};

ReactDOM.render(<App />, document.getElementById("root"));

              
            
!
999px

Console