Skip to content

Commit 70832f9

Browse files
authored
Merge pull request #70 from cheehongw/delete-user
Delete user card
2 parents e4d42bf + 285676c commit 70832f9

File tree

3 files changed

+113
-13
lines changed

3 files changed

+113
-13
lines changed

frontend/src/api/user.ts

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

121121
return apiGatewayClient.getUri({url: `/api/users/uploads/${profilePicFileName}`});
122122
}
123+
124+
125+
126+
export async function deleteUser(id: number) {
127+
const response = await apiGatewayClient.delete(`/api/users/${id}`)
128+
129+
return response;
130+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
</AlertDialogBody>
70+
71+
<AlertDialogFooter>
72+
<Button ref={cancelRef} onClick={onClose}>
73+
Cancel
74+
</Button>
75+
<Button colorScheme='red' onClick={onConfirmDelete} ml={3}
76+
isDisabled={enteredName !== user?.username} isLoading={isLoading}>
77+
Delete
78+
</Button>
79+
</AlertDialogFooter>
80+
</AlertDialogContent>
81+
</AlertDialogOverlay>
82+
</AlertDialog>
83+
</>
84+
)
85+
86+
87+
88+
}

frontend/src/pages/ProfilePage.tsx

Lines changed: 17 additions & 13 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() {
@@ -43,20 +44,23 @@ export default function ProfilePage() {
4344
/>
4445
<FindUserCard />
4546

46-
{currUser!.id !== displayedUser!.id ? (
47-
<></>
48-
) : (
49-
<ChangePasswordCard />
50-
)}
47+
{
48+
currUser!.id !== displayedUser!.id
49+
? <></>
50+
: <ChangePasswordCard />
51+
}
5152

52-
{isAdmin ? (
53-
<PromoteAdminCard
54-
displayedUser={displayedUser!}
55-
setDisplayedUser={setDisplayedUser}
56-
/>
57-
) : (
58-
<></>
59-
)}
53+
{
54+
currUser!.id !== displayedUser!.id
55+
? <></>
56+
: <DeleteUserCard />
57+
}
58+
59+
{
60+
isAdmin
61+
? <PromoteAdminCard displayedUser={displayedUser!} setDisplayedUser={setDisplayedUser} />
62+
: <></>
63+
}
6064
</Flex>
6165
</Box>
6266
<Box w="65%">

0 commit comments

Comments
 (0)