Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions components/GiftExchangeHeader/GiftExchangeHeader.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,17 @@ describe('GiftExchangeHeader owner permissions', () => {
})
});

it('should disable the draw gift exchange button when the gift exchange is being drawn', async () => {

render(
<GiftExchangeHeader
giftExchangeData={mockGiftExchangeDataPending}
members={mockMembersData}
id={mockGiftExchangeDataPending.id}
/>
);
});

it('should render the complete gift exchange button when user_id and owner_id match', async () => {

render(
Expand Down
36 changes: 32 additions & 4 deletions components/GiftExchangeHeader/GiftExchangeHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ import { Button } from '@/components/Button/button';
import LinkCustom from '../LinkCustom/LinkCustom';
import Image from 'next/image';
import { GROUP_IMAGES } from '@/components/ImageSelector/ImageSelector';
import { LoadingSpinner } from '../LoadingSpinner/LoadingSpinner';
import { useToast } from '@/hooks/use-toast';
import { ToastVariants } from '../Toast/Toast.enum';
// initialize type for exchange data response

interface MembersListProps {
Expand Down Expand Up @@ -59,6 +62,8 @@ export const GiftExchangeHeader = ({
id,
}: GiftExchangeHeaderPropsUnion): JSX.Element => {
const [membersData, setMembersData] = useState(members);
const [isDrawing, setIsDrawing] = useState(false);
const { toast } = useToast();

useEffect(() => {
setMembersData(members);
Expand Down Expand Up @@ -128,6 +133,14 @@ export const GiftExchangeHeader = ({
* @returns {Promise<void>}
*/
const call = async (): Promise<void> => {
setIsDrawing(true);

toast({
variant: ToastVariants.Success,
title: '',
description: 'Please keep this browser open until our elves complete the gift drawing.',
});

try {
const response = await fetch(`/api/gift-exchanges/${id}/draw`, {
method: 'POST',
Expand All @@ -148,6 +161,13 @@ export const GiftExchangeHeader = ({
location.reload();
} catch (error) {
console.error('Failed to draw gift exchange:', error);
toast({
variant: ToastVariants.Error,
title: 'Draw Failed',
description: 'Failed to complete the gift drawing. Please try again.',
});
} finally {
setIsDrawing(false);
}
};

Expand Down Expand Up @@ -223,16 +243,24 @@ export const GiftExchangeHeader = ({
<p className="text-xs">{giftExchangeData.description}</p>
</div>
<div>
{getStatusText(giftExchangeData.status) === 'Active' && isOwner && (
<Button onClick={completeGiftExchange} data-testid='complete-gift-exchange'>
{getStatusText(giftExchangeData.status) === 'Active' &&
isOwner && (
<Button
onClick={completeGiftExchange}
data-testid="complete-gift-exchange"
>
Complete Gift Exchange
</Button>
)}
{getStatusText(giftExchangeData.status) === 'Open' && isOwner ? (
<AlertDialog>
<AlertDialogTrigger asChild>
<Button disabled={membersData.length <= 2} data-testid='draw-gift-exchange'>
Draw Gift Exchange
<Button
disabled={membersData.length <= 2 || isDrawing}
data-testid="draw-gift-exchange"
className="min-w-40"
>
{isDrawing ? <LoadingSpinner /> : 'Draw Gift Exchange'}
</Button>
</AlertDialogTrigger>
{membersData.length <= 2 && (
Expand Down
6 changes: 2 additions & 4 deletions lib/drawGiftExchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,13 @@ export async function drawGiftExchange(
// Fire and forget suggestions with error handling
// hacky way to avoid waiting for all suggestions to be generated
// avoids timeout issues
generateAndStoreSuggestions(
await generateAndStoreSuggestions(
supabase,
exchangeId,
giver.user_id,
recipient.user_id,
exchange.budget,
).catch((error) => {
throw new SupabaseError('Failed to generate suggestions', 500, error);
});
);
}

// Update exchange status to active
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "elecretanta",
"version": "0.8.2-alpha",
"version": "0.8.3-alpha",
"private": true,
"scripts": {
"dev": "cross-env PORT=4000 next dev",
Expand Down