From c08dcb822fa8175feb513e120e0ad17afb6d5b99 Mon Sep 17 00:00:00 2001 From: Bahugunajii Date: Thu, 8 Feb 2024 17:25:47 +0530 Subject: [PATCH 1/7] fixed spacing --- app/page.tsx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/page.tsx b/app/page.tsx index 9f0eb1c..86623a2 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -187,9 +187,11 @@ export default function Home() {
- Name: {data.name} +
+ Name: {data.name} +
- Stream: {data.stream} + Stream: {data.stream}
{renderButton(data)} From 1fc36ddcb7785d147cec04b12869ccaa66e698dc Mon Sep 17 00:00:00 2001 From: Bahugunajii Date: Tue, 13 Feb 2024 13:59:00 +0530 Subject: [PATCH 2/7] fetch live sessions based on time table --- api/afdb/session.ts | 17 +++++++++++++++++ app/page.tsx | 27 +++++++++++++++------------ app/types.ts | 8 ++++++++ 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/api/afdb/session.ts b/api/afdb/session.ts index 0bedac9..ba81abc 100644 --- a/api/afdb/session.ts +++ b/api/afdb/session.ts @@ -65,6 +65,23 @@ export const getSessions = async (sessionId: number) => { } }; +export const getSessionSchedule = async (sessionId: number) => { + try { + const urlWithParams = `${url}/session-schedule?session_id=${sessionId}`; + const response = await fetch(urlWithParams, getFetchConfig(bearerToken)); + + if (!response.ok) { + throw new Error(`Error in fetching Session Schedule Details: ${response.statusText}`); + } + + const data = await response.json(); + return data[0]; + } catch (error) { + console.error("Error in fetching Session Schedule Details:", error); + throw error; + } +}; + export const getGroupTypes = async (groupTypeId: number) => { try { const queryParams = new URLSearchParams({ diff --git a/app/page.tsx b/app/page.tsx index 86623a2..11c597f 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -3,9 +3,9 @@ import { useAuth } from "@/services/AuthContext"; import TopBar from "@/components/TopBar"; import BottomNavigationBar from "@/components/BottomNavigationBar"; -import { getSessions, getGroupUser, getGroupSessions, getGroupTypes, getQuizBatchData } from "@/api/afdb/session"; +import { getGroupUser, getGroupSessions, getGroupTypes, getQuizBatchData, getSessionSchedule } from "@/api/afdb/session"; import { useState, useEffect } from "react"; -import { GroupUser, GroupSession, Session, QuizSession } from "./types"; +import { GroupUser, GroupSession, QuizSession, SessionSchedule } from "./types"; import Link from "next/link"; import PrimaryButton from "@/components/Button"; import Loading from "./loading"; @@ -15,7 +15,7 @@ import { api } from "@/services/url"; export default function Home() { const { loggedIn, userId, userDbId } = useAuth(); - const [liveClasses, setLiveClasses] = useState([]); + const [liveClasses, setLiveClasses] = useState([]); const [isLoading, setIsLoading] = useState(true); const [quizzes, setQuizzes] = useState([]); const commonTextClass = "text-gray-700 text-sm md:text-base mx-6 md:mx-8"; @@ -53,9 +53,12 @@ export default function Home() { const flattenedGroupSessions = groupSessions.flat(); const sessionsData = await Promise.all(flattenedGroupSessions.map(async (groupSession: GroupSession) => { - const sessionData = await getSessions(groupSession.session_id); - const isActive = sessionData.is_active; - const repeatSchedule = sessionData.repeat_schedule; + const sessionData = await getSessionSchedule(groupSession.session_id); + if (!sessionData) { + return null; + } + const isActive = sessionData.session.is_active; + const repeatSchedule = sessionData.session.repeat_schedule; if (isActive && repeatSchedule && repeatSchedule.type === 'weekly' && repeatSchedule.params.includes(currentDay)) { return sessionData; @@ -65,7 +68,7 @@ export default function Home() { const filteredSessions = sessionsData.filter(session => session !== null); - const liveClassesData = filteredSessions.filter((session: Session) => session.platform === 'meet'); + const liveClassesData = filteredSessions.filter((sessionSchedule: SessionSchedule) => sessionSchedule.session.platform === 'meet'); setLiveClasses(liveClassesData); } catch (error) { console.error("Error fetching user sessions:", error); @@ -146,21 +149,21 @@ export default function Home() {

- {formatSessionTime(data.start_time)} + {data.start_time}

- {formatSessionTime(data.end_time)} + {data.end_time}

- Subject: {data.meta_data.subject ?? "Science"} + Subject: {data.session.meta_data.subject ?? "Science"}
- Batch: {data.meta_data.batch ?? "Master Batch"} + Batch: {data.session.meta_data.batch ?? "Science Batch"}
- {renderButton(data)} + {renderButton(data.session)}
))} diff --git a/app/types.ts b/app/types.ts index 198f8e1..29b0059 100644 --- a/app/types.ts +++ b/app/types.ts @@ -140,3 +140,11 @@ export interface QuizSession { stream: string, id: string } + +export interface SessionSchedule { + id: number, + session_id: number, + start_time: string, + end_time: string, + session: Session +} From 5e0e6ccfafb22002d10c72f3073f29122eeb428b Mon Sep 17 00:00:00 2001 From: Bahugunajii Date: Tue, 13 Feb 2024 17:05:30 +0530 Subject: [PATCH 3/7] filter session schedules by batch id --- api/afdb/session.ts | 7 +++++-- app/page.tsx | 12 ++++++++---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/api/afdb/session.ts b/api/afdb/session.ts index ba81abc..ad88e77 100644 --- a/api/afdb/session.ts +++ b/api/afdb/session.ts @@ -65,9 +65,12 @@ export const getSessions = async (sessionId: number) => { } }; -export const getSessionSchedule = async (sessionId: number) => { +export const getSessionSchedule = async (sessionId: number, batchId?: number) => { try { - const urlWithParams = `${url}/session-schedule?session_id=${sessionId}`; + const queryParams = new URLSearchParams(); + if (sessionId !== undefined) queryParams.append('session_id', sessionId.toString()); + if (batchId !== undefined) queryParams.append('batch_id', batchId.toString()); + const urlWithParams = `${url}/session-schedule?${queryParams.toString()}`; const response = await fetch(urlWithParams, getFetchConfig(bearerToken)); if (!response.ok) { diff --git a/app/page.tsx b/app/page.tsx index 11c597f..c9e2f4f 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -9,7 +9,7 @@ import { GroupUser, GroupSession, QuizSession, SessionSchedule } from "./types"; import Link from "next/link"; import PrimaryButton from "@/components/Button"; import Loading from "./loading"; -import { formatCurrentTime, formatSessionTime, formatQuizSessionTime } from "@/utils/dateUtils"; +import { formatCurrentTime, formatSessionTime, formatQuizSessionTime, formatTime } from "@/utils/dateUtils"; import { generateQuizLinks } from "@/utils/quizUtils"; import { api } from "@/services/url"; @@ -21,6 +21,7 @@ export default function Home() { const commonTextClass = "text-gray-700 text-sm md:text-base mx-6 md:mx-8"; const infoMessageClass = "flex items-center justify-center text-center h-72 mx-4 pb-40"; const portalBaseUrl = api.portal.frontend.baseUrl; + const [batchId, setBatchId] = useState(); const fetchUserSessions = async () => { try { @@ -34,6 +35,9 @@ export default function Home() { const quizIds = groupType.map((quiz: any) => quiz.child_id.parent_id) + const batchId = groupType.map((groupTypeData: any) => groupTypeData.child_id.id) + setBatchId(batchId[0]) + const groupSessionData = await Promise.all(groupTypeIds.map(async (groupId: number) => { return await getGroupSessions(groupId); })); @@ -53,7 +57,7 @@ export default function Home() { const flattenedGroupSessions = groupSessions.flat(); const sessionsData = await Promise.all(flattenedGroupSessions.map(async (groupSession: GroupSession) => { - const sessionData = await getSessionSchedule(groupSession.session_id); + const sessionData = await getSessionSchedule(groupSession.session_id, batchId); if (!sessionData) { return null; } @@ -149,10 +153,10 @@ export default function Home() {

- {data.start_time} + {formatTime(data.start_time)}

- {data.end_time} + {formatTime(data.end_time)}

From b5812943b367f44c06ded2de080de8f84761ab0c Mon Sep 17 00:00:00 2001 From: Bahugunajii Date: Tue, 13 Feb 2024 17:21:58 +0530 Subject: [PATCH 4/7] added date utils --- utils/dateUtils.ts | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/utils/dateUtils.ts b/utils/dateUtils.ts index eabc56e..b270009 100644 --- a/utils/dateUtils.ts +++ b/utils/dateUtils.ts @@ -1,11 +1,3 @@ -export function isSameDay(date1: Date, date2: Date): boolean { - return ( - date1.getDate() === date2.getDate() && - date1.getMonth() === date2.getMonth() && - date1.getFullYear() === date2.getFullYear() - ); -} - export function formatSessionTime(dateTimeStr: string) { const date = new Date(dateTimeStr); const hours = String(date.getUTCHours()).padStart(2, "0"); @@ -32,3 +24,8 @@ export function formatQuizSessionTime(dateTimeStr: string) { const formattedTime = `${String(hours24).padStart(2, "0")}:${minutes}`; return formattedTime; } + +export function formatTime(dateTimeStr: string) { + const [hours, minutes] = dateTimeStr.split(':'); + return `${hours}:${minutes}`; +} From 933d150e5af90f95f8b341163da9d53f56a4d8b5 Mon Sep 17 00:00:00 2001 From: Bahugunajii Date: Tue, 13 Feb 2024 18:01:46 +0530 Subject: [PATCH 5/7] show sessions only if they are active --- app/page.tsx | 27 +++++++++++++++++++-------- utils/dateUtils.ts | 23 ++++++++++++++++++++--- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/app/page.tsx b/app/page.tsx index c9e2f4f..8de3bc1 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -82,16 +82,18 @@ export default function Home() { function renderButton(data: any) { const currentTime = new Date(); const sessionTimeStr = formatSessionTime(data.start_time); + const sessionEndTimeStr = formatSessionTime(data.end_time); const currentTimeStr = formatCurrentTime(currentTime.toISOString()); const sessionTime = new Date(`2000-01-01T${sessionTimeStr}`); + const sessionEndTime = new Date(`2000-01-01T${sessionEndTimeStr}`); const currentTimeObj = new Date(`2000-01-01T${currentTimeStr}`); const timeDifference = (sessionTime.getTime() - currentTimeObj.getTime()) / (1000 * 60); - if (data.platform === 'meet') { - if (timeDifference <= 5) { + if (data.session && data.session.platform === 'meet') { + if (timeDifference <= 5 && sessionEndTime.getTime() > currentTimeObj.getTime()) { return ( - + JOIN @@ -106,11 +108,20 @@ export default function Home() { } } else if (data.redirectPlatform === 'quiz') { + if (timeDifference <= 5 && sessionEndTime.getTime() > currentTimeObj.getTime()) { + return ( + + START + + ); + } + } else { return ( - - START - +

+ Starts at
+ {sessionTimeStr} +

); } return null; @@ -167,7 +178,7 @@ export default function Home() { Batch: {data.session.meta_data.batch ?? "Science Batch"}
- {renderButton(data.session)} + {renderButton(data)}
))} diff --git a/utils/dateUtils.ts b/utils/dateUtils.ts index b270009..d6276aa 100644 --- a/utils/dateUtils.ts +++ b/utils/dateUtils.ts @@ -1,7 +1,24 @@ export function formatSessionTime(dateTimeStr: string) { - const date = new Date(dateTimeStr); - const hours = String(date.getUTCHours()).padStart(2, "0"); - const minutes = String(date.getUTCMinutes()).padStart(2, "0"); + let date; + if (dateTimeStr.includes('AM') || dateTimeStr.includes('PM')) { + const [time, period] = dateTimeStr.split(' '); + const [hoursStr, minutesStr] = time.split(':'); + let hours = parseInt(hoursStr); + const minutes = parseInt(minutesStr); + + if (period === 'PM' && hours < 12) { + hours += 12; + } else if (period === 'AM' && hours === 12) { + hours = 0; + } + + date = new Date(2000, 0, 1, hours, minutes); + } else { + const [hours, minutes, seconds] = dateTimeStr.split(':').map(Number); + date = new Date(2000, 0, 1, hours, minutes, seconds); + } + const hours = String(date.getHours()).padStart(2, "0"); + const minutes = String(date.getMinutes()).padStart(2, "0"); return `${hours}:${minutes}`; } From 1180d127f63007122dab7eb5b2c9a1a149465a3c Mon Sep 17 00:00:00 2001 From: Bahugunajii Date: Tue, 13 Feb 2024 18:43:18 +0530 Subject: [PATCH 6/7] show only active sessions --- app/page.tsx | 6 +++--- utils/dateUtils.ts | 8 ++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/app/page.tsx b/app/page.tsx index 8de3bc1..34f238c 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -9,7 +9,7 @@ import { GroupUser, GroupSession, QuizSession, SessionSchedule } from "./types"; import Link from "next/link"; import PrimaryButton from "@/components/Button"; import Loading from "./loading"; -import { formatCurrentTime, formatSessionTime, formatQuizSessionTime, formatTime } from "@/utils/dateUtils"; +import { formatCurrentTime, formatSessionTime, formatQuizSessionTime, formatTime, isSessionActive } from "@/utils/dateUtils"; import { generateQuizLinks } from "@/utils/quizUtils"; import { api } from "@/services/url"; @@ -160,7 +160,7 @@ export default function Home() {

Live Classes

{liveClasses.length > 0 ? (
- {liveClasses.map((data, index) => ( + {liveClasses.map((data, index) => (isSessionActive(formatSessionTime(data.end_time)) &&

@@ -192,7 +192,7 @@ export default function Home() {

Tests

{quizzes.length > 0 ? (
- {quizzes.map((data, index) => ( + {quizzes.map((data, index) => (isSessionActive(formatSessionTime(data.end_time)) &&

diff --git a/utils/dateUtils.ts b/utils/dateUtils.ts index d6276aa..7e599cd 100644 --- a/utils/dateUtils.ts +++ b/utils/dateUtils.ts @@ -46,3 +46,11 @@ export function formatTime(dateTimeStr: string) { const [hours, minutes] = dateTimeStr.split(':'); return `${hours}:${minutes}`; } + +export function isSessionActive(endTime: string): boolean { + const currentTime = new Date(); + const currentTimeStr = formatCurrentTime(currentTime.toISOString()); + const sessionEndTime = new Date(`2000-01-01T${endTime}`); + const currentTimeObj = new Date(`2000-01-01T${currentTimeStr}`); + return sessionEndTime.getTime() > currentTimeObj.getTime(); +} From 766e9af68fee54a4fd6844e48b9b396c2f7366aa Mon Sep 17 00:00:00 2001 From: Bahugunajii Date: Wed, 14 Feb 2024 14:22:36 +0530 Subject: [PATCH 7/7] added suggested changes --- app/page.tsx | 25 +++++++++++++------------ app/types.ts | 1 + 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/app/page.tsx b/app/page.tsx index 34f238c..741fb63 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -57,15 +57,15 @@ export default function Home() { const flattenedGroupSessions = groupSessions.flat(); const sessionsData = await Promise.all(flattenedGroupSessions.map(async (groupSession: GroupSession) => { - const sessionData = await getSessionSchedule(groupSession.session_id, batchId); - if (!sessionData) { + const sessionScheduleData = await getSessionSchedule(groupSession.session_id, batchId); + if (!sessionScheduleData) { return null; } - const isActive = sessionData.session.is_active; - const repeatSchedule = sessionData.session.repeat_schedule; + const isActive = sessionScheduleData.session.is_active; + const repeatSchedule = sessionScheduleData.session.repeat_schedule; if (isActive && repeatSchedule && repeatSchedule.type === 'weekly' && repeatSchedule.params.includes(currentDay)) { - return sessionData; + return sessionScheduleData; } return null; })); @@ -81,17 +81,18 @@ export default function Home() { function renderButton(data: any) { const currentTime = new Date(); - const sessionTimeStr = formatSessionTime(data.start_time); + const sessionStartTimeStr = formatSessionTime(data.start_time); const sessionEndTimeStr = formatSessionTime(data.end_time); const currentTimeStr = formatCurrentTime(currentTime.toISOString()); - const sessionTime = new Date(`2000-01-01T${sessionTimeStr}`); + const sessionTime = new Date(`2000-01-01T${sessionStartTimeStr}`); const sessionEndTime = new Date(`2000-01-01T${sessionEndTimeStr}`); const currentTimeObj = new Date(`2000-01-01T${currentTimeStr}`); - const timeDifference = (sessionTime.getTime() - currentTimeObj.getTime()) / (1000 * 60); + const minutesUntilSessionStart = (sessionTime.getTime() - currentTimeObj.getTime()) / (1000 * 60); + const hasSessionNotEnded = sessionEndTime.getTime() > currentTimeObj.getTime() if (data.session && data.session.platform === 'meet') { - if (timeDifference <= 5 && sessionEndTime.getTime() > currentTimeObj.getTime()) { + if (minutesUntilSessionStart <= 5 && hasSessionNotEnded) { return ( Starts at
- {sessionTimeStr} + {sessionStartTimeStr}

); } } else if (data.redirectPlatform === 'quiz') { - if (timeDifference <= 5 && sessionEndTime.getTime() > currentTimeObj.getTime()) { + if (minutesUntilSessionStart <= 5 && sessionEndTime.getTime() > currentTimeObj.getTime()) { return ( Starts at
- {sessionTimeStr} + {sessionStartTimeStr}

); } diff --git a/app/types.ts b/app/types.ts index 29b0059..9dec851 100644 --- a/app/types.ts +++ b/app/types.ts @@ -146,5 +146,6 @@ export interface SessionSchedule { session_id: number, start_time: string, end_time: string, + batch_id: number, session: Session }