fix available gifts filter

This commit is contained in:
2025-03-16 21:07:05 +01:00
parent b55ef5387b
commit 0a642581e8

View File

@@ -1,3 +1,4 @@
// app/(public)/invite/[slug]/gifts/page.tsx
"use client";
import React, { useEffect, useState } from "react";
@@ -121,21 +122,23 @@ export default function GiftRegistryPage() {
// Process gifts into available and reserved categories
useEffect(() => {
if (items && guestPurchases.length >= 0) {
if (items && Array.isArray(items)) {
// Get IDs of gifts that this guest has reserved
const reservedGiftIds = new Set(
guestPurchases.map((purchase) => purchase.gift_id),
);
// Available gifts (visible and not fully reserved)
// Available gifts:
// Only filter out gifts that:
// 1. Are not visible (is_visible === false)
// 2. Are already reserved by this guest (in reservedGiftIds)
// 3. Are completely received (is_fully_received === true)
// 4. Have been removed (status === "removed")
const available = items.filter(
(item) =>
item.is_visible &&
!reservedGiftIds.has(item.id) &&
(item.status === "available" ||
(item.quantity_requested &&
item.quantity_received &&
item.quantity_received < item.quantity_requested)),
item.status !== "removed" &&
!item.is_fully_received,
);
// Reserved gifts (this guest has reserved them)
@@ -145,7 +148,6 @@ export default function GiftRegistryPage() {
setReservedGifts(reserved);
}
}, [items, guestPurchases]);
// Format priority for display
const formatPriority = (priority: string) => {
switch (priority) {
@@ -210,13 +212,10 @@ export default function GiftRegistryPage() {
// Confirm reservation handler
const handleConfirmReservation = async () => {
console.debug("Confirm reservation:", currentGuest);
if (!selectedGift || !currentGuest) {
console.error("Missing information ", { selectedGift, currentGuest });
setErrorMessage("Required information missing.");
return;
}
console.log("Reserving: ", { selectedGift, currentGuest });
try {
await reserveItem(
@@ -284,7 +283,7 @@ export default function GiftRegistryPage() {
<div className="container max-w-4xl mx-auto px-4 py-8">
<div className="flex flex-col items-center mb-8">
<h1 className="text-3xl font-bold mb-2 text-center">
Gift Registry 🎁
🎁 Gift Registry 🎁
</h1>
<p className="text-muted-foreground text-center">
Choose a gift from the wishlist to help celebrate Emma's 1st birthday
@@ -394,7 +393,7 @@ export default function GiftRegistryPage() {
<TableHead>Name</TableHead>
<TableHead>Priority</TableHead>
<TableHead>Link</TableHead>
<TableHead>Quantity</TableHead>
{/*<TableHead>Quantity</TableHead>*/}
<TableHead>Description</TableHead>
<TableHead></TableHead>
</TableRow>
@@ -430,7 +429,7 @@ export default function GiftRegistryPage() {
</span>
)}
</TableCell>
<TableCell>{quantity}</TableCell>
{/*<TableCell>{quantity}</TableCell>*/}
<TableCell>{gift.description || ""}</TableCell>
<TableCell>
<Button