Skip to content

Commit 7d60b20

Browse files
committed
Delete user card
1 parent b2ffaef commit 7d60b20

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

frontend/src/api/user.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,11 @@ export function getProfilePicUrl(profilePicFileName : string | null) {
116116

117117
return userServiceClient.getUri({url: `/api/users/uploads/${profilePicFileName}`});
118118
}
119+
120+
121+
122+
export async function deleteUser(id: string) {
123+
const response = await userServiceClient.delete(`/api/users/${id}`)
124+
125+
return response;
126+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { useSelector } from "react-redux"
2+
import { clearUser, selectUser } from "../../../reducers/authSlice"
3+
import { AlertDialog, AlertDialogBody, AlertDialogContent, AlertDialogFooter, AlertDialogHeader, AlertDialogOverlay, Button, Card, CardBody, Heading, Input, useDisclosure, useToast } from "@chakra-ui/react";
4+
import React, { useState } from "react";
5+
import { deleteUser } from "../../../api/user";
6+
import { useDispatch } from "react-redux";
7+
8+
9+
export default function DeleteUserCard() {
10+
const user = useSelector(selectUser);
11+
const [enteredName, setEnteredName] = useState('');
12+
const { isOpen, onOpen, onClose } = useDisclosure()
13+
const cancelRef = React.useRef<HTMLButtonElement>(null)
14+
const [isLoading, setIsLoading] = useState(false);
15+
const dispatch = useDispatch()
16+
const toast = useToast();
17+
18+
const onConfirmDelete = async () => {
19+
try {
20+
setIsLoading(true);
21+
const response = await deleteUser(user!.id)
22+
} catch (error: any) {
23+
toast({
24+
title: 'Deletion failed!',
25+
description: error.message,
26+
status: 'error'
27+
})
28+
} finally {
29+
setIsLoading(false);
30+
onClose();
31+
dispatch(clearUser());
32+
}
33+
34+
}
35+
36+
37+
return (
38+
<>
39+
<Card variant={"elevated"} colorScheme="red" backgroundColor="pink">
40+
<CardBody>
41+
<Button w="100%" colorScheme="red" onClick={onOpen}>
42+
<Heading size='sm'>Delete My Account</Heading>
43+
</Button>
44+
</CardBody>
45+
</Card>
46+
<AlertDialog
47+
isOpen={isOpen}
48+
leastDestructiveRef={cancelRef}
49+
onClose={onClose}
50+
>
51+
<AlertDialogOverlay>
52+
<AlertDialogContent>
53+
<AlertDialogHeader fontSize='lg' fontWeight='bold'>
54+
Delete Account
55+
</AlertDialogHeader>
56+
57+
<AlertDialogBody>
58+
Are you sure? You can't undo this action afterwards.
59+
60+
Enter your username to confirm this action.
61+
<Input type='text'
62+
name='username'
63+
value={enteredName}
64+
onChange={(e) => { setEnteredName(e.target.value) }}
65+
autoComplete={"off"}
66+
>
67+
</Input>
68+
69+
70+
</AlertDialogBody>
71+
72+
<AlertDialogFooter>
73+
<Button ref={cancelRef} onClick={onClose}>
74+
Cancel
75+
</Button>
76+
<Button colorScheme='red' onClick={onConfirmDelete} ml={3}
77+
isDisabled={enteredName !== user?.username} isLoading={isLoading}>
78+
Delete
79+
</Button>
80+
</AlertDialogFooter>
81+
</AlertDialogContent>
82+
</AlertDialogOverlay>
83+
</AlertDialog>
84+
</>
85+
)
86+
87+
88+
89+
}

frontend/src/pages/ProfilePage.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { getUserProfile } from "../api/user";
1111
import PromoteAdminCard from "../components/profile_page/PromoteAdminCard/PromoteAdminCard.component";
1212
import { ProfileProvider } from "../contexts/profileContext";
1313
import ProgressBar from "../components/ProgressBar/ProgressBar.component";
14+
import DeleteUserCard from "../components/profile_page/DeleteUserCard/DeleteUserCard.component";
1415

1516

1617
export default function ProfilePage() {
@@ -49,6 +50,12 @@ export default function ProfilePage() {
4950
: <ChangePasswordCard />
5051
}
5152

53+
{
54+
currUser!.id !== displayedUser!.id
55+
? <></>
56+
: <DeleteUserCard />
57+
}
58+
5259
{
5360
isAdmin
5461
? <PromoteAdminCard displayedUser={displayedUser!} setDisplayedUser={setDisplayedUser} />

0 commit comments

Comments
 (0)