Pomodoro Clock Works Great, But

Hi : )

I have finished the programming of my clock and it seems to work the same as the example provided by Peter Weinberg:

https://gabehaus.github.io/Pomodoro1a/

I am still failing some tests so have not styled my clock yet. The main failure seems to be:

8. I can see an element with corresponding id=“time-left”. NOTE: Paused or running, the value in this field should always be displayed in mm:ss format (i.e. 25:00).

  • time-left is not formatted correctly: expected ‘59’ to equal ‘60’

I think the problem is related to this condition:

  } else if (seconds == 0 && minutes != 0) {
      setSeconds(59);

I’m frustrated because if you test my clock, it seems to behave the same as the clock provided by Peter Weinberg. If I change setSeconds(59) to 60, it messes up the whole program. I’m looking for a way out without having to start from scratch. This is very demoralizing.

Thanks again for your help…

Here is the code:

App.js

import React, { useState, useEffect, useRef } from "react";
import "./App.css";

function App() {
  const [seconds, setSeconds] = useState(0);

  // active set to true means clock is running
  const [active, setActive] = useState(false);
  const [sessionLength, setSessionLength] = useState(25);
  const [reduceSession, setReduceSession] = useState(false);
  const [increaseSession, setIncreaseSession] = useState(false);

  const [breakLength, setBreakLength] = useState(5);
  const [reduceBreak, setReduceBreak] = useState(false);
  const [increaseBreak, setIncreaseBreak] = useState(false);

  const [minutes, setMinutes] = useState(sessionLength);

  // blockType can be set to "Session" or "Break" and is displayed by id="timer-label"
  const [blockType, setBlockType] = useState("Session");

  //ref referring to audio element
  let qRef = useRef();

  //initial call of setInterval
  useEffect(() => {
    var myVar = null;
    if (active) {
      myVar = setInterval(myTimer, 1000);
    } else if (active == false) {
      clearInterval(myVar);
    }
    return () => clearInterval(myVar);
  });

  //condition that audio plays when clock hits 00:00
  useEffect(() => {
    if (minutes == 0 && seconds == 0) {
      qRef.current.play();
      qRef.current.currentTime = 0;
    }
  });

  //function called by setInterval - Problem here in that I set seconds to 59???
  function myTimer() {
    if (seconds > 0) {
      setSeconds(seconds => seconds - 1);
    } else if (seconds == 0 && minutes != 0) {
      setSeconds(59);
      setMinutes(minutes => minutes - 1);
    } else if (minutes == 0 && seconds == 0 && blockType == "Session") {
      setMinutes(breakLength);
      setSeconds(0);
      setBlockType("Break");
    } else if (minutes == 0 && seconds == 0 && blockType == "Break") {
      setMinutes(sessionLength);
      setBlockType("Session");
    }
  }

  //function called onClick of reset button
  function resetting() {
    setActive(false);
    setSeconds(0);

    setSessionLength(25);
    setMinutes(25);
    setBreakLength(5);
  }

  //conditions for dealing with the click of buttons that change session and break times
  useEffect(() => {
    if (
      reduceSession &&
      blockType == "Session" &&
      active == false &&
      sessionLength > 1
    ) {
      setSessionLength(sessionLength => sessionLength - 1);
      setMinutes(sessionLength - 1);
      setReduceSession(false);
      setSeconds(0);
    } else if (
      reduceSession &&
      blockType == "Session" &&
      active == true &&
      sessionLength > 1
    ) {
      setReduceSession(false);
    } else if (reduceSession && blockType == "Break" && sessionLength > 1) {
      setSessionLength(sessionLength => sessionLength - 1);
      setReduceSession(false);
    } else if (reduceSession && sessionLength <= 1) {
      setReduceSession(false);
    }

    if (
      increaseSession &&
      blockType == "Session" &&
      active == false &&
      sessionLength < 60
    ) {
      setSessionLength(sessionLength => sessionLength + 1);
      setMinutes(sessionLength + 1);
      setSeconds(0);
      setIncreaseSession(false);
    } else if (
      increaseSession &&
      blockType == "Session" &&
      active == true &&
      sessionLength < 60
    ) {
      setIncreaseSession(false);
    } else if (increaseSession && blockType == "Break" && sessionLength < 60) {
      setSessionLength(sessionLength => sessionLength + 1);
      setIncreaseSession(false);
    } else if (increaseSession && sessionLength >= 60) {
      setIncreaseSession(false);
    }
  });

  useEffect(() => {
    if (
      reduceBreak &&
      blockType == "Break" &&
      active == true &&
      breakLength > 1
    ) {
      setReduceBreak(false);
    } else if (
      reduceBreak &&
      blockType == "Break" &&
      active == false &&
      breakLength > 1
    ) {
      setBreakLength(breakLength => breakLength - 1);
      setMinutes(minutes => minutes - 1);
      setSeconds(0);
      setReduceBreak(false);
    } else if (reduceBreak && blockType == "Session" && breakLength > 1) {
      setBreakLength(breakLength => breakLength - 1);
      setReduceBreak(false);
    } else if (reduceBreak && breakLength <= 1) {
      setReduceBreak(false);
    }

    if (
      increaseBreak &&
      blockType == "Break" &&
      active == true &&
      breakLength < 60
    ) {
      setIncreaseBreak(false);
    } else if (
      increaseBreak &&
      blockType == "Break" &&
      active == false &&
      breakLength < 60
    ) {
      setBreakLength(breakLength => breakLength + 1);
      setMinutes(breakLength + 1);
      setSeconds(0);
      setIncreaseBreak(false);
    } else if (increaseBreak && blockType == "Session" && breakLength < 60) {
      setBreakLength(breakLength => breakLength + 1);
      setIncreaseBreak(false);
    } else if (breakLength >= 60) {
      setIncreaseBreak(false);
    }
  });

  return (
    <div className="App">
      <div id="break-label">Break Length</div>
      <button
        id="break-decrement"
        onClick={active == false ? () => setReduceBreak(true) : null}
      >
        down
      </button>
      <div id="break-length">{breakLength}</div>
      <button
        id="break-increment"
        onClick={active == false ? () => setIncreaseBreak(true) : null}
      >
        up
      </button>
      <div id="session-label">Session Length</div>
      <button
        id="session-decrement"
        onClick={active == false ? () => setReduceSession(true) : null}
      >
        down
      </button>
      <div id="session-length">{sessionLength}</div>
      <button
        id="session-increment"
        onClick={active == false ? () => setIncreaseSession(true) : null}
      >
        up
      </button>
      <span></span>
      <button
        id="start_stop"
        onClick={() => {
          setActive(active => !active);
        }}
      >
        pause
      </button>
      <button id="reset" onClick={() => resetting()}>
        reset
      </button>
      <div id="timer-label">{blockType}</div>
      <div id="time-left">
        {minutes < 10 ? `0${minutes}` : minutes}:
        {seconds < 10 ? `0${seconds}` : seconds}
      </div>
      <audio
        src="https://freecodecampassets.s3.us-east-2.amazonaws.com/Clock+Sounds/37720__still-frames__om.mp3"
        type="audio/ogg"
        className="clip"
        id="beep"
        ref={qRef}
        title="Ohm"
      ></audio>
    </div>
  );
}

export default App;

`````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````

index.html
`````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
    <link
      href="//db.onlinewebfonts.com/c/1b40984d7a10bcae004877dc48f4cc8f?family=Digital"
      rel="stylesheet"
      type="text/css"
    />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="theme-color" content="#000000" />
    <meta
      name="description"
      content="Web site created using create-react-app"
    />
    <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
    <!--
      manifest.json provides metadata used when your web app is installed on a
      user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
    -->
    <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
    <!--
      Notice the use of %PUBLIC_URL% in the tags above.
      It will be replaced with the URL of the `public` folder during the build.
      Only files inside the `public` folder can be referenced from the HTML.

      Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
      work correctly both with client-side routing and a non-root public URL.
      Learn how to configure a non-root public URL by running `npm run build`.
    -->
    <title>React App</title>
  </head>
  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
    <!--
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.

      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the <body> tag.

      To begin the development, run `npm start` or `yarn start`.
      To create a production bundle, use `npm run build` or `yarn build`.
    -->
  </body>
</html>
<script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>

`````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````

index.css
`````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen",
    "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue",
    sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

code {
  font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
    monospace;
}

#time-left {
  position: absolute;
  font-family: "Digital";
  width: 300px;
  height: 300px;
  font-size: 100px;
  margin-left: 5px;
}

#date {
  position: relative;
  font-family: "Digital";
  width: 300px;
  height: 300px;
  font-size: 100px;
}


`````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````

It’s because in your code the timer update after button click happens in two passes: first you change increaseSession to trigger useEffect (1st pass) which changes sessionLength and minutes (2nd pass).
You can’t see it because it happens very fast, but tests can catch it.

Overall, the way you use useEffect is not quite right. You should go with a regular onClick handler which updates all the necessary variables (you really don’t need useEffect to handle clicks).

Here’s an example (look at lines 124 and 286): https://codepen.io/jenovs/pen/PoqNWEx?editors=0010

Thank you again. You are really saving my sanity : )