187 lines
5.0 KiB
JavaScript
187 lines
5.0 KiB
JavaScript
// js/utils/api.js
|
|
|
|
// API utility for making requests
|
|
const api = {
|
|
async request(endpoint, options = {}) {
|
|
const headers = {
|
|
'Content-Type': 'application/json',
|
|
...options.headers
|
|
};
|
|
|
|
const config = {
|
|
...options,
|
|
headers
|
|
};
|
|
|
|
try {
|
|
const response = await fetch(`/${endpoint}`, config);
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`API request failed: ${response.status}`);
|
|
}
|
|
|
|
return await response.json();
|
|
} catch (error) {
|
|
console.error('API request error:', error);
|
|
throw error;
|
|
}
|
|
},
|
|
|
|
get(endpoint) {
|
|
return this.request(endpoint);
|
|
},
|
|
|
|
post(endpoint, data) {
|
|
return this.request(endpoint, {
|
|
method: 'POST',
|
|
body: JSON.stringify(data)
|
|
});
|
|
},
|
|
|
|
put(endpoint, data) {
|
|
return this.request(endpoint, {
|
|
method: 'PUT',
|
|
body: JSON.stringify(data)
|
|
});
|
|
},
|
|
|
|
delete(endpoint) {
|
|
return this.request(endpoint, {
|
|
method: 'DELETE'
|
|
});
|
|
}
|
|
};
|
|
|
|
// Fetch availability data from the API
|
|
async function fetchAvailability() {
|
|
try {
|
|
const selectedUid = document.getElementById('uidSelect').value;
|
|
if (!selectedUid) {
|
|
return {}; // Return empty object if no UID is selected
|
|
}
|
|
|
|
return await api.get(`api/availability/${selectedUid}`);
|
|
} catch (error) {
|
|
console.error('Error fetching availability:', error);
|
|
return {}; // Return empty object on error for graceful degradation
|
|
}
|
|
}
|
|
|
|
// Load UIDs from the API
|
|
async function loadUids() {
|
|
try {
|
|
return await api.get('api/uids');
|
|
} catch (error) {
|
|
console.error('Error loading UIDs:', error);
|
|
return []; // Return empty array on error for graceful degradation
|
|
}
|
|
}
|
|
|
|
// Create a new UID
|
|
async function createUid(uid) {
|
|
try {
|
|
return await api.post('api/uids', { uid });
|
|
} catch (error) {
|
|
console.error('Error creating UID:', error);
|
|
throw error; // Rethrow for caller to handle (this is a user-initiated action)
|
|
}
|
|
}
|
|
|
|
// Delete a UID
|
|
async function deleteUid(uid) {
|
|
try {
|
|
await api.delete(`api/uids/${uid}`);
|
|
return true;
|
|
} catch (error) {
|
|
console.error('Error deleting UID:', error);
|
|
throw error; // Rethrow for caller to handle (this is a user-initiated action)
|
|
}
|
|
}
|
|
|
|
// Add a single time slot to a date
|
|
async function addSingleTime(dateStr, time) {
|
|
try {
|
|
const selectedUid = document.getElementById('uidSelect').value;
|
|
if (!selectedUid) {
|
|
throw new Error('No UID selected');
|
|
}
|
|
|
|
// First, get current availability for this date
|
|
const availability = await fetchAvailability();
|
|
const currentTimes = availability[dateStr] || [];
|
|
|
|
// Add the new time if it doesn't already exist
|
|
if (!currentTimes.includes(time)) {
|
|
currentTimes.push(time);
|
|
}
|
|
|
|
// Update the availability with the new times array
|
|
return await api.post(`api/availability/${selectedUid}`, {
|
|
date: dateStr,
|
|
times: currentTimes
|
|
});
|
|
} catch (error) {
|
|
console.error('Error adding time:', error);
|
|
throw error; // Rethrow for caller to handle (this is a user-initiated action)
|
|
}
|
|
}
|
|
|
|
// Remove a time slot from a date
|
|
async function removeTime(dateStr, time) {
|
|
try {
|
|
const selectedUid = document.getElementById('uidSelect').value;
|
|
if (!selectedUid) {
|
|
throw new Error('No UID selected');
|
|
}
|
|
|
|
// First, get current availability for this date
|
|
const availability = await fetchAvailability();
|
|
const currentTimes = availability[dateStr] || [];
|
|
|
|
// Remove the specified time
|
|
const updatedTimes = currentTimes.filter(t => t !== time);
|
|
|
|
// Update the availability with the filtered times array
|
|
return await api.post(`api/availability/${selectedUid}`, {
|
|
date: dateStr,
|
|
times: updatedTimes
|
|
});
|
|
} catch (error) {
|
|
console.error('Error removing time:', error);
|
|
throw error; // Rethrow for caller to handle (this is a user-initiated action)
|
|
}
|
|
}
|
|
|
|
// Flush (delete) all availability entries for a specific UID
|
|
async function flushAvailability(uid) {
|
|
try {
|
|
return await api.delete(`api/availability/${uid}/flush`);
|
|
} catch (error) {
|
|
console.error('Error flushing availability:', error);
|
|
throw error; // Rethrow for caller to handle (this is a user-initiated action)
|
|
}
|
|
}
|
|
|
|
// Reset the database (dev tool)
|
|
async function resetDatabase() {
|
|
try {
|
|
await api.post('api/reset');
|
|
return true;
|
|
} catch (error) {
|
|
console.error('Error resetting database:', error);
|
|
throw error; // Rethrow for caller to handle (this is a user-initiated action)
|
|
}
|
|
}
|
|
|
|
// Export functions and API utility for use in other files
|
|
export {
|
|
api,
|
|
fetchAvailability,
|
|
loadUids,
|
|
createUid,
|
|
deleteUid,
|
|
addSingleTime,
|
|
removeTime,
|
|
flushAvailability,
|
|
resetDatabase
|
|
}; |