-
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 (#235)
* fix: add a verification before deleting a user or an ESP * fix: hide popover if click on NON Closes: #230
- Loading branch information
Showing
6 changed files
with
139 additions
and
33 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
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,78 @@ | ||
import React, { 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"; | ||
|
||
export default function DeleteUserData({ | ||
username, | ||
onDelete, | ||
}: { | ||
username: string; | ||
onDelete: (username: string) => void; | ||
}) { | ||
// Function to hide the delete popup | ||
const [isOpen, setIsOpen] = useState(false); | ||
|
||
const hidePopover = () => { | ||
setIsOpen(false); | ||
}; | ||
|
||
const openPopover = () => { | ||
setIsOpen(true); | ||
}; | ||
|
||
// Function to delete a user | ||
const [users, setUsers] = useState<user[]>([]); | ||
|
||
const handleDelete = async () => { | ||
try { | ||
const response = await fetch(`/postgrest/users?username=eq.${username}`, { | ||
method: "DELETE", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${getToken()}`, | ||
}, | ||
}); | ||
|
||
if (!response.ok) { | ||
return; | ||
} | ||
|
||
onDelete(username); | ||
// Remove user from local state after successful deletion | ||
setUsers(users.filter((user) => user.username !== username)); | ||
setIsOpen(false); | ||
} catch (error) { | ||
console.error("Error:", error); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className="flex cursor-pointer gap-2"> | ||
<Popover open={isOpen} onOpenChange={setIsOpen}> | ||
<PopoverTrigger> | ||
<Trash2 /> | ||
</PopoverTrigger> | ||
|
||
<PopoverContent className="mr-5 mt-2 flex w-fit flex-col gap-2 dark:bg-zinc-800"> | ||
<p>Supprimer {username} ?</p> | ||
<Button onClick={handleDelete} className="w-72"> | ||
OUI | ||
</Button> | ||
<Button | ||
onClick={hidePopover} | ||
className="w-72 data-[state=closed]:animate-out" | ||
> | ||
NON | ||
</Button> | ||
</PopoverContent> | ||
</Popover> | ||
</div> | ||
); | ||
} |