Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/avantifellows/gurukul into …
Browse files Browse the repository at this point in the history
…feature/mixpanel-integration
  • Loading branch information
Bahugunajii committed Feb 26, 2024
2 parents 81d7330 + b26db25 commit 3c63303
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 39 deletions.
20 changes: 20 additions & 0 deletions api/afdb/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
77 changes: 49 additions & 28 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<Session[]>([]);
const [liveClasses, setLiveClasses] = useState<SessionSchedule[]>([]);
const [isLoading, setIsLoading] = useState(true);
const [quizzes, setQuizzes] = useState<QuizSession[]>([]);
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 {
Expand All @@ -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);
}));
Expand All @@ -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) {
Expand All @@ -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 (
<Link href={`${portalBaseUrl}/?sessionId=${data.session_id}`} target="_blank">
<Link href={`${portalBaseUrl}/?sessionId=${data.session.session_id}`} target="_blank">
<PrimaryButton
className="bg-primary text-white text-sm rounded-lg w-12 h-8 mr-4 shadow-md shadow-slate-400">JOIN</PrimaryButton>
</Link>
Expand All @@ -95,17 +105,26 @@ export default function Home() {
return (
<p className="text-sm italic font-normal mr-6">
Starts at <br />
{sessionTimeStr}
{sessionStartTimeStr}
</p>
);
}
}
else if (data.redirectPlatform === 'quiz') {
if (minutesUntilSessionStart <= 5 && sessionEndTime.getTime() > currentTimeObj.getTime()) {
return (
<Link href={`${portalBaseUrl}/?sessionId=${data.id}`} target="_blank">
<PrimaryButton
className="bg-primary text-white text-sm rounded-lg w-16 h-8 mr-4 shadow-md shadow-slate-400">START</PrimaryButton>
</Link>
);
}
} else {
return (
<Link href={`${portalBaseUrl}/?sessionId=${data.id}`} target="_blank">
<PrimaryButton
className="bg-primary text-white text-sm rounded-lg w-16 h-8 mr-4 shadow-md shadow-slate-400">START</PrimaryButton>
</Link>
<p className="text-sm italic font-normal mr-6">
Starts at <br />
{sessionStartTimeStr}
</p>
);
}
return null;
Expand Down Expand Up @@ -144,22 +163,22 @@ export default function Home() {
<h1 className="text-primary ml-4 font-semibold text-xl pt-6">Live Classes</h1>
{liveClasses.length > 0 ? (
<div className="grid grid-cols-1 gap-4 pb-16">
{liveClasses.map((data, index) => (
{liveClasses.map((data, index) => (isSessionActive(formatSessionTime(data.end_time)) &&
<div key={index} className="flex mt-4 items-center" >
<div>
<p className={`${commonTextClass}`}>
{formatSessionTime(data.start_time)}
{formatTime(data.start_time)}
</p>
<p className={`${commonTextClass}`}>
{formatSessionTime(data.end_time)}
{formatTime(data.end_time)}
</p>
</div>
<div className="bg-white rounded-lg shadow-lg min-h-24 h-auto py-6 relative w-full flex flex-row justify-between mr-4">
<div className={`${index % 2 === 0 ? 'bg-orange-200' : 'bg-red-200'} h-full w-2 absolute left-0 top-0 rounded-s-md`}></div>
<div className="text-sm md:text-base font-semibold mx-6 md:mx-8">
<span className="font-normal pr-4">Subject:</span> {data.meta_data.subject ?? "Science"}
<span className="font-normal pr-4">Subject:</span> {data.session.meta_data.subject ?? "Science"}
<div className="text-sm md:text-base font-semibold ">
<span className="font-normal pr-7">Batch:</span> {data.meta_data.batch ?? "Master Batch"}
<span className="font-normal pr-7">Batch:</span> {data.session.meta_data.batch ?? "Science Batch"}
</div>
</div>
{renderButton(data)}
Expand All @@ -176,7 +195,7 @@ export default function Home() {
<h1 className="text-primary ml-4 font-semibold text-xl">Tests</h1>
{quizzes.length > 0 ? (
<div className="grid grid-cols-1 gap-4 pb-40">
{quizzes.map((data, index) => (
{quizzes.map((data, index) => (isSessionActive(formatSessionTime(data.end_time)) &&
<div key={index} className="flex mt-4 items-center" >
<div>
<p className={`${commonTextClass}`}>
Expand All @@ -189,9 +208,11 @@ export default function Home() {
<div className="bg-white rounded-lg shadow-lg min-h-24 h-auto py-6 relative w-full flex flex-row justify-between mr-4">
<div className={`${index % 2 === 0 ? 'bg-orange-200' : 'bg-red-200'} h-full w-2 absolute left-0 top-0 rounded-s-md`}></div>
<div className="text-sm md:text-base font-semibold mx-6 md:mx-8">
<span className="font-normal pr-8">Name:</span> {data.name}
<div className="flex">
<span className="font-normal pr-8">Name:</span> <span>{data.name}</span>
</div>
<div className="text-sm md:text-base font-semibold ">
<span className="font-normal pr-6">Stream:</span> {data.stream}
<span className="font-normal pr-5">Stream:</span> <span>{data.stream}</span>
</div>
</div>
{renderButton(data)}
Expand Down
9 changes: 9 additions & 0 deletions app/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
44 changes: 33 additions & 11 deletions utils/dateUtils.ts
Original file line number Diff line number Diff line change
@@ -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}`;
}

Expand All @@ -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();
}

0 comments on commit 3c63303

Please sign in to comment.