import { useState } from 'react'; import { useLingui } from '@lingui/react/macro'; import { Trans } from '@lingui/react/macro'; import { trpc } from '@documenso/trpc/react'; import { Alert, AlertDescription } from '@documenso/ui/primitives/alert'; import { Button } from '@documenso/ui/primitives/button'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from '@documenso/ui/primitives/dialog'; import { useToast } from '@documenso/ui/primitives/use-toast'; export type EnvelopeItemDeleteDialogProps = { canItemBeDeleted: boolean; envelopeId: string; envelopeItemId: string; envelopeItemTitle: string; onDelete?: (envelopeItemId: string) => void; trigger?: React.ReactNode; }; export const EnvelopeItemDeleteDialog = ({ trigger, canItemBeDeleted, envelopeId, envelopeItemId, envelopeItemTitle, onDelete, }: EnvelopeItemDeleteDialogProps) => { const [open, setOpen] = useState(false); const { t } = useLingui(); const { toast } = useToast(); const { mutateAsync: deleteEnvelopeItem, isPending: isDeleting } = trpc.envelope.item.delete.useMutation({ onSuccess: () => { toast({ title: t`Success`, description: t`You have successfully removed this envelope item.`, duration: 5000, }); onDelete?.(envelopeItemId); setOpen(false); }, onError: () => { toast({ title: t`An unknown error occurred`, description: t`We encountered an unknown error while attempting to remove this envelope item. Please try again later.`, variant: 'destructive', duration: 10000, }); }, }); return ( !isDeleting && setOpen(value)}> {trigger} {canItemBeDeleted ? ( Are you sure? You are about to remove the following document and all associated fields {envelopeItemTitle}
) : ( This item cannot be deleted You cannot delete this item because the document has been sent to recipients )}
); };