Skip to content

Commit

Permalink
Merge pull request #16 from avantifellows/feature/auth-flow
Browse files Browse the repository at this point in the history
Fetch user details from token
  • Loading branch information
Bahugunajii authored Dec 23, 2023
2 parents 436525c + 08f1ed6 commit e07b6fd
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 135 deletions.
27 changes: 27 additions & 0 deletions api/afdb/userName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"use server"

import axios from 'axios';
import getAxiosConfig from '../axiosConfig';
import { User } from '@/app/types';

export const getUserName = async (studentId: string): Promise<User | null> => {
const url = process.env.AF_DB_SERVICE_URL;
const bearerToken = process.env.AF_DB_SERVICE_BEARER_TOKEN!;

try {
const response = await axios.get(`${url}/student`, {
params: { student_id: studentId },
...getAxiosConfig(bearerToken),
});

if (response.data.length === 0) {
console.warn(`No user found for student ID: ${studentId}`);
return null;
}

return response.data[0].user;
} catch (error) {
console.error('Error fetching student data:', error);
throw error;
}
}
6 changes: 2 additions & 4 deletions api/reporting/reports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import { api } from "@/services/url";
import axios from "axios";

export async function getReports() {
export async function getReports(userId: string) {
const apiKey = process.env.AF_REPORTS_DB_API_KEY;
// Temporary till we implement tokens in portal
const studentId = process.env.STUDENT_ID;
const url = `${api.reports.baseUrl}${api.reports.student_reports}${studentId}?format=json`;
const url = `${api.reports.baseUrl}${api.reports.student_reports}${userId}?format=json`;

try {
const responseData = await axios.get(url, {
Expand Down
177 changes: 87 additions & 90 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default function Home() {
const [isLoading, setIsLoading] = useState(true);
const [quizzes, setQuizzes] = useState<LiveClasses[]>([]);
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";
const infoMessageClass = "flex items-center justify-center text-center h-72 mx-4 pb-40";

const fetchSessionOccurrencesAndDetails = async () => {
try {
Expand Down Expand Up @@ -82,7 +82,7 @@ export default function Home() {
);
}
} else if (data.sessionDetail.platform === 'quiz') {
generateQuizLink(data.sessionDetail.platform_link)
generateQuizLink(data.sessionDetail.platform_link, userId!)
.then((quizLink) => {
return (
<Link href={quizLink} target="_blank">
Expand All @@ -99,113 +99,110 @@ export default function Home() {
return null;
}

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

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

fetchData();
}, []);
useEffect(() => {
if (loggedIn) {
fetchData();
}
}, [loggedIn]);

return (
<>
{/* {loggedIn ? ( */}
{isLoading ? (
<div className="max-w-xl mx-auto">
<TopBar />
<Loading />
</div>
) : (
<main className="min-h-screen max-w-xl mx-auto md:mx-auto bg-white">
<TopBar />
<div className="bg-heading">
<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) => (
<div key={index} className="flex mt-4 items-center" >
<div>
<p className={`${commonTextClass}`}>
{formatSessionTime(data.sessionOccurrence.start_time)}
</p>
<p className={`${commonTextClass}`}>
{formatSessionTime(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"}
{loggedIn ? (
isLoading ? (
<div className="max-w-xl mx-auto">
<TopBar />
<Loading />
</div>
) : (
<main className="min-h-screen max-w-xl mx-auto md:mx-auto bg-white">
<TopBar />
<div className="bg-heading">
<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) => (
<div key={index} className="flex mt-4 items-center" >
<div>
<p className={`${commonTextClass}`}>
{formatSessionTime(data.sessionOccurrence.start_time)}
</p>
<p className={`${commonTextClass}`}>
{formatSessionTime(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>
{renderButton(data)}
</div>
</div>
))}
</div>) : (
<p className={infoMessageClass}>
Good Job! There are no more pending live classes today.
</p>
)}
</div>
<div className="bg-heading">
<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) => (
<div key={index} className="flex mt-4 items-center" >
<div>
<p className={`${commonTextClass}`}>
{formatSessionTime(data.sessionOccurrence.start_time)}
</p>
<p className={`${commonTextClass}`}>
{formatSessionTime(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>) : (
<p className={infoMessageClass}>
There are no more live classes today. You can relax!
</p>
)}
</div>
<div className="bg-heading">
<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) => (
<div key={index} className="flex mt-4 items-center" >
<div>
<p className={`${commonTextClass}`}>
{formatSessionTime(data.sessionOccurrence.start_time)}
</p>
<p className={`${commonTextClass}`}>
{formatSessionTime(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>
{renderButton(data)}
</div>
</div>
))}
</div>) : (
<p className={`${infoMessageClass}`}>
Good Job! There are no more pending tests today.
</p>
)}
</div>
<div className="text-blue-500 underline pb-40 bg-heading text-center px-4">
<Link href="https://docs.google.com/spreadsheets/d/1zIjYf4KUXkAHLiuWdtHp-l-5xa96obBtbQU0UvuOy7w/edit?usp=sharing" target="_blank">
Click on the link to see all upcoming Tests
</Link>
</div>
<BottomNavigationBar />
</main>)}
{/* ) : (
))}
</div>) : (
<p className={`${infoMessageClass}`}>
There are no more tests today. You can relax!
</p>
)}
</div>
<BottomNavigationBar />
</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>
)} */}
)}
</>
);
}
22 changes: 11 additions & 11 deletions app/reports/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@ import TopBar from "@/components/TopBar";
import { useAuth } from "../../services/AuthContext";

export default function ReportsPage() {
const { loggedIn } = useAuth();
const { loggedIn, userId } = useAuth();

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

)} */}
)}
</>
);
}
33 changes: 21 additions & 12 deletions app/reports/reports_list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,47 @@ import { Report } from "../types";
import { useState, useEffect } from "react";
import Loading from "../loading";
import { getReports } from "@/api/reporting/reports";
import { ReportsListProps } from "../types";

