Skip to content

Commit 2db6ce6

Browse files
authored
Merge pull request #172 from CS3219-AY2425S1/feature/user-management/delete-confirmation
Add delete confirmation to user management
2 parents b112463 + b0eca14 commit 2db6ce6

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

frontend/components/admin-user-management/admin-user-management.tsx

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
} from "@/components/ui/table";
1515
import LoadingScreen from "@/components/common/loading-screen";
1616
import AdminEditUserModal from "@/components/admin-user-management/admin-edit-user-modal";
17+
import DeleteAccountModal from "@/components/common/delete-account-modal";
1718
import { PencilIcon, Trash2Icon } from "lucide-react";
1819
import { User, UserArraySchema } from "@/lib/schemas/user-schema";
1920
import { userServiceUri } from "@/lib/api-uri";
@@ -51,25 +52,33 @@ export default function AdminUserManagement() {
5152
const [users, setUsers] = useState<User[]>([]);
5253
const [showModal, setShowModal] = useState<boolean>(false);
5354
const [selectedUser, setSelectedUser] = useState<User>();
55+
const [showDeleteModal, setShowDeleteModal] = useState(false);
56+
const [confirmUsername, setConfirmUsername] = useState("");
57+
const [isDeleteButtonEnabled, setIsDeleteButtonEnabled] = useState(false);
5458

5559
useEffect(() => {
5660
if (data) {
5761
setUsers(data);
5862
}
5963
}, [data]);
6064

65+
// Enable delete button in the delete account modal only when the input username matches the original username
66+
useEffect(() => {
67+
setIsDeleteButtonEnabled(confirmUsername === selectedUser?.username);
68+
}, [confirmUsername, selectedUser]);
69+
6170
if (isLoading) {
6271
return <LoadingScreen />;
6372
}
6473

65-
const handleDelete = async (userId: string) => {
74+
const handleDelete = async () => {
6675
const token = auth?.token;
6776
if (!token) {
6877
throw new Error("No authentication token found");
6978
}
7079

7180
const response = await fetch(
72-
`${userServiceUri(window.location.hostname)}/users/${userId}`,
81+
`${userServiceUri(window.location.hostname)}/users/${selectedUser?.id}`,
7382
{
7483
method: "DELETE",
7584
headers: {
@@ -82,7 +91,7 @@ export default function AdminUserManagement() {
8291
throw new Error("Failed to delete user");
8392
}
8493

85-
setUsers(users.filter((user) => user.id !== userId));
94+
setUsers(users.filter((user) => user.id !== selectedUser?.id));
8695
};
8796

8897
const onUserUpdate = () => {
@@ -100,6 +109,16 @@ export default function AdminUserManagement() {
100109
user={selectedUser}
101110
onUserUpdate={onUserUpdate}
102111
/>
112+
<DeleteAccountModal
113+
showDeleteModal={showDeleteModal}
114+
originalUsername={selectedUser?.username || ""}
115+
confirmUsername={confirmUsername}
116+
setConfirmUsername={setConfirmUsername}
117+
handleDeleteAccount={handleDelete}
118+
isDeleteButtonEnabled={isDeleteButtonEnabled}
119+
setShowDeleteModal={setShowDeleteModal}
120+
isAdmin={true}
121+
/>
103122
<Table>
104123
<TableHeader>
105124
<TableRow>
@@ -130,7 +149,10 @@ export default function AdminUserManagement() {
130149
</Button>
131150
<Button
132151
variant="destructive"
133-
onClick={() => handleDelete(user.id)}
152+
onClick={() => {
153+
setSelectedUser(user);
154+
setShowDeleteModal(true);
155+
}}
134156
>
135157
<Trash2Icon className="h-4 w-4" />
136158
</Button>

frontend/components/user-settings/delete-account-modal.tsx renamed to frontend/components/common/delete-account-modal.tsx

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ interface DeleteAccountModalProps {
1717
handleDeleteAccount: () => void;
1818
isDeleteButtonEnabled: boolean;
1919
setShowDeleteModal: (show: boolean) => void;
20+
isAdmin: boolean;
2021
}
2122

2223
const DeleteAccountModal: React.FC<DeleteAccountModalProps> = ({
@@ -27,6 +28,7 @@ const DeleteAccountModal: React.FC<DeleteAccountModalProps> = ({
2728
handleDeleteAccount,
2829
isDeleteButtonEnabled,
2930
setShowDeleteModal,
31+
isAdmin,
3032
}) => {
3133
return (
3234
<>
@@ -37,10 +39,18 @@ const DeleteAccountModal: React.FC<DeleteAccountModalProps> = ({
3739
<DialogTitle>Confirm Delete Account</DialogTitle>
3840
</DialogHeader>
3941
<div className="space-y-4">
40-
<p>To confirm, please type your username ({originalUsername}):</p>
42+
{isAdmin ? (
43+
<p>
44+
To delete, please confirm the username ({originalUsername}):
45+
</p>
46+
) : (
47+
<p>
48+
To confirm, please type your username ({originalUsername}):
49+
</p>
50+
)}
4151
<Input
4252
type="text"
43-
placeholder="Enter your username"
53+
placeholder="Confirm username"
4454
value={confirmUsername}
4555
onChange={(e) => setConfirmUsername(e.target.value)}
4656
/>
@@ -57,7 +67,11 @@ const DeleteAccountModal: React.FC<DeleteAccountModalProps> = ({
5767
</Button>
5868
<Button
5969
variant="destructive"
60-
onClick={handleDeleteAccount}
70+
onClick={() => {
71+
setShowDeleteModal(false);
72+
setConfirmUsername("");
73+
handleDeleteAccount();
74+
}}
6175
disabled={!isDeleteButtonEnabled}
6276
>
6377
Delete Account

frontend/components/user-settings/user-settings.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
1919
import { AlertCircle } from "lucide-react";
2020
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
21-
import DeleteAccountModal from "@/components/user-settings/delete-account-modal";
21+
import DeleteAccountModal from "@/components/common/delete-account-modal";
2222
import ProfileTab from "@/components/user-settings/profile-tab";
2323
import LoadingScreen from "@/components/common/loading-screen";
2424
import { useAuth } from "@/app/auth/auth-context";
@@ -461,6 +461,7 @@ export default function UserSettings({ userId }: { userId: string }) {
461461
handleDeleteAccount={handleDeleteAccount}
462462
isDeleteButtonEnabled={isDeleteButtonEnabled}
463463
setShowDeleteModal={setShowDeleteModal}
464+
isAdmin={false}
464465
/>
465466

466467
<Card className="mt-4">

0 commit comments

Comments
 (0)