Update guest-context.tsx with new guest rsvp hybrid endpoint
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
"use client";
|
"use client";
|
||||||
import React, { useState, useEffect } from "react";
|
import React, { useState, useEffect } from "react";
|
||||||
import { useRSVPs } from "@/context/rsvp-context";
|
|
||||||
import { useGuests } from "@/context/guest-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";
|
||||||
@@ -23,8 +22,12 @@ interface RSVPProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const RSVP: React.FC<RSVPProps> = ({ eventId, onRSVPSuccess }) => {
|
export const RSVP: React.FC<RSVPProps> = ({ eventId, onRSVPSuccess }) => {
|
||||||
const { createRsvp } = useRSVPs();
|
const {
|
||||||
const { guests, isLoadingGuests } = useGuests();
|
guests,
|
||||||
|
isLoadingGuests,
|
||||||
|
findGuestByInvitationCode,
|
||||||
|
submitGuestRsvp,
|
||||||
|
} = useGuests();
|
||||||
const searchParams = useSearchParams();
|
const searchParams = useSearchParams();
|
||||||
|
|
||||||
const [status, setStatus] = useState<RsvpStatus>(RsvpStatus.ATTENDING);
|
const [status, setStatus] = useState<RsvpStatus>(RsvpStatus.ATTENDING);
|
||||||
@@ -46,19 +49,18 @@ export const RSVP: React.FC<RSVPProps> = ({ eventId, onRSVPSuccess }) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Find the guest with matching invitation code
|
// Find the guest with matching invitation code
|
||||||
if (guests && guests.length > 0) {
|
if (guests) {
|
||||||
const matchingGuest = guests.find(
|
const matchingGuest = findGuestByInvitationCode(invitationCode);
|
||||||
(guest) => guest.invitation_code === invitationCode,
|
|
||||||
);
|
|
||||||
console.debug("matchingGuest", matchingGuest);
|
|
||||||
if (matchingGuest) {
|
if (matchingGuest) {
|
||||||
|
console.log("matchingGuest ", matchingGuest);
|
||||||
setGuestId(matchingGuest.id);
|
setGuestId(matchingGuest.id);
|
||||||
setError(null);
|
setError(null);
|
||||||
} else {
|
} else {
|
||||||
setError("Invalid invitation code. Please check your invitation link.");
|
setError("Invalid invitation code. Please check your invitation link.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [searchParams, guests]);
|
}, [searchParams, guests, findGuestByInvitationCode]);
|
||||||
|
|
||||||
const submitRsvp = async (e: React.FormEvent) => {
|
const submitRsvp = async (e: React.FormEvent) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
@@ -74,7 +76,8 @@ export const RSVP: React.FC<RSVPProps> = ({ eventId, onRSVPSuccess }) => {
|
|||||||
setError(null);
|
setError(null);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await createRsvp({
|
// Use the combined endpoint to update both RSVP and Guest status
|
||||||
|
await submitGuestRsvp(guestId, {
|
||||||
event_id: eventId,
|
event_id: eventId,
|
||||||
guest_id: guestId,
|
guest_id: guestId,
|
||||||
status,
|
status,
|
||||||
|
|||||||
@@ -14,20 +14,22 @@ import {
|
|||||||
RefetchOptions,
|
RefetchOptions,
|
||||||
} from "@tanstack/react-query";
|
} from "@tanstack/react-query";
|
||||||
import {
|
import {
|
||||||
GetGuestsResponse,
|
|
||||||
GuestRead,
|
GuestRead,
|
||||||
GuestCreate,
|
GuestCreate,
|
||||||
GuestUpdate,
|
GuestUpdate,
|
||||||
|
RsvpSchemaCreate,
|
||||||
|
GuestWithRsvpResponse,
|
||||||
} from "@/client/types.gen";
|
} from "@/client/types.gen";
|
||||||
import {
|
import {
|
||||||
getGuestsOptions,
|
getGuestsOptions,
|
||||||
getGuestOptions,
|
getGuestOptions,
|
||||||
|
createGuestRsvpMutation,
|
||||||
} from "@/client/@tanstack/react-query.gen";
|
} from "@/client/@tanstack/react-query.gen";
|
||||||
import { createGuest, updateGuest, deleteGuest } from "@/client";
|
import { createGuest, updateGuest, deleteGuest } from "@/client";
|
||||||
|
|
||||||
// Guest context state
|
// Guest context state
|
||||||
interface GuestsContextState {
|
interface GuestsContextState {
|
||||||
guests: GetGuestsResponse | undefined;
|
guests: GuestRead[] | undefined;
|
||||||
guest: GuestRead | undefined;
|
guest: GuestRead | undefined;
|
||||||
isLoadingGuests: boolean;
|
isLoadingGuests: boolean;
|
||||||
isLoadingGuest: boolean;
|
isLoadingGuest: boolean;
|
||||||
@@ -43,6 +45,15 @@ interface GuestsContextState {
|
|||||||
deleteGuest: (id: string) => Promise<any>;
|
deleteGuest: (id: string) => Promise<any>;
|
||||||
fetchGuestById: (id: string) => void;
|
fetchGuestById: (id: string) => void;
|
||||||
|
|
||||||
|
// New method for submitting RSVPs
|
||||||
|
submitGuestRsvp: (
|
||||||
|
guestId: string,
|
||||||
|
rsvpData: RsvpSchemaCreate,
|
||||||
|
) => Promise<GuestWithRsvpResponse>;
|
||||||
|
|
||||||
|
// Find guest by invitation code
|
||||||
|
findGuestByInvitationCode: (code: string) => GuestRead | undefined;
|
||||||
|
|
||||||
currentGuestId: string | null;
|
currentGuestId: string | null;
|
||||||
setCurrentGuestId: (id: string | null) => void;
|
setCurrentGuestId: (id: string | null) => void;
|
||||||
}
|
}
|
||||||
@@ -68,6 +79,10 @@ const defaultGuestsContextState: GuestsContextState = {
|
|||||||
throw new Error("GuestsProvider is not initialized");
|
throw new Error("GuestsProvider is not initialized");
|
||||||
},
|
},
|
||||||
fetchGuestById: () => {},
|
fetchGuestById: () => {},
|
||||||
|
submitGuestRsvp: async () => {
|
||||||
|
throw new Error("GuestsProvider is not initialized");
|
||||||
|
},
|
||||||
|
findGuestByInvitationCode: () => undefined,
|
||||||
currentGuestId: null,
|
currentGuestId: null,
|
||||||
setCurrentGuestId: () => {},
|
setCurrentGuestId: () => {},
|
||||||
};
|
};
|
||||||
@@ -114,7 +129,16 @@ export const GuestsProvider: React.FC<GuestsProviderProps> = ({ children }) => {
|
|||||||
enabled: !!currentGuestId,
|
enabled: !!currentGuestId,
|
||||||
});
|
});
|
||||||
|
|
||||||
// explicitly fetch guest by id
|
// Find guest by invitation code
|
||||||
|
const findGuestByInvitationCode = useCallback(
|
||||||
|
(code: string): GuestRead | undefined => {
|
||||||
|
if (!guests || !Array.isArray(guests)) return undefined;
|
||||||
|
return guests.find((guest) => guest.invitation_code === code);
|
||||||
|
},
|
||||||
|
[guests],
|
||||||
|
);
|
||||||
|
|
||||||
|
// Explicitly fetch guest by id
|
||||||
const fetchGuestById = useCallback((id: string) => {
|
const fetchGuestById = useCallback((id: string) => {
|
||||||
setCurrentGuestId(id);
|
setCurrentGuestId(id);
|
||||||
}, []);
|
}, []);
|
||||||
@@ -160,6 +184,27 @@ export const GuestsProvider: React.FC<GuestsProviderProps> = ({ children }) => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// RSVP mutation
|
||||||
|
const rsvpMutation = useMutation(createGuestRsvpMutation());
|
||||||
|
|
||||||
|
// Submit RSVP function
|
||||||
|
const submitGuestRsvp = async (
|
||||||
|
guestId: string,
|
||||||
|
rsvpData: RsvpSchemaCreate,
|
||||||
|
): Promise<GuestWithRsvpResponse> => {
|
||||||
|
const result = await rsvpMutation.mutateAsync({
|
||||||
|
path: { guest_id: guestId },
|
||||||
|
body: rsvpData,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Invalidate queries to ensure fresh data
|
||||||
|
queryClient.invalidateQueries({
|
||||||
|
queryKey: getGuestsOptions().queryKey,
|
||||||
|
});
|
||||||
|
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
// Context state value
|
// Context state value
|
||||||
const value: GuestsContextState = {
|
const value: GuestsContextState = {
|
||||||
guests,
|
guests,
|
||||||
@@ -173,6 +218,8 @@ export const GuestsProvider: React.FC<GuestsProviderProps> = ({ children }) => {
|
|||||||
createGuest: createMutation.mutateAsync,
|
createGuest: createMutation.mutateAsync,
|
||||||
updateGuest: (id, data) => updateMutation.mutateAsync({ id, data }),
|
updateGuest: (id, data) => updateMutation.mutateAsync({ id, data }),
|
||||||
deleteGuest: deleteMutation.mutateAsync,
|
deleteGuest: deleteMutation.mutateAsync,
|
||||||
|
submitGuestRsvp,
|
||||||
|
findGuestByInvitationCode,
|
||||||
|
|
||||||
fetchGuestById,
|
fetchGuestById,
|
||||||
currentGuestId,
|
currentGuestId,
|
||||||
|
|||||||
Reference in New Issue
Block a user