//
// Copyright (c) 2025 rustmailer.com (https://rustmailer.com)
//
// This file is part of the Bichon Email Archiving Project
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see .
import { useState } from 'react'
import { IconAlertCircle, IconAlertTriangle } from '@tabler/icons-react'
import { toast } from '@/hooks/use-toast'
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import { ConfirmDialog } from '@/components/confirm-dialog'
import { useMutation, useQueryClient } from '@tanstack/react-query'
import { ToastAction } from '@/components/ui/toast'
import { AxiosError } from 'axios'
import { AccountModel, remove_account } from '@/api/account/api'
import { useTranslation } from 'react-i18next'
interface Props {
open: boolean
onOpenChange: (open: boolean) => void
currentRow: AccountModel
}
export function AccountDeleteDialog({ open, onOpenChange, currentRow }: Props) {
const { t } = useTranslation()
const [value, setValue] = useState('')
const queryClient = useQueryClient();
function handleSuccess() {
toast({
title: t('dialogs.accountDeleted'),
description: t('dialogs.accountDeletedDesc'),
action: {t('common.close')},
});
queryClient.invalidateQueries({ queryKey: ['account-list'] });
onOpenChange(false);
}
function handleError(error: AxiosError) {
const errorMessage = (error.response?.data as { message?: string })?.message ||
error.message ||
t('dialogs.deleteFailed');
toast({
variant: "destructive",
title: t('dialogs.accountDeleteFailed'),
description: errorMessage as string,
action: {t('common.tryAgain')},
});
console.error(error);
}
const deleteMutation = useMutation({
mutationFn: (id: number) => remove_account(id),
onSuccess: handleSuccess,
onError: handleError
})
const handleDelete = () => {
if (value.trim() !== currentRow.email) return
deleteMutation.mutate(currentRow.id)
}
return (
{' '}
{t('dialogs.deleteAccountPermanently')}
}
desc={