From b5795e7d4d46b44ca24f5f0d7abb438e6e52b73d Mon Sep 17 00:00:00 2001 From: Ben Elferink Date: Thu, 19 Dec 2024 18:35:27 +0200 Subject: [PATCH] [GEN-2074]: Implement polling mechanism for refetching data in useSourceCRUD (#2034) --- frontend/webapp/hooks/sources/useSourceCRUD.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/frontend/webapp/hooks/sources/useSourceCRUD.ts b/frontend/webapp/hooks/sources/useSourceCRUD.ts index 82f30bdf7..dc2f0ed27 100644 --- a/frontend/webapp/hooks/sources/useSourceCRUD.ts +++ b/frontend/webapp/hooks/sources/useSourceCRUD.ts @@ -1,3 +1,4 @@ +import { useCallback } from 'react'; import { useMutation } from '@apollo/client'; import { useNotificationStore } from '@/store'; import { ACTION, getSseTargetFromId } from '@/utils'; @@ -16,6 +17,18 @@ export const useSourceCRUD = (params?: Params) => { const { data, refetch } = useComputePlatform(); const { addNotification } = useNotificationStore(); + const startPolling = useCallback(async () => { + let retries = 0; + const maxRetries = 5; + const retryInterval = 1 * 1000; // time in milliseconds + + while (retries < maxRetries) { + await new Promise((resolve) => setTimeout(resolve, retryInterval)); + refetch(); + retries++; + } + }, [refetch]); + const notifyUser = (type: NOTIFICATION_TYPE, title: string, message: string, id?: WorkloadId) => { addNotification({ type, @@ -33,7 +46,7 @@ export const useSourceCRUD = (params?: Params) => { const handleComplete = (title: string, message: string, id?: WorkloadId) => { notifyUser(NOTIFICATION_TYPE.SUCCESS, title, message, id); - refetch(); + startPolling(); params?.onSuccess?.(title); };