import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { ServiceForm } from "./ServiceForm"; import { Service } from "@/types/service.types"; import { useQueryClient } from "@tanstack/react-query"; import { useState, useEffect } from "react"; import { ScrollArea } from "@/components/ui/scroll-area"; interface ServiceEditDialogProps { open: boolean; onOpenChange: (open: boolean) => void; service: Service | null; } export function ServiceEditDialog({ open, onOpenChange, service }: ServiceEditDialogProps) { const queryClient = useQueryClient(); const [isSubmitting, setIsSubmitting] = useState(false); // Reset submission state when dialog opens/closes useEffect(() => { if (!open) { setIsSubmitting(false); } }, [open]); const handleSuccess = () => { // Invalidate the services query to trigger a refetch queryClient.invalidateQueries({ queryKey: ["services"] }); setIsSubmitting(false); onOpenChange(false); }; const handleCancel = () => { if (!isSubmitting) { onOpenChange(false); } }; // Only render the form if dialog is open and service data exists // This prevents form validation errors when dialog is closed return ( { // Only allow closing if not currently submitting if (!isSubmitting || !newOpen) { onOpenChange(newOpen); } }}> Edit Service Update the details of your monitored service. {open && service && (
setIsSubmitting(true)} />
)}
); }