diff --git a/frontend/src/components/rsvp/rsvp-form.tsx b/frontend/src/components/rsvp/rsvp-form.tsx index 318a12a..458c703 100644 --- a/frontend/src/components/rsvp/rsvp-form.tsx +++ b/frontend/src/components/rsvp/rsvp-form.tsx @@ -19,6 +19,7 @@ import { useSearchParams, useParams } from "next/navigation"; import { Loader2, Plus, Minus } from "lucide-react"; import { motion } from "framer-motion"; import { useTheme } from "next-themes"; +import { useRSVPs } from "@/context/rsvp-context"; interface RSVPProps { eventId: string; @@ -32,6 +33,8 @@ export const RSVP: React.FC = ({ eventId, onRSVPSuccess }) => { findGuestByInvitationCode, submitGuestRsvp, } = useGuests(); + + const { rsvps } = useRSVPs(); const searchParams = useSearchParams(); const { slug } = useParams<{ slug: string }>(); const { event, fetchEventBySlug, isLoadingEvent } = useEvents(); @@ -45,6 +48,9 @@ export const RSVP: React.FC = ({ eventId, onRSVPSuccess }) => { const [isProcessing, setIsProcessing] = useState(false); const [error, setError] = useState(null); const [success, setSuccess] = useState(false); + const [currentGuest, setCurrentGuest] = useState(null); + const [currentRSVP, setCurrentRSVP] = useState(null); + const { setTheme } = useTheme(); useEffect(() => { @@ -58,6 +64,21 @@ export const RSVP: React.FC = ({ eventId, onRSVPSuccess }) => { } }, [slug, fetchEventBySlug]); + useEffect(() => { + if (rsvps && currentGuest) { + setCurrentRSVP(rsvps.find((rsvp) => rsvp.guest_id === currentGuest?.id)); + } + }, [rsvps, currentGuest]); + + useEffect(() => { + if (currentRSVP) { + setStatus(currentRSVP.status); + setNumberOfGuests(currentRSVP.number_of_guests); + setResponseMessage(currentRSVP.response_message); + setDietaryRequirements(currentRSVP.dietary_requirements); + } + }, [currentRSVP]); + // Find the theme for this event const eventTheme = event && themes?.find((theme) => theme.id === event.theme_id); @@ -114,7 +135,7 @@ export const RSVP: React.FC = ({ eventId, onRSVPSuccess }) => { // Find the guest with matching invitation code if (guests) { const matchingGuest = findGuestByInvitationCode(invitationCode); - + setCurrentGuest(matchingGuest); if (matchingGuest) { console.log("matchingGuest ", matchingGuest); setGuestId(matchingGuest.id); @@ -248,13 +269,12 @@ export const RSVP: React.FC = ({ eventId, onRSVPSuccess }) => { if (success) { return ( @@ -298,13 +318,12 @@ export const RSVP: React.FC = ({ eventId, onRSVPSuccess }) => { return ( @@ -466,12 +485,15 @@ export const RSVP: React.FC = ({ eventId, onRSVPSuccess }) => {