Skip to content

Commit

Permalink
reduce the dupl code blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
justinpark committed Jun 18, 2024
1 parent 4c502c2 commit c457dca
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 72 deletions.
51 changes: 16 additions & 35 deletions superset-frontend/src/hooks/apiResources/catalogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,47 +77,28 @@ export function useCatalogs(options: Params) {
},
);

const handleOnSuccess = useEffectEvent(
(data: CatalogOption[], isRefetched: boolean) => {
onSuccess?.(data, isRefetched);
},
);

const handleOnError = useEffectEvent(() => {
onError?.();
});

const resolver = useEffectEvent(() =>
result.currentData ? undefined : trigger({ dbId, forceRefresh: false }),
);

const refetch = useCallback(() => {
if (dbId) {
trigger({ dbId, forceRefresh: true }).then(
({ isSuccess, isError, data }) => {
const fetchData = useEffectEvent(
(dbId: FetchCatalogsQueryParams['dbId'], forceRefresh = false) => {
if (dbId && (!result.currentData || forceRefresh)) {
trigger({ dbId, forceRefresh }).then(({ isSuccess, isError, data }) => {
if (isSuccess) {
handleOnSuccess(data || EMPTY_CATALOGS, true);
onSuccess?.(data || EMPTY_CATALOGS, forceRefresh);
}
if (isError) {
handleOnError();
onError?.();
}
},
);
}
}, [dbId, handleOnError, handleOnSuccess, trigger]);
});
}
},
);

const refetch = useCallback(() => {
fetchData(dbId, true);
}, [dbId, fetchData]);

useEffect(() => {
if (dbId) {
resolver()?.then(({ isSuccess, isError, data }) => {
if (isSuccess) {
handleOnSuccess(data || EMPTY_CATALOGS, false);
}
if (isError) {
handleOnError();
}
});
}
}, [dbId, result, handleOnSuccess, handleOnError, resolver]);
fetchData(dbId, false);
}, [dbId, fetchData]);

return {
...result,
Expand Down
59 changes: 22 additions & 37 deletions superset-frontend/src/hooks/apiResources/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,49 +81,34 @@ export function useSchemas(options: Params) {
},
);

const handleOnSuccess = useEffectEvent(
(data: SchemaOption[], isRefetched: boolean) => {
onSuccess?.(data, isRefetched);
const fetchData = useEffectEvent(
(
dbId: FetchSchemasQueryParams['dbId'],
catalog: FetchSchemasQueryParams['catalog'],
forceRefresh = false,
) => {
if (dbId && (!result.currentData || forceRefresh)) {
trigger({ dbId, catalog, forceRefresh }).then(
({ isSuccess, isError, data }) => {
if (isSuccess) {
onSuccess?.(data || EMPTY_SCHEMAS, forceRefresh);
}
if (isError) {
onError?.();
}
},
);
}
},
);

const handleOnError = useEffectEvent(() => {
onError?.();
});

const resolver = useEffectEvent(() =>
result.currentData
? undefined
: trigger({ dbId, catalog, forceRefresh: false }),
);

useEffect(() => {
if (dbId) {
resolver()?.then(({ isSuccess, isError, data }) => {
if (isSuccess) {
handleOnSuccess(data || EMPTY_SCHEMAS, false);
}
if (isError) {
handleOnError();
}
});
}
}, [dbId, catalog, handleOnError, handleOnSuccess, resolver]);
fetchData(dbId, catalog, false);
}, [dbId, catalog, fetchData]);

const refetch = useCallback(() => {
if (dbId) {
trigger({ dbId, catalog, forceRefresh: true }).then(
({ isSuccess, isError, data }) => {
if (isSuccess) {
handleOnSuccess(data || EMPTY_SCHEMAS, true);
}
if (isError) {
handleOnError();
}
},
);
}
}, [dbId, catalog, handleOnError, handleOnSuccess, trigger]);
fetchData(dbId, catalog, true);
}, [dbId, catalog, fetchData]);

return {
...result,
Expand Down

0 comments on commit c457dca

Please sign in to comment.