-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add types and component for delete confirmation dialog
- Loading branch information
1 parent
0fa94aa
commit 1910e28
Showing
3 changed files
with
159 additions
and
48 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
"use client"; | ||
|
||
import { | ||
AlertDialog, | ||
AlertDialogAction, | ||
AlertDialogCancel, | ||
AlertDialogContent, | ||
AlertDialogDescription, | ||
AlertDialogFooter, | ||
AlertDialogHeader, | ||
AlertDialogTitle, | ||
AlertDialogTrigger, | ||
} from "@/components/ui/alert-dialog"; | ||
import axios from "axios"; | ||
import { toast } from "sonner"; | ||
import type { AxiosErrorWithDetails } from "@/types"; | ||
|
||
const DeleteConfirmition = ({ | ||
trigger, | ||
actionUrl, | ||
item, | ||
}: { | ||
trigger: React.ReactNode; | ||
actionUrl: string; | ||
item: string; | ||
}) => { | ||
async function action() { | ||
try { | ||
await axios.delete(actionUrl, { | ||
withCredentials: true, | ||
}); | ||
toast.success("Successfully deleted"); | ||
window.location.reload(); | ||
} catch (error) { | ||
alert(JSON.stringify(error)); | ||
const message = | ||
(error as AxiosErrorWithDetails)?.response?.data?.detail ?? | ||
"Cannot delete, something went wrong"; | ||
toast(`Failed to delete: ${message}`); | ||
} | ||
} | ||
return ( | ||
<AlertDialog> | ||
<AlertDialogTrigger asChild>{trigger}</AlertDialogTrigger> | ||
<AlertDialogContent> | ||
<AlertDialogHeader> | ||
<AlertDialogTitle>Are you absolutely sure?</AlertDialogTitle> | ||
<AlertDialogDescription> | ||
This action cannot be undone. This will permanently delete your | ||
{item} and remove your data from our servers. | ||
</AlertDialogDescription> | ||
</AlertDialogHeader> | ||
<AlertDialogFooter> | ||
<AlertDialogCancel>Cancel</AlertDialogCancel> | ||
<AlertDialogAction className="bg-red-500" onClick={action}> | ||
Continue | ||
</AlertDialogAction> | ||
</AlertDialogFooter> | ||
</AlertDialogContent> | ||
</AlertDialog> | ||
); | ||
}; | ||
|
||
export default DeleteConfirmition; |
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