From 3f1975df3b5e833f27ebad1cf385e87cb1ae4136 Mon Sep 17 00:00:00 2001 From: Jose Nunes Date: Sat, 7 Mar 2020 12:08:46 +0000 Subject: [PATCH] fix: Wrong time being displayed in the pomodoro WDY-286 --- frontend/package-lock.json | 6 +++--- frontend/src/helpers/pomodoro.js | 26 ++++++++++++++------------ 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/frontend/package-lock.json b/frontend/package-lock.json index 0fe8e32..60363f4 100644 --- a/frontend/package-lock.json +++ b/frontend/package-lock.json @@ -2390,9 +2390,9 @@ } }, "acorn": { - "version": "7.1.0", - "resolved": "https://registry.npmjs.org/acorn/-/acorn-7.1.0.tgz", - "integrity": "sha512-kL5CuoXA/dgxlBbVrflsflzQ3PAas7RYZB52NOm/6839iVYJgKMJ3cQJD+t2i5+qFa8h3MDpEOJiS64E8JLnSQ==" + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/acorn/-/acorn-7.1.1.tgz", + "integrity": "sha512-add7dgA5ppRPxCFJoAGfMDi7PIBXq1RtGo7BhbLaxwrXPOmw8gq48Y9ozT01hUKy9byMjlR20EJhu5zlkErEkg==" }, "acorn-globals": { "version": "4.3.4", diff --git a/frontend/src/helpers/pomodoro.js b/frontend/src/helpers/pomodoro.js index f3342f1..428e3a3 100644 --- a/frontend/src/helpers/pomodoro.js +++ b/frontend/src/helpers/pomodoro.js @@ -27,11 +27,11 @@ export const getCurrentPomodoroInfo = (time, settings) => { const { pomodoroLength, shortBreak } = settings; const remainingTime = timeInMinutes % (pomodoroLength + shortBreak); - const info = { inBreak: false, elapsedTime: formatTime(remainingTime) }; + const info = { inBreak: false, elapsedTime: formatTime(remainingTime * 60) }; if (remainingTime >= pomodoroLength) { info.inBreak = true; - info.elapsedTime = formatTime(remainingTime - pomodoroLength); + info.elapsedTime = formatTime((remainingTime - pomodoroLength) * 60); } return info; @@ -45,15 +45,18 @@ export const getCurrentPomodoroInfo = (time, settings) => { * getTotalTime(64 * 60) ➜ { hours: 1, minutes: 4 } * * @param {number} time - Time in secs - * @returns {{ hours: number, minutes: number }} - Object containing two integers representing - * time in hours and minutes + * @returns {{ hours: number, minutes: number, seconds: number }} - Object containing three + * integers representing time in hours, minutes and seconds */ export const getTotalTime = time => { - const hours = time / 60 / 60; + const hours = Math.floor(time / 60 / 60); + const minutes = Math.floor((time - hours * 60 * 60) / 60); + const seconds = Math.round(time - hours * 60 * 60 - minutes * 60); return { - hours: Math.floor(hours), - minutes: Math.floor((hours - Math.floor(hours)) * 60), + hours, + minutes, + seconds, }; }; @@ -72,14 +75,13 @@ export const formatTotalTime = time => { }; /** - * Converts time in secs to a formatted string (00 : 00) + * Converts time in seconds to a formatted string (00 : 00) * - * @param {number} time - Time to format (secs) + * @param {number} time - Time to format (seconds) * @returns {string} - Formatted time ➜ 00 : 00 */ export const formatTime = time => { - const mins = Math.floor(time); - const secs = Math.round((time - Math.floor(time)) * 60); + const { minutes, seconds } = getTotalTime(time); - return `${mins} : ${String(secs).padStart(2, '0')}`; + return `${minutes} : ${seconds.toString().padStart(2, '0')}`; };