diff --git a/api/afdb/session.ts b/api/afdb/session.ts index 0bedac9..ad88e77 100644 --- a/api/afdb/session.ts +++ b/api/afdb/session.ts @@ -65,6 +65,26 @@ export const getSessions = async (sessionId: number) => { } }; +export const getSessionSchedule = async (sessionId: number, batchId?: number) => { + try { + 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) { + 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 04f539e..800886c 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -3,25 +3,26 @@ 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"; -import { formatCurrentTime, formatSessionTime, formatQuizSessionTime } from "@/utils/dateUtils"; +import { formatCurrentTime, formatSessionTime, formatQuizSessionTime, formatTime, isSessionActive } from "@/utils/dateUtils"; import { generateQuizLinks } from "@/utils/quizUtils"; import { api } from "@/services/url"; import { MixpanelTracking } from "@/services/mixpanel"; 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"; 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 { @@ -35,6 +36,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); })); @@ -54,19 +58,22 @@ 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 sessionScheduleData = await getSessionSchedule(groupSession.session_id, batchId); + if (!sessionScheduleData) { + return null; + } + 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; })); 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); MixpanelTracking.getInstance().identify(userId!); } catch (error) { @@ -76,17 +83,20 @@ 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.platform === 'meet') { - if (timeDifference <= 5) { + if (data.session && data.session.platform === 'meet') { + if (minutesUntilSessionStart <= 5 && hasSessionNotEnded) { return ( - + JOIN @@ -95,17 +105,26 @@ export default function Home() { return (

Starts at
- {sessionTimeStr} + {sessionStartTimeStr}

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

+ Starts at
+ {sessionStartTimeStr} +

); } return null; @@ -144,22 +163,22 @@ export default function Home() {

Live Classes

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

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

- {formatSessionTime(data.end_time)} + {formatTime(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)} @@ -176,7 +195,7 @@ export default function Home() {

Tests

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

@@ -189,9 +208,11 @@ export default function Home() {

- Name: {data.name} +
+ Name: {data.name} +
- Stream: {data.stream} + Stream: {data.stream}
{renderButton(data)} diff --git a/app/types.ts b/app/types.ts index 198f8e1..9dec851 100644 --- a/app/types.ts +++ b/app/types.ts @@ -140,3 +140,12 @@ export interface QuizSession { stream: string, id: string } + +export interface SessionSchedule { + id: number, + session_id: number, + start_time: string, + end_time: string, + batch_id: number, + session: Session +} diff --git a/utils/dateUtils.ts b/utils/dateUtils.ts index eabc56e..7e599cd 100644 --- a/utils/dateUtils.ts +++ b/utils/dateUtils.ts @@ -1,15 +1,24 @@ -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"); - 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}`; } @@ -32,3 +41,16 @@ 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}`; +} + +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(); +}