import { cn } from '@/lib/utils'; import Image from 'next/image'; // import { // Dialog, // DialogClose, // DialogContent, // DialogDescription, // DialogFooter, // DialogHeader, // DialogTitle, // DialogTrigger, // } from '@/components/ui/dialog'; import { Dispatch, SetStateAction, useState } from 'react'; import axios from 'axios'; import { useMutation } from '@tanstack/react-query'; interface IProps { variant: 'default' | 'selected' | 'disabled'; setSelectedPrize: Dispatch>; className?: string; code: string; title: string; description: string; image: null | string; id: number; } const PrizeCard = ({ variant, setSelectedPrize, code, id, title, description, image, className, }: IProps) => { const [dialogOpen, setDialogOpen] = useState(false); const [dialogTitle, setDialogTitle] = useState('Выберите приз'); const [dialogDescription, setDialogDescription] = useState('Загрузка...'); const [isSuccess, setIsSuccess] = useState(false); // TanStack Query mutation for the API request const choosePrizeMutation = useMutation({ mutationFn: () => axios.post(`https://sms.turkmentv.gov.tm/api/gifts/${id}/choose`, { code, }), onSuccess: () => { setDialogTitle('Успешно'); setDialogDescription('Приз успешно выбран.'); setIsSuccess(true); // Mark as success so we can handle setting the prize when dialog closes }, onError: () => { setDialogTitle('Ошибка'); setDialogDescription('Произошла ошибка, попробуйте еще раз.'); setIsSuccess(false); // Reset on error }, }); const handleDialogOpen = () => { // Reset the dialog to show the loading state setDialogTitle('Загрузка...'); setDialogDescription('Пожалуйста подождите'); setIsSuccess(false); // Reset success state before opening // Trigger the mutation when the dialog opens choosePrizeMutation.mutate(); }; const handleDialogClose = (open: boolean) => { setDialogOpen(open); // Check if the dialog was closed and if the mutation was successful if (!open && isSuccess) { setSelectedPrize(id); // Set the selected prize when the dialog closes after success } }; return (
prize

{title}

{description}

{variant === 'default' ? ( <> {/* DialogTrigger to open the dialog */}
{/* DialogContent that shows loading or response */} {dialogTitle} {dialogDescription} {dialogTitle !== 'Загрузка...' && ( )}
) : variant === 'disabled' ? ( ) : variant === 'selected' ? ( ) : null}
); }; export default PrizeCard;