Skip to content

Commit

Permalink
Merge pull request #9 from avantifellows/home-page
Browse files Browse the repository at this point in the history
Home page
  • Loading branch information
Bahugunajii authored Nov 9, 2023
2 parents 32119e9 + 6782c2f commit e449a21
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 12 deletions.
32 changes: 32 additions & 0 deletions app/SessionList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import axios from 'axios';

const url = process.env.NEXT_PUBLIC_AF_DB_SERVICE_URL;
const bearerToken = process.env.NEXT_PUBLIC_AF_DB_SERVICE_BEARER_TOKEN;

export const getSessionOccurrences = async () => {
try {
const response = await axios.get(`${url}/session-occurrence`, {
headers: {
'Authorization': `Bearer ${bearerToken}`
}
});
return response.data;
} catch (error) {
console.error("Error in fetching Session Occurrence:", error);
throw error;
}
};

export const getSessions = async (sessionId: number) => {
try {
const response = await axios.get(`${url}/session/${sessionId}`, {
headers: {
'Authorization': `Bearer ${bearerToken}`
}
});
return response.data;
} catch (error) {
console.error("Error in fetching Session Details:", error);
throw error;
}
};
2 changes: 1 addition & 1 deletion app/library/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ const Page = () => {
<h1 className="font-semibold ml-4 text-xl pt-6">NEET Course <br /></h1>
<span className="text-xs ml-4 font-normal">New Delhi</span>
</div>
<div className="flex flex-row mt-4 mb-4 justify-between">
<div className="flex flex-row mt-4 mb-4 justify-between md:mx-4 mx-1">
<PrimaryButton
onClick={() => handleTabClick('Physics')}
className={activeTab === 'Physics' ? 'bg-heading text-primary' : 'bg-white text-slate-600'}
Expand Down
182 changes: 176 additions & 6 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,199 @@
import { useAuth } from "./AuthContext";
import TopBar from "@/components/TopBar";
import BottomNavigationBar from "@/components/BottomNavigationBar";
import { getSessionOccurrences, getSessions } from "./SessionList";
import { useState, useEffect } from "react";
import { LiveClasses } from "./types";
import Link from "next/link";
import PrimaryButton from "@/components/Button";
import Loading from "./loading";

export default function Home() {
const { loggedIn, userId } = useAuth();
const [liveClasses, setLiveClasses] = useState<LiveClasses[]>([]);
const [isLoading, setIsLoading] = useState(true);
const [quizzes, setQuizzes] = useState<LiveClasses[]>([]);
const baseUrl = process.env.NEXT_PUBLIC_AF_QUIZ_URL;
const apiKey = process.env.NEXT_PUBLIC_AF_QUIZ_API_KEY;
const userID = process.env.NEXT_PUBLIC_AF_QUIZ_USER_ID;

const fetchSessionOccurrencesAndDetails = async () => {
try {
const sessionOccurrenceData = await getSessionOccurrences();
const currentDate = new Date();
currentDate.setHours(0, 0, 0, 0);

const todaySessions = [];

for (const sessionOccurrence of sessionOccurrenceData) {
const sessionStartTime = new Date(sessionOccurrence.start_time);

if (isSameDay(sessionStartTime, currentDate)) {
try {
const sessionDetail = await getSessions(sessionOccurrence.session_fk);
todaySessions.push({
sessionOccurrence,
sessionDetail,
});
} catch (error) {
console.error("Error fetching session details for session ID:", sessionOccurrence.session_fk, error);
}
}
}

const liveClassesToday = todaySessions.filter(data => data.sessionDetail.platform === "meet");
const quizzesToday = todaySessions.filter(data => data.sessionDetail.platform === "quiz");

setLiveClasses(liveClassesToday);
setQuizzes(quizzesToday);
} catch (error) {
console.error("Error in fetching Live Classes:", error);
}
}



function isSameDay(date1: Date, date2: Date): boolean {
return (
date1.getDate() === date2.getDate() &&
date1.getMonth() === date2.getMonth() &&
date1.getFullYear() === date2.getFullYear()
);
}


function formatTime(dateTimeStr: string) {
const date = new Date(dateTimeStr);
const hours = String(date.getUTCHours()).padStart(2, "0");
const minutes = String(date.getUTCMinutes()).padStart(2, "0");
return `${hours}:${minutes}`;
}

function renderButton(data: { sessionOccurrence: any, sessionDetail: any }) {
const currentTime = new Date();
const sessionTimeStr = formatTime(data.sessionOccurrence.start_time);
const currentTimeStr = formatTime(currentTime.toISOString());

if (data.sessionDetail.platform === 'meet') {
if (sessionTimeStr <= currentTimeStr) {
return (
<Link href={`https://${data.sessionDetail.platform_link}`} target="_blank">
<PrimaryButton
className="bg-primary text-white text-sm rounded-lg w-14 h-8 mr-4 shadow-md shadow-slate-400">JOIN</PrimaryButton>
</Link>
);
} else {
return (
<p className="text-sm italic font-normal mr-6">
Starts at <br />
{sessionTimeStr}
</p>
);
}
} else if (data.sessionDetail.platform === 'quiz') {
return (
<Link href={`${baseUrl}${data.sessionDetail.platform_link}?apiKey=${apiKey}&userId=${userID}`} 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>
);
}
return null;
}

useEffect(() => {
async function fetchData() {
setIsLoading(true);

try {
await fetchSessionOccurrencesAndDetails();
} catch (error) {
console.log("Error:", error)
} finally {
setIsLoading(false);
}
}

fetchData();
}, []);


return (
<>
{loggedIn ? (
<main className="max-w-xl mx-auto bg-white">
{/* {loggedIn ? ( */}
{isLoading ? (
<div className="md:max-w-xl md:mx-auto">
<TopBar />
<Loading />
</div>
) : (
<main className="md:max-w-xl md:mx-auto bg-white">
<TopBar />
<div className="min-h-screen bg-heading">
<h1 className="text-primary ml-4 font-semibold text-xl pt-6">Welcome to Gurukul</h1>
<h1 className="text-primary ml-4 font-semibold text-xl pt-6">Live Classes</h1>
<div className="grid grid-cols-1 gap-4 pb-16">
{liveClasses.map((data, index) => (
<div key={index} className="flex mt-4 items-center" >
<div>
<p className="text-gray-700 text-sm md:text-base mx-6 md:mx-8">
{formatTime(data.sessionOccurrence.start_time)}
</p>
<p className="text-gray-700 text-sm md:text-base mx-6 md:mx-8">
{formatTime(data.sessionOccurrence.end_time)}
</p>
</div>
<div className="bg-card 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.sessionDetail.meta_data.subject ?? "Science"}
<div className="text-sm md:text-base font-semibold ">
<span className="font-normal pr-7">Batch:</span> {data.sessionDetail.meta_data.batch ?? "Master Batch"}
</div>
</div>
{renderButton(data)}
</div>
</div>
))}
</div>
</div>
<div className="bg-heading">
<h1 className="text-primary ml-4 font-semibold text-xl">Tests</h1>
<div className="grid grid-cols-1 gap-4 pb-40">
{quizzes.map((data, index) => (
<div key={index} className="flex mt-4 items-center" >
<div>
<p className="text-gray-700 text-sm md:text-base mx-6 md:mx-8">
{formatTime(data.sessionOccurrence.start_time)}
</p>
<p className="text-gray-700 text-sm md:text-base mx-6 md:mx-8">
{formatTime(data.sessionOccurrence.end_time)}
</p>
</div>
<div className="bg-card 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.sessionDetail.meta_data.stream}
<div className="text-sm md:text-base font-semibold ">
<span className="font-normal pr-9">Type:</span> {data.sessionDetail.meta_data.test_type}
</div>
</div>
{renderButton(data)}
</div>
</div>
))}
</div>
</div>
<BottomNavigationBar />
</main>
) : (
</main>)}
{/* ) : (
<main className="max-w-xl mx-auto bg-white">
<TopBar />
<div className="min-h-screen flex flex-col items-center justify-between p-24">
<p>User not logged in</p>
</div>
<BottomNavigationBar />
</main>
)}
)} */}
</>
);
}
6 changes: 3 additions & 3 deletions app/reports/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ export default function ReportsPage() {

return (
<>
{loggedIn ? (
{/* {loggedIn ? ( */}
<main className="max-w-xl mx-auto bg-white">
<TopBar />
<Client message="">
<ReportsList />
</Client>
<BottomNavigationBar />
</main>
) : (
{/* ) : (
<main className="max-w-xl mx-auto bg-white">
<TopBar />
<Loading />
</main>

)}
)} */}
</>
);
}
2 changes: 1 addition & 1 deletion app/reports/reports_list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default function ReportsList() {
<div className="grid grid-cols-1 gap-4 pb-40">
{responseData.reports.map((report: Report, index: number) => (
<Link href={report.report_link} target="_blank" key={index} className="bg-card rounded-lg shadow-lg h-24 mx-4 mt-4 relative">
<div className="bg-red-200 h-full w-2 absolute left-0 top-0"></div>
<div className={`${index % 2 === 0 ? 'bg-orange-200' : 'bg-red-200'} h-full w-2 absolute left-0 top-0 rounded-s-md`}></div>
<p className="text-sm md:text-base font-semibold mx-6 md:mx-8 mt-2 md:mt-4">{report.test_name}</p>
<p className="text-gray-700 text-sm md:text-base mx-6 md:mx-8 mt-2">Rank: {report.rank}</p>
</Link>
Expand Down
30 changes: 29 additions & 1 deletion app/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface Report {
};

export interface PrimaryButton {
onClick: () => void;
onClick?: () => void;
children: React.ReactNode;
className?: string;
}
Expand Down Expand Up @@ -60,3 +60,31 @@ export interface Topic {
name: string;
chapter_id: number;
}

export interface SessionOccurrence {
id: number;
name: string;
session_id: string;
session_fk: number;
start_time: string;
end_time: string;
}

export interface Session {
id: number;
name: string;
platform: string;
platform_link: string;
meta_data: {
subject: string;
batch: string;
stream: string;
test_type: string;
};
}

export interface LiveClasses {
sessionOccurrence: SessionOccurrence;
sessionDetail: Session;
}

0 comments on commit e449a21

Please sign in to comment.