Added ability to delete account

This commit is contained in:
2026-09-27 19:20:01 +02:00
parent 1543af0ccf
commit 2aa56b59c7

View File

@@ -9,8 +9,10 @@ import {
Label, Label,
Typography, Typography,
Spinner, Spinner,
AlertDialog,
useOverlayState,
} from "@heroui/react"; } from "@heroui/react";
import { useState, type Dispatch, type SetStateAction } from "react"; import { useEffect, useState, type Dispatch, type SetStateAction } from "react";
import { useAuth } from "~/hooks/auth"; import { useAuth } from "~/hooks/auth";
import { useNotification } from "~/hooks/notification"; import { useNotification } from "~/hooks/notification";
@@ -21,9 +23,19 @@ export function Settings({
show: boolean; show: boolean;
setShow: Dispatch<SetStateAction<boolean>>; setShow: Dispatch<SetStateAction<boolean>>;
}) { }) {
const { user, update } = useAuth(); const { user, update, logout } = useAuth();
const { notify } = useNotification(); const { notify } = useNotification();
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [canClose, setCanClose] = useState(true);
const deleteState = useOverlayState();
useEffect(() => {
if (!deleteState.isOpen) {
setTimeout(() => {
setCanClose(true);
}, 100);
}
}, [deleteState]);
const notifySucess = () => { const notifySucess = () => {
notify( notify(
@@ -109,12 +121,40 @@ export function Settings({
setLoading(false); setLoading(false);
}; };
const deleteAccount = async () => {
setLoading(true);
let ok = await fetch(`${import.meta.env.VITE_BASE_API_URL}/delete`, {
credentials: "include",
})
.then((r) => r.ok)
.catch((e) => {
notify(
{
type: "danger",
loading: false,
title: "An error occured.",
message: `An error occured: ${e}`,
},
10000,
);
});
if (ok) {
await logout();
notifySucess();
}
setLoading(false);
deleteState.close();
};
return ( return (
<div> <div>
<Modal isOpen={show}> <Modal isOpen={show}>
<Modal.Backdrop <Modal.Backdrop
variant="blur" variant="blur"
shouldCloseOnInteractOutside={(_element) => { shouldCloseOnInteractOutside={(_element) => {
if (!canClose) return false;
setShow(false); setShow(false);
return true; return true;
}} }}
@@ -158,14 +198,32 @@ export function Settings({
image url. image url.
</Description> </Description>
</TextField> </TextField>
<Button
className="w-full"
variant="tertiary"
isPending={loading}
onPress={() => {
loadFromSteam();
}}
>
{loading ? (
<Spinner
color="current"
size="md"
/>
) : (
"Load from Steam"
)}
</Button>
</Fieldset.Group> </Fieldset.Group>
<Fieldset.Actions className="flex justify-end"> <Fieldset.Actions className="flex justify-end">
<div className="flex-1"> <div className="flex-1">
<Button <Button
variant="tertiary" variant="danger"
isPending={loading} isPending={loading}
onPress={() => { onPress={() => {
loadFromSteam(); deleteState.open();
setCanClose(false);
}} }}
> >
{loading ? ( {loading ? (
@@ -174,10 +232,11 @@ export function Settings({
size="md" size="md"
/> />
) : ( ) : (
"Load from Steam" "Delete account"
)} )}
</Button> </Button>
</div> </div>
<Button <Button
variant="secondary" variant="secondary"
onPress={() => { onPress={() => {
@@ -208,6 +267,50 @@ export function Settings({
</Modal.Container> </Modal.Container>
</Modal.Backdrop> </Modal.Backdrop>
</Modal> </Modal>
<AlertDialog>
<AlertDialog.Backdrop
isOpen={deleteState.isOpen}
onOpenChange={deleteState.setOpen}
>
<AlertDialog.Container>
<AlertDialog.Dialog>
<AlertDialog.CloseTrigger />
<AlertDialog.Header>
<AlertDialog.Icon status="danger" />
<AlertDialog.Heading>
Delete account permanently?
</AlertDialog.Heading>
</AlertDialog.Header>
<AlertDialog.Body>
<p>
This will permanently delete your account
and all of its data. This action cannot be
undone.
</p>
</AlertDialog.Body>
<AlertDialog.Footer>
<Button slot="close" variant="tertiary">
{loading ? (
<Spinner color="current" size="md" />
) : (
"Cancel"
)}
</Button>
<Button
onClick={deleteAccount}
variant="danger"
>
{loading ? (
<Spinner color="current" size="md" />
) : (
"Delete account"
)}
</Button>
</AlertDialog.Footer>
</AlertDialog.Dialog>
</AlertDialog.Container>
</AlertDialog.Backdrop>
</AlertDialog>
</div> </div>
); );
} }