fix: RSVP update functionality to properly handle 204 responses and prevent accidental deletions
This commit is contained in:
@@ -261,68 +261,53 @@ const EventAdmin: React.FC = () => {
|
|||||||
bringing_guests: editForm.bringing_guests,
|
bringing_guests: editForm.bringing_guests,
|
||||||
guest_count: parseInt(editForm.guest_count.toString(), 10),
|
guest_count: parseInt(editForm.guest_count.toString(), 10),
|
||||||
guest_names: editForm.guest_names,
|
guest_names: editForm.guest_names,
|
||||||
items_bringing: JSON.stringify(editForm.items_bringing)
|
items_bringing: JSON.stringify(editForm.items_bringing),
|
||||||
|
event_id: event.id // Ensure event_id is included
|
||||||
};
|
};
|
||||||
|
|
||||||
// Log the data being sent for debugging
|
|
||||||
console.log('Submitting RSVP update:', {
|
console.log('Submitting RSVP update:', {
|
||||||
endpoint: `/api/events/${slug}/rsvps/${rsvpToEdit.id}`,
|
endpoint: `/api/events/${slug}/rsvps/${rsvpToEdit.id}`,
|
||||||
data: submissionData
|
data: submissionData
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
// Update the RSVP
|
||||||
// First try to get the current RSVP to verify the endpoint
|
const response = await axios({
|
||||||
const checkResponse = await axios.get(`/api/events/${slug}/rsvps`, {
|
method: 'put',
|
||||||
headers: {
|
url: `/api/events/${slug}/rsvps/${rsvpToEdit.id}`,
|
||||||
'Accept': 'application/json'
|
data: submissionData,
|
||||||
}
|
headers: {
|
||||||
});
|
'Content-Type': 'application/json',
|
||||||
console.log('Current RSVPs:', checkResponse.data);
|
'Accept': 'application/json'
|
||||||
|
|
||||||
// Update the RSVP
|
|
||||||
const response = await axios({
|
|
||||||
method: 'put',
|
|
||||||
url: `/api/events/${slug}/rsvps/${rsvpToEdit.id}`,
|
|
||||||
data: submissionData,
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'Accept': 'application/json'
|
|
||||||
},
|
|
||||||
validateStatus: (status) => {
|
|
||||||
return status >= 200 && status < 300;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Log the response for debugging
|
|
||||||
console.log('Server response:', {
|
|
||||||
status: response.status,
|
|
||||||
statusText: response.statusText,
|
|
||||||
data: response.data,
|
|
||||||
headers: response.headers
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.data) {
|
|
||||||
throw new Error('Server returned empty response');
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// Create updated RSVP object
|
console.log('Server response:', {
|
||||||
|
status: response.status,
|
||||||
|
statusText: response.statusText,
|
||||||
|
data: response.data,
|
||||||
|
headers: response.headers
|
||||||
|
});
|
||||||
|
|
||||||
|
// Handle successful update (both 200 and 204 responses)
|
||||||
|
if (response.status === 204 || response.status === 200) {
|
||||||
|
// Create updated RSVP object using our submitted data since 204 returns no content
|
||||||
const updatedRsvp: RSVP = {
|
const updatedRsvp: RSVP = {
|
||||||
|
...rsvpToEdit,
|
||||||
...submissionData,
|
...submissionData,
|
||||||
id: rsvpToEdit.id,
|
|
||||||
event_id: event.id,
|
|
||||||
items_bringing: editForm.items_bringing // Keep as array in local state
|
items_bringing: editForm.items_bringing // Keep as array in local state
|
||||||
};
|
};
|
||||||
|
|
||||||
// Update the local state
|
// Update the local state
|
||||||
const updatedRsvps = rsvps.map((r: RSVP) => {
|
setRsvps(prevRsvps => prevRsvps.map((r: RSVP) =>
|
||||||
if (r.id === rsvpToEdit.id) {
|
r.id === rsvpToEdit.id ? updatedRsvp : r
|
||||||
return updatedRsvp;
|
));
|
||||||
}
|
|
||||||
return r;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Recalculate claimed items
|
// Recalculate claimed items
|
||||||
const claimed = new Set<string>();
|
const claimed = new Set<string>();
|
||||||
|
const updatedRsvps = rsvps.map((r: RSVP) =>
|
||||||
|
r.id === rsvpToEdit.id ? updatedRsvp : r
|
||||||
|
);
|
||||||
|
|
||||||
updatedRsvps.forEach((rsvp: RSVP) => {
|
updatedRsvps.forEach((rsvp: RSVP) => {
|
||||||
let rsvpItems: string[] = [];
|
let rsvpItems: string[] = [];
|
||||||
try {
|
try {
|
||||||
@@ -332,7 +317,6 @@ const EventAdmin: React.FC = () => {
|
|||||||
} else if (Array.isArray(rsvp.items_bringing)) {
|
} else if (Array.isArray(rsvp.items_bringing)) {
|
||||||
rsvpItems = rsvp.items_bringing;
|
rsvpItems = rsvp.items_bringing;
|
||||||
}
|
}
|
||||||
|
|
||||||
rsvpItems.forEach(item => claimed.add(item));
|
rsvpItems.forEach(item => claimed.add(item));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Error processing items for RSVP:', e);
|
console.error('Error processing items for RSVP:', e);
|
||||||
@@ -359,49 +343,34 @@ const EventAdmin: React.FC = () => {
|
|||||||
const claimedArray = Array.from(claimed);
|
const claimedArray = Array.from(claimed);
|
||||||
const availableItems = allItems.filter(item => !claimed.has(item));
|
const availableItems = allItems.filter(item => !claimed.has(item));
|
||||||
|
|
||||||
setRsvps(updatedRsvps);
|
|
||||||
setNeededItems(availableItems);
|
setNeededItems(availableItems);
|
||||||
setClaimedItems(claimedArray);
|
setClaimedItems(claimedArray);
|
||||||
setEditDialogOpen(false);
|
setEditDialogOpen(false);
|
||||||
setRsvpToEdit(null);
|
setRsvpToEdit(null);
|
||||||
|
|
||||||
// Verify the update was successful
|
// Verify the update was successful but don't throw error if verification response is empty
|
||||||
const verifyResponse = await axios.get(`/api/events/${slug}/rsvps`, {
|
try {
|
||||||
headers: {
|
const verifyResponse = await axios.get(`/api/events/${slug}/rsvps`, {
|
||||||
'Accept': 'application/json'
|
headers: {
|
||||||
}
|
'Accept': 'application/json'
|
||||||
});
|
}
|
||||||
console.log('Verification response:', verifyResponse.data);
|
|
||||||
|
|
||||||
if (!verifyResponse.data.some((r: RSVP) => r.id === rsvpToEdit.id)) {
|
|
||||||
throw new Error('Update verification failed');
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Failed to update RSVP:', error);
|
|
||||||
if (axios.isAxiosError(error)) {
|
|
||||||
console.error('Server error details:', {
|
|
||||||
status: error.response?.status,
|
|
||||||
statusText: error.response?.statusText,
|
|
||||||
data: error.response?.data,
|
|
||||||
headers: error.response?.headers
|
|
||||||
});
|
});
|
||||||
|
console.log('Verification response:', verifyResponse.data);
|
||||||
if (error.response?.status === 404) {
|
} catch (verifyError) {
|
||||||
setError('This RSVP no longer exists. The page will refresh.');
|
console.warn('Verification request failed, but update may still be successful:', verifyError);
|
||||||
await fetchEventAndRsvps();
|
|
||||||
setEditDialogOpen(false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
throw error;
|
} else {
|
||||||
|
throw new Error(`Unexpected response status: ${response.status}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error in handleEditSubmit:', error);
|
console.error('Error in handleEditSubmit:', error);
|
||||||
setError('Failed to update RSVP. Please try again.');
|
setError('Failed to update RSVP. Please try again.');
|
||||||
// Refresh the data to ensure we're in sync with the server
|
|
||||||
await fetchEventAndRsvps();
|
// Only refresh data if we get a specific error indicating the RSVP doesn't exist
|
||||||
|
if (axios.isAxiosError(error) && error.response?.status === 404) {
|
||||||
|
await fetchEventAndRsvps();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user