|
| 1 | +import { LoaderCircle } from 'lucide-react'; |
| 2 | +import { useState } from 'react'; |
| 3 | +import { Trans, useTranslation } from 'react-i18next'; |
| 4 | +import { toast } from 'sonner'; |
| 5 | + |
| 6 | +import { |
| 7 | + AlertDialog, |
| 8 | + AlertDialogAction, |
| 9 | + AlertDialogCancel, |
| 10 | + AlertDialogContent, |
| 11 | + AlertDialogDescription, |
| 12 | + AlertDialogFooter, |
| 13 | + AlertDialogHeader, |
| 14 | + AlertDialogTitle, |
| 15 | +} from '@/components/ui/alert-dialog'; |
| 16 | +import { http } from '@/lib/request'; |
| 17 | + |
| 18 | +interface IProps { |
| 19 | + open: boolean; |
| 20 | + titleKey?: string; |
| 21 | + descriptionKey?: string; |
| 22 | + targetName: string; |
| 23 | + itemTitle: string; |
| 24 | + deleteUrl: string; |
| 25 | + restoreUrl?: string; |
| 26 | + successMessage?: string; |
| 27 | + successDescription?: string; |
| 28 | + onOpenChange: (open: boolean) => void; |
| 29 | + onSuccess?: () => void; |
| 30 | + onRestoreSuccess?: (response?: any) => void; |
| 31 | + onError?: (error: any) => void; |
| 32 | +} |
| 33 | + |
| 34 | +export default function ConfirmDeleteDialog(props: IProps) { |
| 35 | + const { |
| 36 | + open, |
| 37 | + titleKey, |
| 38 | + targetName, |
| 39 | + descriptionKey, |
| 40 | + itemTitle, |
| 41 | + deleteUrl, |
| 42 | + restoreUrl, |
| 43 | + successMessage, |
| 44 | + successDescription, |
| 45 | + onOpenChange, |
| 46 | + onSuccess, |
| 47 | + onRestoreSuccess, |
| 48 | + onError, |
| 49 | + } = props; |
| 50 | + const { t } = useTranslation(); |
| 51 | + const [loading, setLoading] = useState(false); |
| 52 | + |
| 53 | + const handleCancel = () => { |
| 54 | + onOpenChange(false); |
| 55 | + }; |
| 56 | + |
| 57 | + const handleConfirm = () => { |
| 58 | + setLoading(true); |
| 59 | + http |
| 60 | + .delete(deleteUrl) |
| 61 | + .then(() => { |
| 62 | + onOpenChange(false); |
| 63 | + onSuccess?.(); |
| 64 | + |
| 65 | + // show success message |
| 66 | + const toastOptions: any = { |
| 67 | + description: successDescription, |
| 68 | + }; |
| 69 | + |
| 70 | + // if restore url is provided, add undo function |
| 71 | + if (restoreUrl) { |
| 72 | + toastOptions.action = { |
| 73 | + label: t('undo'), |
| 74 | + onClick: () => { |
| 75 | + http.post(restoreUrl).then(response => { |
| 76 | + if (onRestoreSuccess) { |
| 77 | + onRestoreSuccess(response); |
| 78 | + } else { |
| 79 | + onSuccess?.(); |
| 80 | + } |
| 81 | + }); |
| 82 | + }, |
| 83 | + }; |
| 84 | + } |
| 85 | + |
| 86 | + toast(successMessage, toastOptions); |
| 87 | + }) |
| 88 | + .catch(error => { |
| 89 | + onError?.(error); |
| 90 | + toast.error(t('error.title'), { |
| 91 | + description: error.message || t('request.failed'), |
| 92 | + }); |
| 93 | + }) |
| 94 | + .finally(() => { |
| 95 | + setLoading(false); |
| 96 | + }); |
| 97 | + }; |
| 98 | + |
| 99 | + return ( |
| 100 | + <AlertDialog open={open} onOpenChange={onOpenChange}> |
| 101 | + <AlertDialogContent> |
| 102 | + <AlertDialogHeader> |
| 103 | + <AlertDialogTitle> |
| 104 | + <Trans |
| 105 | + i18nKey={titleKey || 'common.dialog.delete.title'} |
| 106 | + values={{ target_name: targetName }} |
| 107 | + /> |
| 108 | + </AlertDialogTitle> |
| 109 | + <AlertDialogDescription> |
| 110 | + <Trans |
| 111 | + i18nKey={descriptionKey || 'common.dialog.delete.description'} |
| 112 | + values={{ title: itemTitle }} |
| 113 | + components={{ |
| 114 | + strong: <strong className="font-bold" />, |
| 115 | + }} |
| 116 | + /> |
| 117 | + </AlertDialogDescription> |
| 118 | + </AlertDialogHeader> |
| 119 | + <AlertDialogFooter> |
| 120 | + <AlertDialogCancel onClick={handleCancel}> |
| 121 | + {t('cancel')} |
| 122 | + </AlertDialogCancel> |
| 123 | + <AlertDialogAction |
| 124 | + disabled={loading} |
| 125 | + onClick={handleConfirm} |
| 126 | + className="bg-red-500 text-white hover:bg-red-600" |
| 127 | + > |
| 128 | + {loading && <LoaderCircle className="mr-2 h-4 w-4 animate-spin" />} |
| 129 | + {t('delete')} |
| 130 | + </AlertDialogAction> |
| 131 | + </AlertDialogFooter> |
| 132 | + </AlertDialogContent> |
| 133 | + </AlertDialog> |
| 134 | + ); |
| 135 | +} |
0 commit comments