From 5b7a9fb4f28f2634638481a4bc2b2b8b4a429b05 Mon Sep 17 00:00:00 2001 From: JaiPannu-IITI Date: Sun, 14 Jul 2024 19:24:03 +0530 Subject: [PATCH] fixed some leftover api calls in pages --- .../NewTableComponent/StudentModal.tsx | 49 +++++-------------- src/helpers/api.ts | 17 ++++++- 2 files changed, 28 insertions(+), 38 deletions(-) diff --git a/src/components/NewTableComponent/StudentModal.tsx b/src/components/NewTableComponent/StudentModal.tsx index bff30942..85849b74 100644 --- a/src/components/NewTableComponent/StudentModal.tsx +++ b/src/components/NewTableComponent/StudentModal.tsx @@ -18,6 +18,7 @@ import { Button } from '@mui/material'; import {fetchRegistrations} from '@/helpers/api'; import Cookies from 'js-cookie'; import Loader from '@/components/Loader/loader'; +import { fetchStudentDataById, fetchRegistrationDataById } from '@/helpers/api'; const redirect = () => {}; const theme = createTheme({ palette: { @@ -77,24 +78,12 @@ export default function StudentModal({ open, setOpen, id }) { const [registrationData, setRegistrationData] = useState(null); const [loading, setLoading] = useState(true); - const fetchStudentData = async (accessToken:any, id:any) => { - if (!accessToken) { - console.error('No access token provided'); - return; - } - + const fetchStudentData = async (id:any) => { + setLoading(true); try { - const response = await fetch(`${baseUrl}/api/v1/students/${id}`, { - headers: { - Authorization: `Bearer ${accessToken}` - } - }); - if (!response.ok) { - throw new Error(`Error fetching student data: ${response.statusText}`); - } - const data = await response.json(); - setStudentData(data); + const data = await fetchStudentDataById(id); + return data; } catch (error) { console.error('Error fetching student data:', error); } finally { @@ -102,26 +91,12 @@ export default function StudentModal({ open, setOpen, id }) { } }; - const fetchRegistrationData = async (accessToken:any, studentId:any) => { - if (!accessToken) { - console.error('No access token provided'); - return; - } - + const fetchRegistrationData = async (studentId:any) => { + setLoading(true); try { - const response = await fetch(`${baseUrl}/api/v1/registrations`, { - headers: { - Authorization: `Bearer ${accessToken}` - } - }); - - if (!response.ok) { - throw new Error(`Error fetching registration data: ${response.statusText}`); - } - const allData = await response.json(); - const filteredData = allData.filter((registration) => registration.student.id === studentId); - setRegistrationData(filteredData); + const data = await fetchRegistrationDataById(studentId); + return data; } catch (error) { } finally { @@ -131,9 +106,9 @@ export default function StudentModal({ open, setOpen, id }) { useEffect(() => { if (open && id) { - const accessToken = Cookies.get("accessToken"); - fetchStudentData(accessToken, id); - fetchRegistrationData(accessToken, id); + + fetchStudentData(id); + fetchRegistrationData(id); } }, [open, id]); diff --git a/src/helpers/api.ts b/src/helpers/api.ts index ca6bde17..288fcb18 100644 --- a/src/helpers/api.ts +++ b/src/helpers/api.ts @@ -183,6 +183,10 @@ export const fetchStudentData = async ( }); }; +export const fetchStudentDataById = async (id: any) => { + return apiCall(`/students/${id}`); +}; + export const fetchSeasonData = async ( filter: string | undefined ) => { @@ -336,4 +340,15 @@ export const fetchRegistrations = async (studentId:any, seasonId:any, currentSta registered: !currentStatus }] }); -}; \ No newline at end of file +}; + +export const fetchRegistrationDataById = async (studentId: any) => { + try { + const data = await apiCall("/registrations"); + + const filteredData = data.filter((registration: any) => registration.student.id === studentId); + return filteredData; + } catch (error) { + console.error('Error fetching registration data:', error); + } +};