import React, { useState } from "react"; import { useNavigate, useParams } from "react-router-dom"; import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; import { format, parseISO } from "date-fns"; import { toast } from "@/lib/custom-toast"; import { Calendar, Edit, Trash } from "lucide-react"; // UI Components import Layout from "@/components/layout/layout"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "@/components/ui/alert-dialog"; import { Separator } from "@/components/ui/separator"; // Types & API import { Deliverable, ProjectApi } from "@/types"; import { deliverablesApi, projectService } from "@/lib/api"; const DeliverablePage = () => { const { id } = useParams<{ id: string }>(); const deliverableId = parseInt(id || "0", 10); const [deleteDialogOpen, setDeleteDialogOpen] = useState(false); const navigate = useNavigate(); const queryClient = useQueryClient(); // Fetch deliverable data const { isLoading: isLoadingDeliverable, error: deliverableError, data: deliverable } = useQuery({ queryKey: ['deliverable', deliverableId], queryFn: () => deliverablesApi.getById(deliverableId), enabled: deliverableId > 0, }); // Fetch project data for the deliverable const { isLoading: isLoadingProject, error: projectError, data: project } = useQuery({ queryKey: ['project', deliverable?.projectId], queryFn: () => projectService.getById(deliverable?.projectId || 0), enabled: !!deliverable?.projectId, }); // Delete mutation const deleteDeliverableMutation = useMutation({ mutationFn: (id: number) => deliverablesApi.delete(id), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['deliverables'] }); toast.success('Deliverable deleted successfully'); navigate('/deliverables'); }, onError: (error) => { console.error('Delete error:', error); toast.error('Failed to delete deliverable'); } }); // Handle delete deliverable const handleDeleteDeliverable = () => { setDeleteDialogOpen(true); }; // Confirm delete const confirmDelete = () => { deleteDeliverableMutation.mutate(deliverableId); }; // Handle edit const handleEdit = () => { navigate(`/deliverable-edit/${deliverableId}`); }; if (isLoadingDeliverable || (deliverable?.projectId && isLoadingProject)) { return ( Loading deliverable data... ); } if (deliverableError || (deliverable?.projectId && projectError)) { return ( Error loading deliverable data navigate("/deliverables")} > Back to Deliverables ); } if (!deliverable) { return ( Deliverable not found navigate("/deliverables")} > Back to Deliverables ); } return ( navigate("/deliverables")} className="mb-2" > Back to Deliverables {deliverable.name} {project && ( Project: {project.projectName} )} Edit Delete Deliverable Details Name {deliverable.name} Description {deliverable.description} Delivery Date {format(parseISO(deliverable.deliveryDate), "MMMM d, yyyy")} Project {project ? project.projectName : "Loading project..."} Are you absolutely sure? This action cannot be undone. This will permanently delete the deliverable. Cancel Delete ); }; export default DeliverablePage;
Loading deliverable data...
Error loading deliverable data
Deliverable not found
Project: {project.projectName}
{deliverable.name}
{deliverable.description}
{project ? project.projectName : "Loading project..."}