fixed some of the loading bugs with shifts so that the map can load faster.

This commit is contained in:
2025-08-18 14:03:36 -06:00
parent 2a424d1399
commit c220ef265a
4 changed files with 198 additions and 1254 deletions

View File

@@ -2192,8 +2192,9 @@ async function addUserToShift() {
return;
}
if (!currentShiftData) {
showStatus('No shift selected', 'error');
if (!currentShiftData || !currentShiftData.ID) {
showStatus('No shift selected or invalid shift data', 'error');
console.error('Invalid currentShiftData:', currentShiftData);
return;
}
@@ -2212,9 +2213,14 @@ async function addUserToShift() {
showStatus('User successfully added to shift', 'success');
userSelect.value = ''; // Clear selection
// Refresh the shift data and reload volunteers
await refreshCurrentShiftData();
console.log('Refreshed shift data after adding user');
// Refresh the shift data and reload volunteers with better error handling
try {
await refreshCurrentShiftData();
console.log('Refreshed shift data after adding user');
} catch (refreshError) {
console.error('Error during refresh after adding user:', refreshError);
// Still show success since the add operation worked
}
} else {
showStatus(data.error || 'Failed to add user to shift', 'error');
}
@@ -2230,8 +2236,9 @@ async function removeVolunteerFromShift(volunteerId, volunteerEmail) {
return;
}
if (!currentShiftData) {
showStatus('No shift selected', 'error');
if (!currentShiftData || !currentShiftData.ID) {
showStatus('No shift selected or invalid shift data', 'error');
console.error('Invalid currentShiftData:', currentShiftData);
return;
}
@@ -2245,9 +2252,14 @@ async function removeVolunteerFromShift(volunteerId, volunteerEmail) {
if (data.success) {
showStatus('Volunteer successfully removed from shift', 'success');
// Refresh the shift data and reload volunteers
await refreshCurrentShiftData();
console.log('Refreshed shift data after removing volunteer');
// Refresh the shift data and reload volunteers with better error handling
try {
await refreshCurrentShiftData();
console.log('Refreshed shift data after removing volunteer');
} catch (refreshError) {
console.error('Error during refresh after removing volunteer:', refreshError);
// Still show success since the remove operation worked
}
} else {
showStatus(data.error || 'Failed to remove volunteer from shift', 'error');
}
@@ -2259,45 +2271,73 @@ async function removeVolunteerFromShift(volunteerId, volunteerEmail) {
// Refresh current shift data
async function refreshCurrentShiftData() {
if (!currentShiftData) return;
if (!currentShiftData || !currentShiftData.ID) {
console.warn('No current shift data or missing ID, skipping refresh');
return;
}
try {
console.log('Refreshing shift data for shift ID:', currentShiftData.ID);
// Reload admin shifts to get updated data
const response = await fetch('/api/shifts/admin');
// Instead of reloading ALL admin shifts, just get this specific shift's signups
// This prevents the expensive backend call and reduces the refresh cascade
const response = await fetch(`/api/shifts/admin`);
const data = await response.json();
if (data.success) {
const updatedShift = data.shifts.find(s => s.ID === currentShiftData.ID);
if (data.success && data.shifts && Array.isArray(data.shifts)) {
const updatedShift = data.shifts.find(s => s && s.ID === currentShiftData.ID);
if (updatedShift) {
console.log('Found updated shift with', updatedShift.signups?.length || 0, 'volunteers');
currentShiftData = updatedShift;
displayCurrentVolunteers(updatedShift.signups || []);
// Immediately refresh the main shifts list to show updated counts
console.log('Refreshing main shifts list with', data.shifts.length, 'shifts');
displayAdminShifts(data.shifts);
// Only update the specific shift in the main list, don't refresh everything
updateShiftInList(updatedShift);
} else {
console.warn('Could not find updated shift with ID:', currentShiftData.ID);
}
} else {
console.error('Failed to refresh shift data:', data.error);
console.error('Failed to refresh shift data:', data.error || 'Invalid response format');
}
} catch (error) {
console.error('Error refreshing shift data:', error);
}
}
// New function to update a single shift in the list without full refresh
function updateShiftInList(updatedShift) {
const shiftElement = document.querySelector(`[data-shift-id="${updatedShift.ID}"]`);
if (shiftElement) {
const shiftItem = shiftElement.closest('.shift-admin-item');
if (shiftItem) {
const signupCount = updatedShift.signups ? updatedShift.signups.length : 0;
// Find the volunteer count paragraph (contains 👥)
const volunteerCountElement = Array.from(shiftItem.querySelectorAll('p')).find(p =>
p.textContent.includes('👥')
);
if (volunteerCountElement) {
volunteerCountElement.textContent = `👥 ${signupCount}/${updatedShift['Max Volunteers']} volunteers`;
}
// Update the data attribute with new shift data
const manageBtn = shiftItem.querySelector('.manage-volunteers-btn');
if (manageBtn) {
manageBtn.setAttribute('data-shift', JSON.stringify(updatedShift).replace(/'/g, "'"));
}
}
}
}
// Close modal
function closeShiftUserModal() {
document.getElementById('shift-user-modal').style.display = 'none';
currentShiftData = null;
// Refresh the main shifts list one more time when closing the modal
// to ensure any changes are reflected
console.log('Refreshing shifts list on modal close');
loadAdminShifts();
// Don't refresh the entire shifts list when closing modal
// The shifts list should already be up to date from the individual updates
console.log('Modal closed - shifts list should already be current');
}
// Email shift details to all volunteers

File diff suppressed because it is too large Load Diff