|
| 1 | +import { Alert, Button, Icon, Input, Modal, Spinner, features, useToast, type Tenant } from '@ngo-platform/shared'; |
| 2 | +import { type FC, useEffect, useState } from 'react'; |
| 3 | + |
| 4 | +interface DeleteTenantDialogProps { |
| 5 | + isOpen: boolean; |
| 6 | + onClose: () => void; |
| 7 | + onSuccess: (tenantId: string) => void; |
| 8 | + tenant: Tenant; |
| 9 | +} |
| 10 | + |
| 11 | +export const DeleteTenantDialog: FC<DeleteTenantDialogProps> = ({ |
| 12 | + isOpen, |
| 13 | + onClose, |
| 14 | + onSuccess, |
| 15 | + tenant, |
| 16 | +}) => { |
| 17 | + const { success, error } = useToast(); |
| 18 | + const [confirmName, setConfirmName] = useState(''); |
| 19 | + const [isDeleting, setIsDeleting] = useState(false); |
| 20 | + |
| 21 | + // Reset state when dialog opens/closes |
| 22 | + useEffect(() => { |
| 23 | + if (isOpen) { |
| 24 | + setConfirmName(''); |
| 25 | + } |
| 26 | + }, [isOpen]); |
| 27 | + |
| 28 | + const deleteTenantMutation = features.useDeleteTenant(); |
| 29 | + const isNameMatch = confirmName === tenant.name; |
| 30 | + |
| 31 | + const handleDelete = async () => { |
| 32 | + if (!isNameMatch) return; |
| 33 | + |
| 34 | + setIsDeleting(true); |
| 35 | + try { |
| 36 | + await deleteTenantMutation.mutateAsync(tenant.id); |
| 37 | + success('Tenant deleted', `${tenant.name} has been permanently deleted.`); |
| 38 | + onSuccess(tenant.id); |
| 39 | + onClose(); |
| 40 | + } catch (err) { |
| 41 | + error('Failed to delete tenant', err instanceof Error ? err.message : 'Unknown error occurred'); |
| 42 | + } finally { |
| 43 | + setIsDeleting(false); |
| 44 | + } |
| 45 | + }; |
| 46 | + |
| 47 | + return ( |
| 48 | + <Modal |
| 49 | + isOpen={isOpen} |
| 50 | + onClose={onClose} |
| 51 | + title="Delete Tenant" |
| 52 | + size="lg" |
| 53 | + > |
| 54 | + <div className="space-y-6"> |
| 55 | + <div className="flex items-center gap-2 text-error-600 mb-4"> |
| 56 | + <Icon name="alertTriangle" size={24} /> |
| 57 | + <span className="text-lg font-semibold">Warning: Irreversible Action</span> |
| 58 | + </div> |
| 59 | + <Alert variant="danger" title="Warning: Irreversible Action"> |
| 60 | + <p> |
| 61 | + You are about to permanently delete <strong>{tenant.name}</strong>. |
| 62 | + This action cannot be undone. All associated data, including users, settings, and historical records, will be removed. |
| 63 | + </p> |
| 64 | + </Alert> |
| 65 | + |
| 66 | + <div className="grid grid-cols-1 md:grid-cols-3 gap-4"> |
| 67 | + <div className="p-4 bg-neutral-50 dark:bg-neutral-800 rounded-lg border border-neutral-200 dark:border-neutral-700 flex flex-col items-center justify-center text-center"> |
| 68 | + <Icon name="users" size={24} className="text-neutral-400 mb-2" /> |
| 69 | + <span className="text-2xl font-bold text-neutral-900 dark:text-white"> |
| 70 | + {tenant.userCount ?? 0} |
| 71 | + </span> |
| 72 | + <span className="text-xs text-neutral-500 uppercase tracking-wide">Active Users</span> |
| 73 | + </div> |
| 74 | + <div className="p-4 bg-neutral-50 dark:bg-neutral-800 rounded-lg border border-neutral-200 dark:border-neutral-700 flex flex-col items-center justify-center text-center"> |
| 75 | + {/* Placeholder for tasks until backend supports it */} |
| 76 | + <Icon name="checkCircle" size={24} className="text-neutral-400 mb-2" /> |
| 77 | + <span className="text-2xl font-bold text-neutral-900 dark:text-white">N/A</span> |
| 78 | + <span className="text-xs text-neutral-500 uppercase tracking-wide">Pending Tasks</span> |
| 79 | + </div> |
| 80 | + <div className="p-4 bg-neutral-50 dark:bg-neutral-800 rounded-lg border border-neutral-200 dark:border-neutral-700 flex flex-col items-center justify-center text-center"> |
| 81 | + <Icon name="calendar" size={24} className="text-neutral-400 mb-2" /> |
| 82 | + <span className="text-base font-semibold text-neutral-900 dark:text-white"> |
| 83 | + {new Date(tenant.createdAt).toLocaleDateString()} |
| 84 | + </span> |
| 85 | + <span className="text-xs text-neutral-500 uppercase tracking-wide">Created</span> |
| 86 | + </div> |
| 87 | + </div> |
| 88 | + |
| 89 | + <div className="space-y-3"> |
| 90 | + <label className="block text-sm font-medium text-neutral-700 dark:text-neutral-300"> |
| 91 | + To confirm, type <span className="font-bold select-all">{tenant.name}</span> below: |
| 92 | + </label> |
| 93 | + <div className="relative"> |
| 94 | + <Input |
| 95 | + value={confirmName} |
| 96 | + onChange={(e) => setConfirmName(e.target.value)} |
| 97 | + placeholder={tenant.name} |
| 98 | + className={isNameMatch ? 'border-success-500 focus:border-success-500 focus:ring-success-500' : ''} |
| 99 | + disabled={isDeleting} |
| 100 | + /> |
| 101 | + {isNameMatch && ( |
| 102 | + <div className="absolute right-3 top-1/2 -translate-y-1/2 text-success-500 animate-in fade-in zoom-in duration-200"> |
| 103 | + <Icon name="check" size={18} /> |
| 104 | + </div> |
| 105 | + )} |
| 106 | + </div> |
| 107 | + </div> |
| 108 | + |
| 109 | + <div className="flex justify-end gap-3 pt-2"> |
| 110 | + <Button variant="ghost" onClick={onClose} disabled={isDeleting}> |
| 111 | + Cancel |
| 112 | + </Button> |
| 113 | + <Button |
| 114 | + variant="danger" |
| 115 | + onClick={handleDelete} |
| 116 | + disabled={!isNameMatch || isDeleting} |
| 117 | + className="min-w-[120px]" |
| 118 | + > |
| 119 | + {isDeleting ? <Spinner size="sm" className="mr-2" /> : <Icon name="trash" size={16} className="mr-2" />} |
| 120 | + {isDeleting ? 'Deleting...' : 'Delete Tenant'} |
| 121 | + </Button> |
| 122 | + </div> |
| 123 | + </div> |
| 124 | + </Modal> |
| 125 | + ); |
| 126 | +}; |
0 commit comments