All files / components/collections DeleteCollectionDialog.tsx

63.63% Statements 21/33
80% Branches 24/30
66.66% Functions 4/6
64.51% Lines 20/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185                                                                  145x 145x 145x 145x   145x 3x   3x 3x 3x 3x 3x 3x       3x       145x                     145x             145x   89x   89x                                                                                                                                               42x                                 1x                                      
/**
 * Delete collection dialog component
 * Shows confirmation with record count warning
 */
 
import { useState } from 'react';
import {
    AlertDialog,
    AlertDialogContent,
    AlertDialogDescription,
    AlertDialogFooter,
    AlertDialogHeader,
    AlertDialogTitle,
} from '@/components/ui/alert-dialog';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Button } from '@/components/ui/button';
import { CheckCircle2, Loader2 } from 'lucide-react';
import type { CollectionListItem } from '@/services/collections.service';
 
interface DeleteCollectionDialogProps {
    open: boolean;
    onOpenChange: (open: boolean) => void;
    collection: CollectionListItem | null;
    onConfirm: (collectionId: string) => Promise<void>;
}
 
export default function DeleteCollectionDialog({
    open,
    onOpenChange,
    collection,
    onConfirm,
}: DeleteCollectionDialogProps) {
    const [confirmText, setConfirmText] = useState('');
    const [isDeleting, setIsDeleting] = useState(false);
    const [isSuccess, setIsSuccess] = useState(false);
    const [deletedCollectionName, setDeletedCollectionName] = useState('');
 
    const handleConfirm = async () => {
        Iif (!collection || confirmText !== collection.name) return;
 
        setIsDeleting(true);
        try {
            await onConfirm(collection.id);
            setDeletedCollectionName(collection.name);
            setIsSuccess(true);
            setConfirmText('');
        } catch (error) {
            console.error('Failed to delete collection:', error);
        } finally {
            setIsDeleting(false);
        }
    };
 
    const handleOpenChange = (newOpen: boolean) => {
        if (!isDeleting) {
            onOpenChange(newOpen);
            if (!newOpen) {
                setConfirmText('');
                setIsSuccess(false);
                setDeletedCollectionName('');
            }
        }
    };
 
    const handleClose = () => {
        onOpenChange(false);
        setIsSuccess(false);
        setDeletedCollectionName('');
        setConfirmText('');
    };
 
    if (!collection) return null;
 
    const isConfirmValid = confirmText === collection.name;
 
    return (
        <AlertDialog open={open} onOpenChange={handleOpenChange}>
            <AlertDialogContent>
                <AlertDialogHeader>
                    <AlertDialogTitle>
                        {isSuccess ? 'Collection Deleted' : 'Delete Collection'}
                    </AlertDialogTitle>
                    <AlertDialogDescription asChild>
                        <div>
                            {isDeleting && (
                                <div className="flex flex-col items-center justify-center py-8 space-y-4">
                                    <Loader2 className="h-12 w-12 animate-spin text-destructive" />
                                    <div className="text-center">
                                        <p className="font-medium">Deleting collection and applying migrations...</p>
                                        <p className="text-sm text-muted-foreground mt-1">
                                            Please wait while the database table is being dropped.
                                        </p>
                                    </div>
                                </div>
                            )}
 
                            {isSuccess && !isDeleting && (
                                <div className="flex flex-col items-center justify-center py-8 space-y-4">
                                    <div className="rounded-full bg-green-100 dark:bg-green-900/30 p-4">
                                        <CheckCircle2 className="h-12 w-12 text-green-600 dark:text-green-400" />
                                    </div>
                                    <div className="text-center space-y-2">
                                        <p className="font-medium text-lg">
                                            Collection "{deletedCollectionName}" deleted successfully!
                                        </p>
                                        <p className="text-sm text-muted-foreground">
                                            The database table has been dropped and all migrations are complete.
                                        </p>
                                    </div>
                                </div>
                            )}
 
                            {!isSuccess && !isDeleting && (
                                <div className="space-y-4">
                                    <p>
                                        Are you sure you want to delete the collection <strong>{collection.name}</strong>?
                                    </p>
 
                                    {collection.records_count > 0 && (
                                        <div className="bg-destructive/10 border border-destructive/20 rounded-lg p-4">
                                            <p className="text-destructive font-medium">
                                                ⚠️ Warning: This collection contains {collection.records_count} record
                                                {collection.records_count !== 1 ? 's' : ''}
                                            </p>
                                            <p className="text-sm text-muted-foreground mt-2">
                                                All data will be permanently deleted and cannot be recovered.
                                            </p>
                                        </div>
                                    )}
 
                                    <div className="bg-muted/50 rounded-lg p-4 space-y-2 text-sm">
                                        <p className="font-medium">This action will:</p>
                                        <ul className="list-disc list-inside space-y-1 text-muted-foreground">
                                            <li>Drop the physical database table</li>
                                            <li>Delete all {collection.records_count} records</li>
                                            <li>Remove the collection definition</li>
                                            <li>Cannot be undone</li>
                                        </ul>
                                    </div>
 
                                    <div className="space-y-2">
                                        <Label htmlFor="confirm-name">
                                            Type <strong>{collection.name}</strong> to confirm
                                        </Label>
                                        <Input
                                            id="confirm-name"
                                            value={confirmText}
                                            onChange={(e) => setConfirmText(e.target.value)}
                                            placeholder={collection.name}
                                            disabled={isDeleting}
                                        />
                                    </div>
                                </div>
                            )}
                        </div>
                    </AlertDialogDescription>
                </AlertDialogHeader>
                <AlertDialogFooter>
                    {isSuccess ? (
                        <Button onClick={handleClose}>Done</Button>
                    ) : (
                        <>
                            <Button
                                variant="outline"
                                onClick={() => onOpenChange(false)}
                                disabled={isDeleting}
                            >
                                Cancel
                            </Button>
                            <Button
                                variant="destructive"
                                onClick={handleConfirm}
                                disabled={!isConfirmValid || isDeleting}
                            >
                                {isDeleting ? 'Deleting...' : 'Delete Collection'}
                            </Button>
                        </>
                    )}
                </AlertDialogFooter>
            </AlertDialogContent>
        </AlertDialog>
    );
}