forked from CSI-KJSCE/appointment_to_examiner
140 lines
3.7 KiB
JavaScript
140 lines
3.7 KiB
JavaScript
const BASE_URL = "http://localhost:8080/api";
|
|
|
|
// 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 {
|
|
const queryString = new URLSearchParams(filterData).toString();
|
|
const url = `${BASE_URL}/courses?${queryString}`;
|
|
return fetchData(url, {
|
|
method: "GET",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
});
|
|
} catch (error) {
|
|
console.error("Error fetching courses:", error.message);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
// Fetch list of faculties
|
|
export const fetchFaculties = async () => {
|
|
try {
|
|
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 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;
|
|
}
|
|
};
|
|
|
|
// Update course status
|
|
export const updateCourseStatus = async (courseId) => {
|
|
if (!courseId) {
|
|
throw new Error("Course ID is required to update the status");
|
|
}
|
|
|
|
const url = `${BASE_URL}/courses/${courseId}`;
|
|
return fetchData(url, {
|
|
method: "PATCH",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify({ status: "submitted" }), // Update status to "Submitted"
|
|
});
|
|
};
|
|
|