Skip to content

Commit

Permalink
fixed some leftover api calls in pages
Browse files Browse the repository at this point in the history
  • Loading branch information
JaiPannu-IITI committed Jul 14, 2024
1 parent 99bb158 commit 5b7a9fb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 38 deletions.
49 changes: 12 additions & 37 deletions src/components/NewTableComponent/StudentModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -77,51 +78,25 @@ 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 {
setLoading(false);
}
};

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 {
Expand All @@ -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]);

Expand Down
17 changes: 16 additions & 1 deletion src/helpers/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
) => {
Expand Down Expand Up @@ -336,4 +340,15 @@ export const fetchRegistrations = async (studentId:any, seasonId:any, currentSta
registered: !currentStatus
}]
});
};
};

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);
}
};

0 comments on commit 5b7a9fb

Please sign in to comment.