From fda91aedb93e65222d336f729fd0acc7aa76afb9 Mon Sep 17 00:00:00 2001 From: Tola Leng Date: Wed, 23 Jul 2025 22:13:20 +0700 Subject: [PATCH] Fix: Display new service immediately - Ensure that newly created services are immediately visible in the services table list on the Uptime Monitoring dashboard, without requiring a manual refresh or a delay. --- .../src/components/services/AddServiceDialog.tsx | 7 ++++++- .../components/services/add-service/ServiceForm.tsx | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/application/src/components/services/AddServiceDialog.tsx b/application/src/components/services/AddServiceDialog.tsx index 4d37c26..13e5947 100644 --- a/application/src/components/services/AddServiceDialog.tsx +++ b/application/src/components/services/AddServiceDialog.tsx @@ -8,6 +8,7 @@ import { } from "@/components/ui/dialog"; import { ServiceForm } from "./ServiceForm"; import { ScrollArea } from "@/components/ui/scroll-area"; +import { useQueryClient } from "@tanstack/react-query"; interface AddServiceDialogProps { open: boolean; @@ -15,7 +16,11 @@ interface AddServiceDialogProps { } export function AddServiceDialog({ open, onOpenChange }: AddServiceDialogProps) { - const handleSuccess = () => { + const queryClient = useQueryClient(); + const handleSuccess = async () => { + // Immediately invalidate and refetch services data + await queryClient.invalidateQueries({ queryKey: ["services"] }); + await queryClient.refetchQueries({ queryKey: ["services"] }); onOpenChange(false); }; diff --git a/application/src/components/services/add-service/ServiceForm.tsx b/application/src/components/services/add-service/ServiceForm.tsx index 8e66a92..28ba8e9 100644 --- a/application/src/components/services/add-service/ServiceForm.tsx +++ b/application/src/components/services/add-service/ServiceForm.tsx @@ -13,6 +13,7 @@ import { serviceService } from "@/services/serviceService"; import { Service } from "@/types/service.types"; import { ServiceRegionalFields } from "./ServiceRegionalFields"; import { getServiceFormDefaults, mapServiceToFormData, mapFormDataToServiceData } from "./serviceFormUtils"; +import { useQueryClient } from "@tanstack/react-query"; interface ServiceFormProps { onSuccess: () => void; @@ -31,6 +32,7 @@ export function ServiceForm({ }: ServiceFormProps) { const { toast } = useToast(); const [isSubmitting, setIsSubmitting] = useState(false); + const queryClient = useQueryClient(); // Initialize form with default values const form = useForm({ @@ -91,6 +93,16 @@ export function ServiceForm({ }); } + // Force immediate refresh of services data + queryClient.setQueryData(["services"], (oldData: any) => { + // Invalidate the cache to force a fresh fetch + return undefined; + }); + + // Invalidate and refetch services query + await queryClient.invalidateQueries({ queryKey: ["services"] }); + await queryClient.refetchQueries({ queryKey: ["services"] }); + onSuccess(); if (!isEdit) { form.reset();