-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add a verification before deleting a user or an ESP
Closes: #230
- Loading branch information
Showing
3 changed files
with
90 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |