import { CircularProgress, Dialog, DialogActions, DialogContent, DialogTitle } from "@mui/material"; import { useNavigate } from "react-router"; import { Button } from "@/components/ui/button"; import { DialogCancelButton } from "@/components/ui/dialog-cancel-button"; import { Text } from "@/components/ui/text"; import type { CompletionTemplate, LinkedWorkOrders, } from "@/domain/completion-templates/types/completion-template"; import { useDeleteCompletionTemplate } from "@/domain/completion-templates/use-cases/use-completion-template-mutations"; import { useLinkedWorkOrders } from "@/domain/completion-templates/use-cases/use-completion-templates"; import { buildWorkOrderIdsHref } from "@/domain/work-orders/utils/work-order-ids-filter"; type DeleteCompletionTemplateDialogProps = { template: CompletionTemplate; open: boolean; onClose: () => void; onDeleted: () => void; }; function deleteMessage(name: string, linked: LinkedWorkOrders): string { if (linked.count === 0) return `This will permanently remove "${name}". This cannot be undone.`; const plural = linked.count === 1 ? "" : "s"; return ( `This will permanently remove "${name}". It's still linked to ${linked.count} open work ` + `order${plural} through its Service — deleting it won't change that Service, but new ` + "documents generated for those work orders will fall back to a generic procedure until " + "reassigned. This cannot be undone." ); } function LinkedWorkOrdersMessage({ name, linked, }: { name: string; linked: ReturnType; }) { if (linked.isPending) { return (
Checking linked work orders…
); } if (linked.isError) { return ( Couldn't check which work orders use this template. Close this and try again. ); } return {deleteMessage(name, linked.data)}; } /** Deleting always asks first, and names the open work orders that still depend on the template. */ export function DeleteCompletionTemplateDialog({ template, open, onClose, onDeleted, }: DeleteCompletionTemplateDialogProps) { const navigate = useNavigate(); const linked = useLinkedWorkOrders(open ? template.id : null); const remove = useDeleteCompletionTemplate(); const linkedIds = linked.data?.workOrderIds ?? []; function confirmDelete() { remove.mutate(template.id, { onSuccess: onDeleted }); } return ( Delete this template? {linkedIds.length > 0 && ( )} ); }