Skip to content

Commit

Permalink
Fix : Showing wrong ETA for tasks (#602)
Browse files Browse the repository at this point in the history
* fix: wrong eta showing on task

* fix:wrong eta
  • Loading branch information
Achintya-Chatterjee authored Jul 11, 2024
1 parent 0b13e5f commit 417c179
Showing 1 changed file with 23 additions and 33 deletions.
56 changes: 23 additions & 33 deletions app/helpers/convertDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,57 +7,47 @@ const timeDifference = (timestamp, timeNow) => {
const mins = calc(60, timeInSec);
const hours = calc(60, mins);
const days = calc(24, hours);
const weeks = calc(7, days);
const months = calc(30, days);
const years = calc(12, months);
let result = years;
let cycle = 'year';
const months = Math.floor(days / 30);
const years = Math.floor(months / 12);

console.log({ timeInSec, mins, hours, days, months, years }); // Debug log

if (timeInSec < 1) {
return 'just now';
return { result: '', cycle: 'just now' };
}

if (years > 0) {
result = years;
cycle = 'year';
return { result: years, cycle: 'year' };
} else if (months > 0) {
result = months;
cycle = 'month';
} else if (weeks > 0) {
result = weeks;
cycle = 'week';
return { result: months, cycle: 'month' };
} else if (days > 0) {
result = days;
cycle = 'day';
return { result: days, cycle: 'day' };
} else if (hours > 0) {
result = hours;
cycle = 'hour';
return { result: hours, cycle: 'hour' };
} else if (mins > 0) {
result = mins;
cycle = 'minute';
return { result: mins, cycle: 'minute' };
} else {
result = '';
cycle = 'few seconds';
return { result: '', cycle: 'few seconds' };
}
return { result, cycle };
};

function convertDate([timestamp], { end_date, timeNow = Date.now() }) {
if (!timestamp) return 'TBD';
if (end_date == 1 && timestamp * 1000 < timeNow) {
const time_value = timeDifference(timestamp, timeNow);
return `${time_value.result} ${time_value.cycle}${
time_value.result > 1 ? 's' : ''
} ago`;
}
const time_value = timeDifference(timestamp, timeNow);
if (timestamp * 1000 < timeNow)
return `${time_value.result} ${time_value.cycle}${
time_value.result > 1 ? 's ago' : ' ago'
}`;
return `in ${time_value.result} ${time_value.cycle}${

let timeString = `${time_value.result} ${time_value.cycle}${
time_value.result > 1 ? 's' : ''
}`;

if (end_date == 1 && timestamp * 1000 < timeNow) {
return `${timeString} ago`;
}

if (timestamp * 1000 < timeNow) {
return `${timeString} ago`;
}

return `in ${timeString}`;
}

export default helper(convertDate);

0 comments on commit 417c179

Please sign in to comment.