export default function ReportsList() {
export default function ReportsList({ userId }: ReportsListProps) {
const [responseData, setResponseData] = useState<{ reports: Report[] } | null>(null);

useEffect(() => {
async function fetchReportsData() {
try {
const data = await getReports();
const data = await getReports(userId);
setResponseData(data);
} catch (error) {
throw error;
}
}

fetchReportsData();
}, []);
}, [userId]);

if (!responseData) {
return <Loading />;
}

return (
<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 relative flex items-center my-1 md:my-2">
<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-left mx-6 md:mx-8">
<p className="text-sm md:text-base font-semibold">{report.test_name}</p>
<p className="text-gray-700 text-sm md:text-base mt-2">Rank: {report.rank}</p>
</div>
</Link>
))}
{responseData.reports.length > 0 ? (
<>
{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 relative flex items-center my-1 md:my-2">
<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-left mx-6 md:mx-8">
<p className="text-sm md:text-base font-semibold">{report.test_name}</p>
<p className="text-gray-700 text-sm md:text-base mt-2">Rank: {report.rank}</p>
</div>
</Link>
))}
</>
) : (
<div className="mt-20 flex items-center justify-center text-center mx-4">
Sorry! There are no reports avaiable.
</div>
)}
</div>
);
}
9 changes: 9 additions & 0 deletions app/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,16 @@ export interface LiveClasses {
sessionOccurrence: SessionOccurrence;
sessionDetail: Session;
}
export interface ReportsListProps {
userId: string;

}
export interface AxiosAdditionalHeaders {
[key: string]: string;
}

export interface User {
id: number;
first_name: string;
last_name: string;
}
2 changes: 1 addition & 1 deletion components/TopBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const TopBar = () => {
};

const pathname = usePathname();
const routeName = routeNames[pathname] || <p>Welcome, <br /> Shagun Panday <br /> </p>;
const routeName = routeNames[pathname] || <p>Welcome <br /> {userName} </p>;

return (
<div className="max-w-xl mx-auto text-white p-4 h-32 justify-between items-center bg-primary">
Expand Down
Loading

0 comments on commit e07b6fd

Please sign in to comment.