From 53adef2e1565688f16f6b66d3bd6214ee7aa1357 Mon Sep 17 00:00:00 2001 From: Starstrike Date: Thu, 1 May 2025 09:14:26 -0400 Subject: [PATCH] Add robust error handling for RSVP items processing --- frontend/src/components/RSVPForm.tsx | 74 +++++++++++++++++----------- 1 file changed, 44 insertions(+), 30 deletions(-) diff --git a/frontend/src/components/RSVPForm.tsx b/frontend/src/components/RSVPForm.tsx index eea2531..bc8b891 100644 --- a/frontend/src/components/RSVPForm.tsx +++ b/frontend/src/components/RSVPForm.tsx @@ -67,20 +67,29 @@ const RSVPForm: React.FC = () => { // Get all claimed items from existing RSVPs const claimed = new Set(); - rsvpsResponse.data.forEach((rsvp: any) => { - try { - let rsvpItems: string[] = []; - if (typeof rsvp.items_bringing === 'string') { - rsvpItems = JSON.parse(rsvp.items_bringing); - } else if (Array.isArray(rsvp.items_bringing)) { - rsvpItems = rsvp.items_bringing; + if (Array.isArray(rsvpsResponse.data)) { + rsvpsResponse.data.forEach((rsvp: any) => { + try { + let rsvpItems: string[] = []; + if (typeof rsvp.items_bringing === 'string') { + try { + const parsed = JSON.parse(rsvp.items_bringing); + rsvpItems = Array.isArray(parsed) ? parsed : []; + } catch (e) { + console.error('Error parsing items_bringing JSON:', e); + } + } else if (Array.isArray(rsvp.items_bringing)) { + rsvpItems = rsvp.items_bringing; + } + + if (Array.isArray(rsvpItems)) { + rsvpItems.forEach((item: string) => claimed.add(item)); + } + } catch (e) { + console.error('Error processing RSVP items:', e); } - - rsvpItems.forEach((item: string) => claimed.add(item)); - } catch (e) { - console.error('Error processing RSVP items:', e); - } - }); + }); + } // Filter out claimed items from available items const availableItems = items.filter(item => !claimed.has(item)); @@ -160,25 +169,30 @@ const RSVPForm: React.FC = () => { } // Then add items from existing RSVPs - rsvpsResponse.data.forEach((rsvp: { items_bringing: string | string[] }) => { - try { - let rsvpItems: string[] = []; - if (typeof rsvp.items_bringing === 'string') { - try { - rsvpItems = JSON.parse(rsvp.items_bringing); - } catch (e) { - console.error('Error parsing items_bringing JSON:', e); - rsvpItems = []; + if (Array.isArray(rsvpsResponse.data)) { + rsvpsResponse.data.forEach((rsvp: { items_bringing: string | string[] }) => { + try { + let rsvpItems: string[] = []; + if (typeof rsvp.items_bringing === 'string') { + try { + const parsed = JSON.parse(rsvp.items_bringing); + rsvpItems = Array.isArray(parsed) ? parsed : []; + } catch (e) { + console.error('Error parsing items_bringing JSON:', e); + rsvpItems = []; + } + } else if (Array.isArray(rsvp.items_bringing)) { + rsvpItems = rsvp.items_bringing; } - } else if (Array.isArray(rsvp.items_bringing)) { - rsvpItems = rsvp.items_bringing; + + if (Array.isArray(rsvpItems)) { + rsvpItems.forEach((item: string) => claimed.add(item)); + } + } catch (e) { + console.error('Error processing RSVP items:', e); } - - rsvpItems.forEach((item: string) => claimed.add(item)); - } catch (e) { - console.error('Error processing RSVP items:', e); - } - }); + }); + } // Filter out claimed items const availableItems = items.filter(item => !claimed.has(item));