diff --git a/frontend/src/main/components/Commons/CommonsOverview.js b/frontend/src/main/components/Commons/CommonsOverview.js index 973bf0b14..29317cfb7 100644 --- a/frontend/src/main/components/Commons/CommonsOverview.js +++ b/frontend/src/main/components/Commons/CommonsOverview.js @@ -2,7 +2,7 @@ import React from "react"; import { Row, Card, Col, Button } from "react-bootstrap"; import { useNavigate } from "react-router-dom"; import { hasRole } from "main/utils/currentUser"; -import { calculateDays } from "main/utils/dayUtils"; +import { calculateDays } from "main/utils/dateUtils"; export default function CommonsOverview({ commons, currentUser }) { @@ -11,8 +11,8 @@ export default function CommonsOverview({ commons, currentUser }) { const leaderboardButtonClick = () => { navigate("/leaderboard/" + commons.id) }; const showLeaderboard = (hasRole(currentUser, "ROLE_ADMIN") || commons.showLeaderboard ); - //const startingDate = commons.startingDate; - commons.day = calculateDays(commons.startingDate); + const today = new Date(); + commons.day = calculateDays(commons.startingDate,today); return ( diff --git a/frontend/src/main/utils/dateUtils.js b/frontend/src/main/utils/dateUtils.js index dfd7e693e..3ceca7a26 100644 --- a/frontend/src/main/utils/dateUtils.js +++ b/frontend/src/main/utils/dateUtils.js @@ -6,4 +6,13 @@ const timestampToDate = (timestamp) => { return (date.getFullYear() + "-" + (padWithZero(date.getMonth()+1)) + "-" + padWithZero(date.getDate())); } -export {timestampToDate, padWithZero}; \ No newline at end of file + +const calculateDays = (startingDate,currentDate) => { + const start = new Date(startingDate); + const today = new Date(currentDate); + const timeDifference = today.getTime() - start.getTime(); + const days = Math.floor(timeDifference / (1000 * 3600 * 24)); + return days; + } + +export {timestampToDate, padWithZero, calculateDays}; \ No newline at end of file diff --git a/frontend/src/main/utils/dayUtils.js b/frontend/src/main/utils/dayUtils.js deleted file mode 100644 index 3870deb52..000000000 --- a/frontend/src/main/utils/dayUtils.js +++ /dev/null @@ -1,7 +0,0 @@ -export const calculateDays = (startingDate) => { - const start = new Date(startingDate); - const today = new Date(); - const timeDifference = today.getTime() - start.getTime(); - const days = Math.floor(timeDifference / (1000 * 3600 * 24)); - return days; - }; \ No newline at end of file diff --git a/frontend/src/tests/utils/dateUtils.test.js b/frontend/src/tests/utils/dateUtils.test.js index 7b9ffd31c..27b342f8b 100644 --- a/frontend/src/tests/utils/dateUtils.test.js +++ b/frontend/src/tests/utils/dateUtils.test.js @@ -1,4 +1,4 @@ -import { padWithZero, timestampToDate } from "main/utils/dateUtils"; +import { padWithZero, timestampToDate, calculateDays} from "main/utils/dateUtils"; describe("dateUtils tests", () => { @@ -25,4 +25,13 @@ describe("dateUtils tests", () => { }); }); + describe("calculateDays tests", () => { + test("with mock current date 2023-06-01T00:00:00", () => { + const startingDate = "2023-05-01T00:00:00"; + const currentDate = "2023-06-01T00:00:00"; + expect(calculateDays(startingDate,currentDate)).toBe(31); + + }); + }); + });