update rsvp-form handling the guest
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
"use client";
|
"use client";
|
||||||
import React, { useState } from "react";
|
import React, { useState, useEffect } from "react";
|
||||||
import { useRSVPs } from "@/context/rsvp-context";
|
import { useRSVPs } from "@/context/rsvp-context";
|
||||||
|
import { useGuests } from "@/context/guest-context";
|
||||||
import { Label } from "@/components/ui/label";
|
import { Label } from "@/components/ui/label";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
import { Input } from "@/components/ui/input";
|
import { Input } from "@/components/ui/input";
|
||||||
@@ -13,42 +14,119 @@ import {
|
|||||||
} from "@/components/ui/select";
|
} from "@/components/ui/select";
|
||||||
import { Textarea } from "@/components/ui/textarea";
|
import { Textarea } from "@/components/ui/textarea";
|
||||||
import { RsvpStatus } from "@/client/types.gen";
|
import { RsvpStatus } from "@/client/types.gen";
|
||||||
|
import { useSearchParams } from "next/navigation";
|
||||||
|
import { Loader2 } from "lucide-react";
|
||||||
|
|
||||||
interface RSVPProps {
|
interface RSVPProps {
|
||||||
eventId: string;
|
eventId: string;
|
||||||
guestId: string;
|
|
||||||
onRSVPSuccess?: () => void;
|
onRSVPSuccess?: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const RSVP: React.FC<RSVPProps> = ({
|
export const RSVP: React.FC<RSVPProps> = ({ eventId, onRSVPSuccess }) => {
|
||||||
eventId,
|
|
||||||
guestId,
|
|
||||||
onRSVPSuccess,
|
|
||||||
}) => {
|
|
||||||
const { createRsvp } = useRSVPs();
|
const { createRsvp } = useRSVPs();
|
||||||
|
const { guests, isLoadingGuests } = useGuests();
|
||||||
|
const searchParams = useSearchParams();
|
||||||
|
|
||||||
const [status, setStatus] = useState<RsvpStatus>(RsvpStatus.ATTENDING);
|
const [status, setStatus] = useState<RsvpStatus>(RsvpStatus.ATTENDING);
|
||||||
const [number_of_guests, setNumberOfGuests] = useState<number>(1);
|
const [number_of_guests, setNumberOfGuests] = useState<number>(1);
|
||||||
const [response_message, setResponseMessage] = useState("");
|
const [response_message, setResponseMessage] = useState("");
|
||||||
const [dietary_requirements, setDietaryRequirements] = useState("");
|
const [dietary_requirements, setDietaryRequirements] = useState("");
|
||||||
|
const [guestId, setGuestId] = useState<string | null>(null);
|
||||||
|
const [isProcessing, setIsProcessing] = useState(false);
|
||||||
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
|
||||||
|
// Extract invitation code from URL parameters
|
||||||
|
useEffect(() => {
|
||||||
|
const invitationCode = searchParams.get("code");
|
||||||
|
if (!invitationCode) {
|
||||||
|
setError(
|
||||||
|
"Missing invitation code. Please use the link provided in your invitation.",
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find the guest with matching invitation code
|
||||||
|
if (guests && guests.length > 0) {
|
||||||
|
const matchingGuest = guests.find(
|
||||||
|
(guest) => guest.invitation_code === invitationCode,
|
||||||
|
);
|
||||||
|
console.debug("matchingGuest", matchingGuest);
|
||||||
|
if (matchingGuest) {
|
||||||
|
setGuestId(matchingGuest.id);
|
||||||
|
setError(null);
|
||||||
|
} else {
|
||||||
|
setError("Invalid invitation code. Please check your invitation link.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [searchParams, guests]);
|
||||||
|
|
||||||
const submitRsvp = async (e: React.FormEvent) => {
|
const submitRsvp = async (e: React.FormEvent) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
await createRsvp({
|
if (!guestId) {
|
||||||
event_id: eventId,
|
setError(
|
||||||
guest_id: guestId,
|
"Cannot identify your invitation. Please check your link or contact the host.",
|
||||||
status,
|
);
|
||||||
number_of_guests,
|
return;
|
||||||
response_message,
|
}
|
||||||
dietary_requirements,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (onRSVPSuccess) {
|
setIsProcessing(true);
|
||||||
onRSVPSuccess();
|
setError(null);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await createRsvp({
|
||||||
|
event_id: eventId,
|
||||||
|
guest_id: guestId,
|
||||||
|
status,
|
||||||
|
number_of_guests,
|
||||||
|
response_message,
|
||||||
|
dietary_requirements,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (onRSVPSuccess) {
|
||||||
|
onRSVPSuccess();
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error("Error submitting RSVP:", err);
|
||||||
|
setError(
|
||||||
|
"Failed to submit your RSVP. Please try again or contact the host.",
|
||||||
|
);
|
||||||
|
} finally {
|
||||||
|
setIsProcessing(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Show loading state while checking invitation code
|
||||||
|
if (isLoadingGuests) {
|
||||||
|
return (
|
||||||
|
<div className="w-full bg-card text-card-foreground rounded-lg border shadow-sm p-6 flex justify-center items-center min-h-[300px]">
|
||||||
|
<Loader2 className="h-8 w-8 animate-spin text-primary" />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Show error if no valid invitation code
|
||||||
|
if (error) {
|
||||||
|
return (
|
||||||
|
<div className="w-full bg-card text-card-foreground rounded-lg border shadow-sm">
|
||||||
|
<div className="p-6 flex flex-col gap-6">
|
||||||
|
<div>
|
||||||
|
<h2 className="text-lg font-medium text-destructive">
|
||||||
|
Invitation Error
|
||||||
|
</h2>
|
||||||
|
<p className="mt-2 text-muted-foreground">{error}</p>
|
||||||
|
</div>
|
||||||
|
<Button
|
||||||
|
onClick={() => (window.location.href = "/")}
|
||||||
|
className="w-full md:w-auto self-end"
|
||||||
|
>
|
||||||
|
Return Home
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="w-full bg-card text-card-foreground rounded-lg border shadow-sm">
|
<div className="w-full bg-card text-card-foreground rounded-lg border shadow-sm">
|
||||||
<div className="p-6 flex flex-col gap-6">
|
<div className="p-6 flex flex-col gap-6">
|
||||||
@@ -123,8 +201,19 @@ export const RSVP: React.FC<RSVPProps> = ({
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="flex justify-end mt-6">
|
<div className="flex justify-end mt-6">
|
||||||
<Button type="submit" className="w-full md:w-auto">
|
<Button
|
||||||
Submit RSVP
|
type="submit"
|
||||||
|
className="w-full md:w-auto"
|
||||||
|
disabled={isProcessing}
|
||||||
|
>
|
||||||
|
{isProcessing ? (
|
||||||
|
<>
|
||||||
|
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
|
||||||
|
Submitting...
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
"Submit RSVP"
|
||||||
|
)}
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|||||||
Reference in New Issue
Block a user