Skip to content

Commit

Permalink
fix: add a verification before deleting a user or an ESP
Browse files Browse the repository at this point in the history
Closes: #230
  • Loading branch information
ColinRgm committed Jan 21, 2025
1 parent 2d7a6da commit e326e2c
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 28 deletions.
30 changes: 3 additions & 27 deletions nextjs-interface/src/app/dashboard/users/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from "@/components/ui/card";
import { getToken, user } from "@/lib/context";
import EditUsersData from "@/app/ui/dashboard/EditUsersData";
import DeleteUsersData from "@/app/ui/dashboard/DeleteUsersData";

export default function Page() {
const [users, setUsers] = useState<user[]>([]);
Expand All @@ -27,30 +28,6 @@ export default function Page() {
return <div>Chargement...</div>;
}

// Fonction de suppression d'un utilisateur
const handleDelete = async (username: string) => {
try {
const response = await fetch(`/postgrest/users?username=eq.${username}`, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${getToken()}`,
},
});

if (!response.ok) {
console.error("Erreur à la suppression de l'utilisateur");
console.error(await response.json());
return;
}

// Remove user from local state after successful deletion
setUsers(users.filter((user) => user.username !== username));
} catch (error) {
console.error("Error:", error);
}
};

return (
<>
<Card>
Expand All @@ -74,9 +51,8 @@ export default function Page() {
password={user.password}
/>

<Trash2
className="delete-icon cursor-pointer"
onClick={() => handleDelete(user.username)}
<DeleteUsersData
username={user.username}
/>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion nextjs-interface/src/app/ui/dashboard/AddUserElement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function AddUserElement({
setUsername("");
setPassword("");
} else {
setMessage("Erreur à l'ajout 'un utilisateur. Veuillez réessayer.");
setMessage("Erreur à l'ajout de l'utilisateur. Veuillez réessayer.");
}
} catch (error: any) {
setMessage("Error: " + error.message);
Expand Down
86 changes: 86 additions & 0 deletions nextjs-interface/src/app/ui/dashboard/DeleteUsersData.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { useEffect, useState } from "react";
import { getToken, user } from "@/lib/context";
import {
Popover,
PopoverContent,
PopoverTrigger,
} from "@/components/ui/popover";
import { Trash2 } from "lucide-react";
import { Button } from "@/components/ui/button";
import { useAllUsers } from "@/lib/data";
import {Simulate} from "react-dom/test-utils";
import error = Simulate.error;

export default function DeleteUserData({
username
}: {
username: string;
}) {

const [users, setUsers] = useState<user[]>([]);
const allUsers = useAllUsers();

useEffect(() => {
if (allUsers) {
setUsers(allUsers);
}
}, [allUsers]);

if (!allUsers) {
return <div>Chargement...</div>;
}

// Fonction de suppression d'un utilisateur
const handleDelete = async (username: string) => {
try {
const response = await fetch(`/postgrest/users?username=eq.${username}`, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${getToken()}`,
},
});

if (!response.ok) {

return;
} else {
// window.location.href = '/dashboard/users'
}

// Remove user from local state after successful deletion
setUsers(users.filter((user) => user.username !== username));
} catch (error) {
console.error("Error:", error);
}
};

return (
<div>
<div className="flex cursor-pointer gap-2">
<Popover>
<PopoverTrigger>
<Trash2 />
</PopoverTrigger>

<PopoverContent>
<p>Supprimer cet utilisateur ?</p>
<Button
onClick={async () => {
try {
await handleDelete(username);
} catch (e) {
console.error(e);
}
}}
className="w-72"
>
OUI
</Button>
<Button className="w-72">NON</Button>
</PopoverContent>
</Popover>
</div>
</div>
);
}

0 comments on commit e326e2c

Please sign in to comment.