Files
sign/apps/web/src/app/(dashboard)/admin/users/[id]/delete-user-dialog.tsx

146 lines
4.1 KiB
TypeScript
Raw Normal View History

2024-03-03 01:55:33 +11:00
'use client';
import { useState } from 'react';
import { useRouter } from 'next/navigation';
2024-08-27 20:34:39 +09:00
import { Trans, msg } from '@lingui/macro';
import { useLingui } from '@lingui/react';
2024-03-03 01:55:33 +11:00
import type { User } from '@documenso/prisma/client';
import { TRPCClientError } from '@documenso/trpc/client';
import { trpc } from '@documenso/trpc/react';
import { Alert, AlertDescription, AlertTitle } 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 { Input } from '@documenso/ui/primitives/input';
import { useToast } from '@documenso/ui/primitives/use-toast';
export type DeleteUserDialogProps = {
className?: string;
user: User;
};
export const DeleteUserDialog = ({ className, user }: DeleteUserDialogProps) => {
2024-08-27 20:34:39 +09:00
const { _ } = useLingui();
const { toast } = useToast();
2024-08-27 20:34:39 +09:00
const router = useRouter();
2024-03-03 01:55:33 +11:00
const [email, setEmail] = useState('');
const { mutateAsync: deleteUser, isLoading: isDeletingUser } =
trpc.admin.deleteUser.useMutation();
const onDeleteAccount = async () => {
try {
await deleteUser({
id: user.id,
});
toast({
2024-08-27 20:34:39 +09:00
title: _(msg`Account deleted`),
description: _(msg`The account has been deleted successfully.`),
2024-03-03 01:55:33 +11:00
duration: 5000,
});
router.push('/admin/users');
} catch (err) {
if (err instanceof TRPCClientError && err.data?.code === 'BAD_REQUEST') {
toast({
2024-08-27 20:34:39 +09:00
title: _(msg`An error occurred`),
2024-03-03 01:55:33 +11:00
description: err.message,
variant: 'destructive',
});
} else {
toast({
2024-08-27 20:34:39 +09:00
title: _(msg`An unknown error occurred`),
2024-03-03 01:55:33 +11:00
variant: 'destructive',
description:
err.message ??
2024-08-27 20:34:39 +09:00
_(
msg`We encountered an unknown error while attempting to delete your account. Please try again later.`,
),
2024-03-03 01:55:33 +11:00
});
}
}
};
return (
<div className={className}>
<Alert
className="flex flex-col items-center justify-between gap-4 p-6 md:flex-row"
2024-03-03 01:55:33 +11:00
variant="neutral"
>
<div>
<AlertTitle>Delete Account</AlertTitle>
<AlertDescription className="mr-2">
2024-08-27 20:34:39 +09:00
<Trans>
Delete the users account and all its contents. This action is irreversible and will
cancel their subscription, so proceed with caution.
</Trans>
2024-03-03 01:55:33 +11:00
</AlertDescription>
</div>
<div className="flex-shrink-0">
<Dialog>
<DialogTrigger asChild>
2024-08-27 20:34:39 +09:00
<Button variant="destructive">
<Trans>Delete Account</Trans>
</Button>
2024-03-03 01:55:33 +11:00
</DialogTrigger>
<DialogContent>
<DialogHeader className="space-y-4">
2024-08-27 20:34:39 +09:00
<DialogTitle>
<Trans>Delete Account</Trans>
</DialogTitle>
2024-03-03 01:55:33 +11:00
<Alert variant="destructive">
<AlertDescription className="selection:bg-red-100">
2024-08-27 20:34:39 +09:00
<Trans>This action is not reversible. Please be certain.</Trans>
2024-03-03 01:55:33 +11:00
</AlertDescription>
</Alert>
</DialogHeader>
<div>
<DialogDescription>
2024-08-27 20:34:39 +09:00
<Trans>
To confirm, please enter the accounts email address <br />({user.email}).
</Trans>
2024-03-03 01:55:33 +11:00
</DialogDescription>
<Input
className="mt-2"
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
/>
</div>
<DialogFooter>
<Button
onClick={onDeleteAccount}
loading={isDeletingUser}
variant="destructive"
disabled={email !== user.email}
>
2024-08-27 20:34:39 +09:00
<Trans>Delete account</Trans>
2024-03-03 01:55:33 +11:00
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</div>
</Alert>
</div>
);
};