diff --git a/frontend/src/app/(main)/dashboard/events/[slug]/gifts/page.tsx b/frontend/src/app/(main)/dashboard/events/[slug]/gifts/page.tsx index 887059a..c5444d5 100644 --- a/frontend/src/app/(main)/dashboard/events/[slug]/gifts/page.tsx +++ b/frontend/src/app/(main)/dashboard/events/[slug]/gifts/page.tsx @@ -44,7 +44,7 @@ import { SelectTrigger, SelectValue, } from "@/components/ui/select"; -import { GiftPriority, GiftStatus } from "@/client/types.gen"; +import { GiftPriority, GiftPurchase, GiftStatus } from "@/client/types.gen"; import { CategoryModal } from "@/components/gifts/category-modal"; import { GiftModal } from "@/components/gifts/gift-modal"; import { @@ -52,6 +52,7 @@ import { PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; +import { useGuests } from "@/context/guest-context"; export default function GiftRegistryPage() { const { slug } = useParams<{ slug: string }>(); @@ -68,8 +69,10 @@ export default function GiftRegistryPage() { currentEventId, setCurrentEventId, deleteItem, + fetchGuestsGiftPurchases, } = useGifts(); + const { guests } = useGuests(); // State for modals const [isAddGiftModalOpen, setIsAddGiftModalOpen] = useState(false); const [isEditGiftModalOpen, setIsEditGiftModalOpen] = useState(false); @@ -84,6 +87,39 @@ export default function GiftRegistryPage() { const [searchQuery, setSearchQuery] = useState(""); const [categoryFilter, setCategoryFilter] = useState("all"); const [statusFilter, setStatusFilter] = useState("all"); + const [reservations, setReservations] = useState< + Record + >({}); + const [loadingReservations, setLoadingReservations] = useState(true); + + useEffect(() => { + const loadReservations = async () => { + if (currentEventId) { + setLoadingReservations(true); + try { + const data = await fetchGuestsGiftPurchases(currentEventId); + if (data) { + const groupedReservations = data.reduce( + (acc, purchase) => { + const giftId = purchase.gift_id; + if (!acc[giftId]) acc[giftId] = []; + acc[giftId].push(purchase); + return acc; + }, + {} as Record, + ); + setReservations(groupedReservations); + } + } catch (err) { + console.error("Unable to fetch reservations:", err); + } finally { + setLoadingReservations(false); + } + } + }; + + loadReservations(); + }, [currentEventId, fetchGuestsGiftPurchases]); // Filter items based on search query and filters const filteredItems = items @@ -436,20 +472,44 @@ export default function GiftRegistryPage() { {getStatusBadge(item.status)} - {/**/} - {/* {item.reservations &&*/} - {/* item.reservations.length > 0 ? (*/} - {/*
    */} - {/* {item.reservations.map((res) => (*/} - {/*
  • */} - {/* {res.guest_name}: {res.quantity}*/} - {/*
  • */} - {/* ))}*/} - {/*
*/} - {/* ) : (*/} - {/*

No reservations available.

*/} - {/* )}*/} - {/*
*/} + + {loadingReservations ? ( +
+ + + Loading reservations... + +
+ ) : reservations[item.id] && + reservations[item.id].length > 0 ? ( +
    + {reservations[item.id].map( + (purchase, index) => { + const guest = guests?.find( + (g) => g.id === purchase.guest_id, + ); + + return ( +
  • + + {guest?.full_name || + purchase.guest_id} + + : {purchase.quantity} +
  • + ); + }, + )} +
+ ) : ( +

+ No reservations available. +

+ )} +
) : ( getStatusBadge(item.status) diff --git a/frontend/src/context/gift-context.tsx b/frontend/src/context/gift-context.tsx index 22e1e44..bcfc2cd 100644 --- a/frontend/src/context/gift-context.tsx +++ b/frontend/src/context/gift-context.tsx @@ -24,6 +24,7 @@ import { readGiftPurchase, readGiftPurchasesByGift, readGiftPurchasesByGuest, + readGuestsGiftReservations, } from "@/client/sdk.gen"; import { GiftCategory, @@ -48,6 +49,10 @@ interface GiftContextState { refetchCategories: (eventId: string) => Promise; fetchCategoryById: (id: string, eventId?: string) => void; + fetchGuestsGiftPurchases: ( + eventId: string, + ) => Promise; + createCategory: ( data: GiftCategoryCreate, ) => Promise; @@ -147,6 +152,8 @@ const defaultGiftContextState: GiftContextState = { }, fetchCategoryById: () => {}, + fetchGuestsGiftPurchases: async () => undefined, + createCategory: async () => { throw new Error("GiftContext not initialized"); }, @@ -310,6 +317,20 @@ export const GiftProvider: React.FC = ({ children }) => { } }; + const fetchGuestsGiftPurchases = async ( + eventId: string, + ): Promise => { + try { + const result = await readGuestsGiftReservations({ + query: { event_id: eventId }, + }); + return result.data; + } catch (error) { + console.error("Error fetching guests' gift purchases:", error); + throw error; + } + }; + // Create Category Mutation const createCategoryMutation = useMutation({ mutationFn: (data: GiftCategoryCreate) => @@ -771,7 +792,7 @@ export const GiftProvider: React.FC = ({ children }) => { isLoadingCategories, isLoadingCategory, refetchCategories, - + fetchGuestsGiftPurchases, fetchCategoryById, createCategory: createCategoryMutation.mutateAsync, updateCategory: (id, data, eventId) =>