diff --git a/src/app/(dashboard)/reservation/page.jsx b/src/app/(dashboard)/reservation/page.jsx index 0b82d13f3fe2d8704a69d36eda7945c42388da87..e75a1b401f35a3080e51f11db15a7e1e7d990ff4 100644 --- a/src/app/(dashboard)/reservation/page.jsx +++ b/src/app/(dashboard)/reservation/page.jsx @@ -9,6 +9,7 @@ import PresenceButton from './PresenceButton' import Cookies from 'js-cookie'; import { decrypt } from '@/app/lib/session'; import ReservationConfirmation from '@/app/ui/ReservationConfirmation' +import { dateDiffInDays, extractDate } from '@/app/lib/DateHelper' export const ReservationContext = React.createContext() @@ -174,10 +175,17 @@ const Reservation = () => { const currentMonth = new Date().getMonth() + 1 // filter dates from now to 2 weeks later + + // console.log("target date", datesData); + // const filteredDatesData = datesData?.filter((element) => { + // const date = new Date(element.date) + // const month = date.getMonth() + 1 + // return (month === currentMonth) && (date.getDate() >= new Date().getDate() && date.getDate() <= new Date().getDate() + 14) + // }) const filteredDatesData = datesData?.filter((element) => { - const date = new Date(element.date) - const month = date.getMonth() + 1 - return (month === currentMonth) && (date.getDate() >= new Date().getDate() && date.getDate() <= new Date().getDate() + 14) + const diff = dateDiffInDays(extractDate(new Date()), element.date) + return diff >= 0 && diff <= 14 + }) const selectedZoneData = floors.find((element) => element.id == selectedZone.floorId)?.zones.find((zone) => zone.id === selectedZone.zoneId) return ( diff --git a/src/app/lib/DateHelper.js b/src/app/lib/DateHelper.js index a056401f635a4d48bcc3a3baec2a57c99ec4be23..39a4aa33e49972c5663b6f8b9460fd9484bec9be 100644 --- a/src/app/lib/DateHelper.js +++ b/src/app/lib/DateHelper.js @@ -117,4 +117,28 @@ export const getWeeksBetween = (fromWeek, toWeek) => { } return weeks; -}; \ No newline at end of file +}; + +/** + * Calculates the difference in days between two dates. + * + * @param {string} date1 - The first date in 'YYYY-MM-DD' format. + * @param {string} date2 - The second date in 'YYYY-MM-DD' format. + * @returns {number} The difference in days between the two dates. + */ +export function dateDiffInDays(date1, date2) { + const dateObj1 = new Date(date1); + const dateObj2 = new Date(date2); + + dateObj1.setHours(0, 0, 0, 0); + dateObj2.setHours(0, 0, 0, 0); + + const timestamp1 = dateObj1.getTime(); + const timestamp2 = dateObj2.getTime(); + + const differenceInMillis = timestamp2 - timestamp1 + + const differenceInDays = Math.ceil(differenceInMillis / (1000 * 60 * 60 * 24)); + + return differenceInDays; +} \ No newline at end of file diff --git a/src/app/ui/SideBarLink.jsx b/src/app/ui/SideBarLink.jsx index 22e3549fcc0cc8b504f4223614a14c6bbf8e21d9..b64503e115a609c884b02eb302c80ccb7e3bc6b6 100644 --- a/src/app/ui/SideBarLink.jsx +++ b/src/app/ui/SideBarLink.jsx @@ -1,7 +1,7 @@ 'use client' import Link from 'next/link'; import { usePathname } from 'next/navigation'; -import React from 'react'; +import React, { useLayoutEffect, useState } from 'react'; const SideBarLink = ({ links, label }) => { const pathname = usePathname(); @@ -13,10 +13,11 @@ const SideBarLink = ({ links, label }) => { target.classList.replace("active", "notActive"); sideBar.classList.replace("active", "notActive"); } - console.log(target, sideBar); }; - - const isActive = links?.find((element) => element.link === pathname); + const [isActive, setActive] = useState(false) + useLayoutEffect(() => { + setActive(links?.find((element) => element.link === pathname) !== undefined) + }, [pathname]) return (