Fix guest names handling in RSVP updates to properly maintain array format

This commit is contained in:
Starstrike
2025-05-01 18:00:10 -04:00
parent 2010c583bd
commit 6b3cf70534
2 changed files with 36 additions and 13 deletions

View File

@@ -361,13 +361,19 @@ app.put('/api/events/:slug/rsvps/:id', async (req: Request, res: Response) => {
// Parse guest_names if it's a string // Parse guest_names if it's a string
let parsedGuestNames: string[] = []; let parsedGuestNames: string[] = [];
try { try {
if (typeof guest_names === 'string') { if (typeof guest_names === 'string' && guest_names.includes('[')) {
// If it's a JSON string array
parsedGuestNames = JSON.parse(guest_names); parsedGuestNames = JSON.parse(guest_names);
} else if (typeof guest_names === 'string') {
// If it's a comma-separated string
parsedGuestNames = guest_names.split(',').map(name => name.trim()).filter(name => name);
} else if (Array.isArray(guest_names)) { } else if (Array.isArray(guest_names)) {
parsedGuestNames = guest_names; // If it's already an array
parsedGuestNames = guest_names.filter(name => name && name.trim());
} }
} catch (e) { } catch (e) {
console.error('Error parsing guest_names:', e); console.error('Error parsing guest_names:', e);
parsedGuestNames = [];
} }
// Update the RSVP // Update the RSVP

View File

@@ -247,12 +247,23 @@ const EventAdmin: React.FC = () => {
}); });
} else if (name === 'guest_count') { } else if (name === 'guest_count') {
const newCount = Math.max(0, parseInt(value) || 0); const newCount = Math.max(0, parseInt(value) || 0);
setEditForm(prev => ({ setEditForm(prev => {
...prev, // Create new guest names array preserving existing names
guest_count: newCount, const newGuestNames = [...prev.guest_names];
// Adjust guest_names array size to match new count // If increasing size, add empty strings for new slots
guest_names: prev.guest_names.slice(0, newCount).concat(Array(Math.max(0, newCount - prev.guest_names.length)).fill('')) while (newGuestNames.length < newCount) {
})); newGuestNames.push('');
}
// If decreasing size, truncate the array
while (newGuestNames.length > newCount) {
newGuestNames.pop();
}
return {
...prev,
guest_count: newCount,
guest_names: newGuestNames
};
});
} else { } else {
setEditForm(prev => ({ setEditForm(prev => ({
...prev, ...prev,
@@ -281,13 +292,13 @@ const EventAdmin: React.FC = () => {
bringing_guests: value as 'yes' | 'no', bringing_guests: value as 'yes' | 'no',
// If changing to 'yes', set guest count to 1 and initialize one empty name field // If changing to 'yes', set guest count to 1 and initialize one empty name field
guest_count: value === 'yes' ? 1 : 0, guest_count: value === 'yes' ? 1 : 0,
// Clear guest names if changing to 'no', otherwise keep existing or initialize new // Clear guest names if changing to 'no', otherwise initialize with empty string or keep existing
guest_names: value === 'no' ? [] : (value === 'yes' ? [''] : prev.guest_names) guest_names: value === 'no' ? [] : (value === 'yes' ? [''] : prev.guest_names)
})); }));
} else { } else {
setEditForm((prev: typeof editForm) => ({ setEditForm(prev => ({
...prev, ...prev,
[name as string]: value, [name]: value
})); }));
} }
}; };
@@ -305,13 +316,18 @@ const EventAdmin: React.FC = () => {
if (!rsvpToEdit || !event) return; if (!rsvpToEdit || !event) return;
try { try {
// Filter out empty guest names
const filteredGuestNames = editForm.guest_names
.map(name => name.trim())
.filter(name => name.length > 0);
// Prepare submission data in the exact format the backend expects // Prepare submission data in the exact format the backend expects
const submissionData = { const submissionData = {
name: editForm.name, name: editForm.name,
attending: editForm.attending, attending: editForm.attending,
bringing_guests: editForm.bringing_guests, bringing_guests: editForm.bringing_guests,
guest_count: parseInt(editForm.guest_count.toString(), 10), guest_count: editForm.bringing_guests === 'yes' ? Math.max(1, parseInt(editForm.guest_count.toString(), 10)) : 0,
guest_names: editForm.guest_names.filter(name => name.trim()).join(', '), // Join non-empty names guest_names: filteredGuestNames,
items_bringing: JSON.stringify(editForm.items_bringing), items_bringing: JSON.stringify(editForm.items_bringing),
event_id: event.id event_id: event.id
}; };
@@ -345,6 +361,7 @@ const EventAdmin: React.FC = () => {
const updatedRsvp: RSVP = { const updatedRsvp: RSVP = {
...rsvpToEdit, ...rsvpToEdit,
...submissionData, ...submissionData,
guest_names: filteredGuestNames,
items_bringing: editForm.items_bringing // Keep as array in local state items_bringing: editForm.items_bringing // Keep as array in local state
}; };