-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[UI v2] feat: Moves delete deployment confirmation dialog logic to a …
…re-usable hook (#17001)
- Loading branch information
1 parent
0f092b5
commit a02cc8d
Showing
2 changed files
with
50 additions
and
25 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
45 changes: 45 additions & 0 deletions
45
ui-v2/src/components/deployments/use-delete-deployment-confirmation-dialog.ts
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,45 @@ | ||
import { Deployment, useDeleteDeployment } from "@/api/deployments"; | ||
import { useDeleteConfirmationDialog } from "@/components/ui/delete-confirmation-dialog"; | ||
import { useToast } from "@/hooks/use-toast"; | ||
import { getRouteApi } from "@tanstack/react-router"; | ||
|
||
const routeApi = getRouteApi("/deployments/"); | ||
|
||
export const useDeleteDeploymentConfirmationDialog = () => { | ||
const navigate = routeApi.useNavigate(); | ||
const { toast } = useToast(); | ||
const [dialogState, confirmDelete] = useDeleteConfirmationDialog(); | ||
|
||
const { deleteDeployment } = useDeleteDeployment(); | ||
|
||
const handleConfirmDelete = ( | ||
deployment: Deployment, | ||
{ | ||
shouldNavigate = false, | ||
}: { | ||
/** Should navigate back to /deployments */ | ||
shouldNavigate?: boolean; | ||
} = {}, | ||
) => | ||
confirmDelete({ | ||
title: "Delete Deployment", | ||
description: `Are you sure you want to delete ${deployment.name}? This action cannot be undone.`, | ||
onConfirm: () => { | ||
deleteDeployment(deployment.id, { | ||
onSuccess: () => { | ||
toast({ title: "Deployment deleted" }); | ||
if (shouldNavigate) { | ||
void navigate({ to: "/deployments" }); | ||
} | ||
}, | ||
onError: (error) => { | ||
const message = | ||
error.message || "Unknown error while deleting deployment."; | ||
console.error(message); | ||
}, | ||
}); | ||
}, | ||
}); | ||
|
||
return [dialogState, handleConfirmDelete] as const; | ||
}; |