diff --git a/src/components/RunTable/RunRow.jsx b/src/components/RunTable/RunRow.jsx index 8892b5c3146..634807946a6 100644 --- a/src/components/RunTable/RunRow.jsx +++ b/src/components/RunTable/RunRow.jsx @@ -11,7 +11,7 @@ const RunRow = ({ runs, run, locateActivity, runIndex, setRunIndex }) => { const heartRate = run.average_heartrate; - const runTime = formatRunTime(distance,pace); + const runTime = formatRunTime(run.moving_time); // change click color const handleClick = (e, runs, run) => { diff --git a/src/components/RunTable/index.jsx b/src/components/RunTable/index.jsx index 37c9fcfa72e..39edb075163 100644 --- a/src/components/RunTable/index.jsx +++ b/src/components/RunTable/index.jsx @@ -1,6 +1,6 @@ import React, { useState } from 'react'; import { MAIN_COLOR } from 'src/utils/const'; -import { sortDateFunc, sortDateFuncReverse } from 'src/utils/utils'; +import { sortDateFunc, sortDateFuncReverse, convertMovingTime2Sec } from 'src/utils/utils'; import RunRow from './RunRow'; import styles from './style.module.scss'; @@ -24,19 +24,11 @@ const RunTable = ({ ? a.average_heartrate - b.average_heartrate : b.average_heartrate - a.average_heartrate; const sortRunTimeFunc = (a, b) => { - if (Number.isNaN(a.distance) || Number.isNaN(b.distance) - || Number.isNaN(a.average_speed) || Number.isNaN(b.average_speed)) { - return 0; - } - const aDistance = (a.distance / 1000.0).toFixed(1); - const bDistance = (b.distance / 1000.0).toFixed(1); - const aPace = (1000.0 / 60.0) * (1.0 / a.average_speed); - const bPace = (1000.0 / 60.0) * (1.0 / b.average_speed); - if (sortFuncInfo === 'Time') { - return aDistance * aPace - bDistance * bPace; - } else { - return bDistance * bPace - aDistance * aPace; - } + const aTotalSeconds = convertMovingTime2Sec(a.moving_time) + const bTotalSeconds = convertMovingTime2Sec(b.moving_time) + return sortFuncInfo === 'Time' + ? aTotalSeconds - bTotalSeconds + : bTotalSeconds - aTotalSeconds }; const sortDateFuncClick = sortFuncInfo === 'Date' ? sortDateFunc : sortDateFuncReverse; diff --git a/src/utils/utils.js b/src/utils/utils.js index 346738af4e1..b0b695a1ad8 100644 --- a/src/utils/utils.js +++ b/src/utils/utils.js @@ -28,14 +28,24 @@ const formatPace = (d) => { return `${minutes}'${seconds.toFixed(0).toString().padStart(2, '0')}"`; }; -const formatRunTime = (distance,pace) => { - if (Number.isNaN(distance) || Number.isNaN(pace)) { - return '0min'; - } - const formatPace = (1000.0 / 60.0) * (1.0 / pace); - const minutes = Math.floor(formatPace * distance); +const convertMovingTime2Sec = (moving_time) => { + if (!moving_time) { + return 0; + } + // moving_time : '2 days, 12:34:56' or '12:34:56'; + const splits = moving_time.split(', '); + const days = splits.length == 2 ? parseInt(splits[0]) : 0; + const time = splits.splice(-1)[0] + const [hours, minutes, seconds] = time.split(':').map(Number); + const totalSeconds = (((days * 24) + hours) * 60 + minutes) * 60 + seconds; + return totalSeconds; +} + +const formatRunTime = (moving_time) => { + const totalSeconds = convertMovingTime2Sec(moving_time) + const seconds = totalSeconds % 60 + const minutes = (totalSeconds-seconds) / 60 if (minutes === 0) { - const seconds = Math.floor((formatPace * distance - minutes) * 60.0); return seconds + 's'; } return minutes + 'min'; @@ -241,4 +251,5 @@ export { sortDateFuncReverse, getBoundsForGeoData, formatRunTime, + convertMovingTime2Sec, };