This commit is contained in:
Harikrishnan Gopal
2024-12-17 11:21:25 +05:30
parent ce73a591c5
commit b4adca13c7
7 changed files with 702 additions and 461 deletions

View File

@@ -1,55 +1,122 @@
const BASE_URL = "http://localhost:8080/api";
export const fetchCourses = async (filterData) => {
// Helper function for handling fetch requests
const fetchData = async (url, options) => {
try {
const response = await fetch(url, options);
// Check if response is OK (status 200-299)
if (!response.ok) {
let errorDetails = {};
try {
errorDetails = await response.json(); // Attempt to parse error response
} catch (err) {
console.warn("Failed to parse error details:", err);
}
throw new Error(
`Error: ${response.statusText} (${response.status}) - ${
errorDetails.message || "No details available"
}`
);
}
// Return JSON response if successful
return await response.json();
} catch (error) {
console.error(`Request failed for ${url}:`, error.message);
throw error; // Re-throw for the caller to handle
}
};
// Fetch courses with optional filters
export const fetchCourses = async (filterData = {}) => {
try {
// Serialize filterData into query parameters
const queryString = new URLSearchParams(filterData).toString();
// console.log(queryString);
const response = await fetch(`${BASE_URL}/courses?${queryString}`, {
const url = `${BASE_URL}/courses?${queryString}`;
return fetchData(url, {
method: "GET",
headers: {
"Content-Type": "application/json",
},
});
if (!response.ok) {
throw new Error("Failed to fetch courses");
}
const filteredCourses = await response.json();
console.log(filteredCourses);
return filteredCourses;
} catch (error) {
console.error("Error fetching courses:", error);
throw error; // Re-throw error to be handled by the caller
console.error("Error fetching courses:", error.message);
throw error;
}
};
// Fetch list of faculties
export const fetchFaculties = async () => {
try {
const response = await fetch(`${BASE_URL}/faculty`);
if (!response.ok) {
throw new Error(`Failed to fetch faculties: ${response.statusText}`);
}
const data = await response.json();
return data;
const url = `${BASE_URL}/faculty`;
return fetchData(url, {
method: "GET",
headers: {
"Content-Type": "application/json",
},
});
} catch (error) {
console.error("Error fetching faculties:", error.message);
throw error;
}
};
// Fetch available options for form dropdowns
export const fetchOptions = async () => {
try {
const response = await fetch(`${BASE_URL}/options`);
if (!response.ok) {
throw new Error(`Failed to fetch options: ${response.statusText}`);
}
const data = await response.json();
return data;
const url = `${BASE_URL}/options`;
return fetchData(url, {
method: "GET",
headers: {
"Content-Type": "application/json",
},
});
} catch (error) {
console.error("Error fetching options:", error.message);
throw error;
}
};
// Save multiple appointments to MongoDB
export const saveAppointment = async (appointmentsData) => {
console.log("Saving appointments with payload:", appointmentsData);
// Validate input format
if (!Array.isArray(appointmentsData) || appointmentsData.length === 0) {
const errorMessage =
"Invalid or missing appointment data: expected a non-empty array";
console.error(errorMessage);
throw new Error(errorMessage);
}
// Validate each appointment's structure
const invalidEntries = appointmentsData.filter(
(appointment) =>
!appointment.facultyId || !appointment.courseId || !Array.isArray(appointment.tasks)
);
if (invalidEntries.length > 0) {
const errorMessage = `Invalid appointments detected: ${JSON.stringify(
invalidEntries
)}`;
console.error(errorMessage);
throw new Error(errorMessage);
}
try {
const url = `${BASE_URL}/appointments`;
const response = await fetchData(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ appointments: appointmentsData }), // Send appointments as an array
});
console.log("Appointments saved successfully:", response);
return response;
} catch (error) {
console.error("Error saving appointments:", error.message);
throw error;
}
};