Four tabs over three cards was navigation for its own sake, and the two shells disagreed about what a settings screen looks like. It is one grid of cards now, matching the portal: the account card carries changing a password and deleting the account in its footer, appearance is a card with one row, and remote access — an operator's concern, not a personal preference — is a card of its own for a superuser. SettingRow, alert-dialog and UserAvatar come across from the portal, so an account renders the same face in both shells.
90 lines
2.7 KiB
TypeScript
90 lines
2.7 KiB
TypeScript
import { useMutation, useQueryClient } from "@tanstack/react-query"
|
|
import { useState } from "react"
|
|
import { useForm } from "react-hook-form"
|
|
import { UsersService } from "@/client"
|
|
import {
|
|
AlertDialog,
|
|
AlertDialogContent,
|
|
AlertDialogDescription,
|
|
AlertDialogFooter,
|
|
AlertDialogHeader,
|
|
AlertDialogTitle,
|
|
} from "@/components/ui/alert-dialog"
|
|
import { Button } from "@/components/ui/button"
|
|
import { LoadingButton } from "@/components/ui/loading-button"
|
|
import useAuth from "@/hooks/useAuth"
|
|
import useCustomToast from "@/hooks/useCustomToast"
|
|
import { handleError } from "@/utils"
|
|
|
|
const DeleteConfirmation = () => {
|
|
const [isOpen, setIsOpen] = useState(false)
|
|
const queryClient = useQueryClient()
|
|
const { showSuccessToast, showErrorToast } = useCustomToast()
|
|
const { handleSubmit } = useForm()
|
|
const { logout } = useAuth()
|
|
|
|
const mutation = useMutation({
|
|
mutationFn: () => UsersService.deleteUserMe(),
|
|
onSuccess: () => {
|
|
showSuccessToast("Your account has been successfully deleted")
|
|
logout()
|
|
},
|
|
onError: handleError.bind(showErrorToast),
|
|
onSettled: () => {
|
|
queryClient.invalidateQueries({ queryKey: ["currentUser"] })
|
|
},
|
|
})
|
|
|
|
const onSubmit = async () => {
|
|
mutation.mutate()
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<Button
|
|
variant="ghost"
|
|
size="sm"
|
|
className="text-destructive hover:text-destructive"
|
|
onClick={() => setIsOpen(true)}
|
|
>
|
|
Delete account
|
|
</Button>
|
|
<AlertDialog open={isOpen} onOpenChange={setIsOpen}>
|
|
<AlertDialogContent>
|
|
<form onSubmit={handleSubmit(onSubmit)}>
|
|
<AlertDialogHeader>
|
|
<AlertDialogTitle>Confirmation Required</AlertDialogTitle>
|
|
<AlertDialogDescription>
|
|
All your account data will be{" "}
|
|
<strong>permanently deleted.</strong> If you are sure, please
|
|
click <strong>"Confirm"</strong> to proceed. This action cannot
|
|
be undone.
|
|
</AlertDialogDescription>
|
|
</AlertDialogHeader>
|
|
|
|
<AlertDialogFooter className="mt-4">
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
disabled={mutation.isPending}
|
|
onClick={() => setIsOpen(false)}
|
|
>
|
|
Cancel
|
|
</Button>
|
|
<LoadingButton
|
|
type="submit"
|
|
variant="destructive"
|
|
loading={mutation.isPending}
|
|
>
|
|
Delete
|
|
</LoadingButton>
|
|
</AlertDialogFooter>
|
|
</form>
|
|
</AlertDialogContent>
|
|
</AlertDialog>
|
|
</>
|
|
)
|
|
}
|
|
|
|
export default DeleteConfirmation
|