Skip to content

Commit

Permalink
Merge pull request #69 from AAISS/schedules-reorder
Browse files Browse the repository at this point in the history
feat: changed schedule orders to display the closest ones first
  • Loading branch information
AlirezaYousefpourM authored Dec 8, 2023
2 parents a26e93e + 6836675 commit 79a51d5
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 25 deletions.
37 changes: 33 additions & 4 deletions frontend/src/pages/schedule/useSchedulePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -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({
Expand All @@ -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,
Expand Down
23 changes: 3 additions & 20 deletions frontend/src/pages/workshops/useWorkshopsPage.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand Down
21 changes: 20 additions & 1 deletion frontend/src/utils/Helper.js
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -87,6 +105,7 @@ export const Helper = {
convertDateTimeToTime,
omitLongString,
getToastDataFromResponse,
removeEverythingFromDateString,
checkTokenValidity,
parseJWT,
}

0 comments on commit 79a51d5

Please sign in to comment.