GeneIT/geneit_app/src/routes/DeleteAccountRoute.tsx

81 lines
2.3 KiB
TypeScript
Raw Normal View History

2023-06-15 07:33:41 +00:00
import { Button, Typography } from "@mui/material";
2023-06-15 07:33:52 +00:00
import React from "react";
2023-06-15 07:33:41 +00:00
import { Link, useLocation, useNavigate } from "react-router-dom";
2023-06-15 07:33:52 +00:00
import { AuthApi } from "../api/AuthApi";
2023-06-15 07:33:41 +00:00
import { DeleteAccountTokenInfo, UserApi } from "../api/UserApi";
import { useAlert } from "../hooks/context_providers/AlertDialogProvider";
2023-06-15 07:33:41 +00:00
import { AsyncWidget } from "../widgets/AsyncWidget";
import { useConfirm } from "../hooks/context_providers/ConfirmDialogProvider";
2023-06-15 07:33:41 +00:00
export function DeleteAccountRoute(): React.ReactElement {
const alert = useAlert();
const confirm = useConfirm();
const navigate = useNavigate();
const { hash } = useLocation();
const token = hash.substring(1);
const [info, setInfo] = React.useState<DeleteAccountTokenInfo>();
const checkToken = async () => {
setInfo(await UserApi.CheckDeleteAccountToken(token));
};
const doDelete = async () => {
try {
if (
2023-07-09 15:35:12 +00:00
!(await confirm(
2023-06-15 07:33:41 +00:00
"Voulez-vous vraiment supprimer votre compte ?",
undefined,
"Supprimer mon compte"
))
)
return;
await UserApi.DeleteAccount(token);
AuthApi.RemoveAuthToken();
2023-07-09 15:35:12 +00:00
await alert("Votre compte a été supprimé avec succès !");
2023-06-15 07:33:41 +00:00
navigate("/");
} catch (e) {
console.error(e);
2023-07-09 15:35:12 +00:00
alert("Echec de la suppression du compte !");
2023-06-15 07:33:41 +00:00
}
};
return (
<AsyncWidget
errMsg="Echec de la validation du jeton de suppression de compte !"
load={checkToken}
loadKey={token}
build={() => (
<div
style={{
height: "100vh",
backgroundColor: "#b23c17",
color: "white",
display: "flex",
justifyContent: "center",
alignItems: "center",
flexDirection: "column",
}}
>
<Typography variant="h3">Suppression de compte</Typography>
<p>{info?.email}</p>
<p>
Voulez-vous vraiment supprimer votre compte ? Cette opération est
irréversible !
</p>
<Button onClick={doDelete} color="inherit">
Supprimer mon compte
</Button>
<Link to="/" style={{ color: "inherit", textDecoration: "none" }}>
<Button color="inherit">Annuler</Button>
</Link>
</div>
)}
/>
);
}