diff --git a/frontend/src/pages/schedule/useSchedulePage.js b/frontend/src/pages/schedule/useSchedulePage.js index 8214bc9..232e506 100644 --- a/frontend/src/pages/schedule/useSchedulePage.js +++ b/frontend/src/pages/schedule/useSchedulePage.js @@ -3,6 +3,7 @@ import {useEffect, useState} from "react"; import DateObject from 'react-date-object'; import persian from "react-date-object/calendars/persian"; import persian_en from "react-date-object/locales/persian_en"; +import {Helper} from "../../utils/Helper.js"; export default function useSchedulePage() { const { @@ -22,7 +23,7 @@ export default function useSchedulePage() { useEffect(() => { if (workshopsData == null) return - setTableWorkshopsData(workshopsData.map(item => { + let parsedWorkshopData =workshopsData.map(item => { const startDate = new DateObject({ date: new Date(item["start_date"]), calendar: persian, @@ -39,12 +40,25 @@ export default function useSchedulePage() { Starts: startDate.format("HH:mm"), Ends: endDate.format("HH:mm"), } - })) + }) + + const now = Helper.removeEverythingFromDateString(new Date().toLocaleString('fa-IR-u-nu-latn')) + let index = -1 + for (const item of parsedWorkshopData) { + const itemDate = Helper.removeEverythingFromDateString(item.date + ", " + item.Starts) + index++ + if (itemDate > now) + break + } + const notEligibleItems = parsedWorkshopData.slice(0, index) + parsedWorkshopData = parsedWorkshopData.slice(index) + parsedWorkshopData.push(...notEligibleItems) + setTableWorkshopsData(parsedWorkshopData) }, [workshopsData]) useEffect(() => { if (presentationsData == null) return - setTablePresentationsData(presentationsData.filter(element => { + let parsedPresentations = presentationsData.filter(element => { return element.name != null }).map(item => { const startDate = new DateObject({ @@ -63,9 +77,24 @@ export default function useSchedulePage() { Starts: startDate.format("HH:mm"), Ends: endDate.format("HH:mm"), } - })) + }) + + const now = Helper.removeEverythingFromDateString(new Date().toLocaleString('fa-IR-u-nu-latn')) + let index = -1 + for (const item of parsedPresentations) { + const itemDate = Helper.removeEverythingFromDateString(item.date + ", " + item.Starts + ":00") + index++ + if (itemDate > now) + break + } + const notEligibleItems = parsedPresentations.slice(0, index) + parsedPresentations = parsedPresentations.slice(index) + parsedPresentations.push(...notEligibleItems) + setTablePresentationsData(parsedPresentations) }, [presentationsData]) + + return { tableWorkshopsData, tablePresentationsData, diff --git a/frontend/src/pages/workshops/useWorkshopsPage.js b/frontend/src/pages/workshops/useWorkshopsPage.js index 19b0c3d..24092d9 100644 --- a/frontend/src/pages/workshops/useWorkshopsPage.js +++ b/frontend/src/pages/workshops/useWorkshopsPage.js @@ -1,5 +1,6 @@ import {useCallback, useEffect, useState} from 'react'; import {useAPI} from '../../providers/APIProvider/APIProvider.jsx'; +import {Helper} from "../../utils/Helper.js"; export default function useWorkshopsPage() { const { @@ -118,10 +119,10 @@ export default function useWorkshopsPage() { .filter((e) => e != null); parsedData.sort((a, b) => a.start_date > b.start_date ? 1 : -1) - const now = removeEverythingFromDateString(new Date().toLocaleString('fa-IR-u-nu-latn')) + const now = Helper.removeEverythingFromDateString(new Date().toLocaleString('fa-IR-u-nu-latn')) let index = -1 for (const item of parsedData) { - const itemDate = removeEverythingFromDateString(new Date(item.start_date).toLocaleString('fa-IR-u-nu-latn')) + const itemDate = Helper.removeEverythingFromDateString(new Date(item.start_date).toLocaleString('fa-IR-u-nu-latn')) index++ if (itemDate > now) break @@ -134,24 +135,6 @@ export default function useWorkshopsPage() { setParsedItemsList(parsedData); setFileteredItems(parsedData); - function removeEverythingFromDateString(str) { - const splitStr = str.split('/') - if (splitStr[1].length === 1) - splitStr[1] = '0' + splitStr[1] - const secondSplitStr = splitStr[2].split(",") - if (secondSplitStr[0].length === 1) - splitStr[2] = "0" + secondSplitStr.join(",") - const timeSplitStr = splitStr[2].split(",")[1].split(":")[0].trim() - if (timeSplitStr.length === 1) - splitStr[2] = splitStr[2].split(",")[0] + "0" + splitStr[2].split(",")[1].split(":")[0].trim() - str = splitStr.join('/') - return str - .replaceAll("/", '') - .replaceAll(" ", '') - .replaceAll(":", "") - .replaceAll(",", "") - } - }, [workshopsData, presentationsData, presenterData, teachersData]); const addToCart = useCallback( diff --git a/frontend/src/utils/Helper.js b/frontend/src/utils/Helper.js index 535973c..fa217f9 100644 --- a/frontend/src/utils/Helper.js +++ b/frontend/src/utils/Helper.js @@ -1,5 +1,4 @@ import {jwtDecode} from "jwt-decode"; -import ROUTES from "../providers/config-provider/ROUTES.jsx"; function convertStringToDateWithoutTimezone(dateInString) { const date = new Date(dateInString) @@ -73,6 +72,25 @@ function checkTokenValidity(token) { return token.exp > Math.floor(new Date().getTime() / 1000) } +function removeEverythingFromDateString(str) { + str = str.replaceAll(" UTC+03:30", "") + const splitStr = str.split('/') + if (splitStr[1].length === 1) + splitStr[1] = '0' + splitStr[1] + const secondSplitStr = splitStr[2].split(",") + if (secondSplitStr[0].length === 1) + splitStr[2] = "0" + secondSplitStr.join(",") + const timeSplitStr = splitStr[2].split(",")[1].split(":")[0].trim() + if (timeSplitStr.length === 1) + splitStr[2] = splitStr[2].split(",")[0] + "0" + splitStr[2].split(",")[1].split(":")[0].trim() + str = splitStr.join('/') + return str + .replaceAll("/", '') + .replaceAll(" ", '') + .replaceAll(":", "") + .replaceAll(",", "") +} + function parseJWT(token) { try { return jwtDecode(token) @@ -87,6 +105,7 @@ export const Helper = { convertDateTimeToTime, omitLongString, getToastDataFromResponse, + removeEverythingFromDateString, checkTokenValidity, parseJWT, } \ No newline at end of file