-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add delete account functionality with server action
- Loading branch information
Showing
2 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
"use client"; | ||
|
||
import { useState, type ReactNode } from "react"; | ||
|
||
import { | ||
Dialog, | ||
DialogClose, | ||
DialogContent, | ||
DialogDescription, | ||
DialogFooter, | ||
DialogHeader, | ||
DialogTitle, | ||
DialogTrigger, | ||
} from "@/ui/dialog"; | ||
import { deleteProfile } from "@/server/actions/profile"; | ||
import { toast } from "sonner"; | ||
import { Input } from "@/ui/input"; | ||
import { LoaderIcon } from "lucide-react"; | ||
import { Button } from "@/ui/button"; | ||
|
||
interface DeleteAccountProps { | ||
trigger: ReactNode; | ||
email: string; | ||
} | ||
|
||
const DeleteAccount = (props: DeleteAccountProps) => { | ||
const [confirmEmail, setConfirmEmail] = useState<string>(""); | ||
const [loading, setLoading] = useState<boolean>(false); | ||
|
||
const handleDeleteAccount = async (e: React.FormEvent<HTMLFormElement>) => { | ||
e.preventDefault(); | ||
if (confirmEmail !== props.email) { | ||
toast.error("Email does not match."); | ||
return; | ||
} | ||
setLoading(true); | ||
toast.promise(deleteProfile, { | ||
loading: "Deleting account...", | ||
description: "Your account is being deleted.", | ||
success: () => { | ||
setLoading(false); | ||
return `Your account has been deleted.`; | ||
}, | ||
error: "Failed to delete account. Please try again or contact us.", | ||
}); | ||
}; | ||
|
||
return ( | ||
<Dialog> | ||
<DialogTrigger asChild>{props.trigger}</DialogTrigger> | ||
<DialogContent> | ||
<DialogHeader> | ||
<DialogTitle>Delete Account</DialogTitle> | ||
<DialogDescription> | ||
<span className="text-red-500">This action cannot be undone.</span>{" "} | ||
This will permanently delete your account and remove all links from | ||
our servers. | ||
</DialogDescription> | ||
</DialogHeader> | ||
<form onSubmit={handleDeleteAccount}> | ||
<div className="flex flex-col space-y-3"> | ||
<p className="text-sm"> | ||
To confirm, please type your email address:{" "} | ||
<span className="font-mono">{props.email}</span> | ||
</p> | ||
<Input | ||
type="email" | ||
className="input" | ||
onChange={(e) => setConfirmEmail(e.target.value)} | ||
placeholder="Your email address" | ||
disabled={loading} | ||
/> | ||
<DialogFooter className="mt-3"> | ||
<DialogClose asChild> | ||
<Button variant="ghost" disabled={loading}> | ||
Cancel | ||
</Button> | ||
</DialogClose> | ||
<Button | ||
disabled={loading || confirmEmail !== props.email} | ||
type="submit" | ||
variant="destructive" | ||
> | ||
{loading ?? <LoaderIcon size={16} className="animate-spin" />} | ||
<span>{loading ? "Deleting..." : "Delete"}</span> | ||
</Button> | ||
</DialogFooter> | ||
</div> | ||
</form> | ||
</DialogContent> | ||
</Dialog> | ||
); | ||
}; | ||
|
||
export default DeleteAccount; |