Skip to content

Commit

Permalink
[GEN-2074]: Implement polling mechanism for refetching data in useSou…
Browse files Browse the repository at this point in the history
…rceCRUD (#2034)
  • Loading branch information
BenElferink authored Dec 19, 2024
1 parent 4a1debf commit b5795e7
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion frontend/webapp/hooks/sources/useSourceCRUD.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useCallback } from 'react';
import { useMutation } from '@apollo/client';
import { useNotificationStore } from '@/store';
import { ACTION, getSseTargetFromId } from '@/utils';
Expand All @@ -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,
Expand All @@ -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);
};

Expand Down

0 comments on commit b5795e7

Please sign in to comment.