From 2166c5b14e1e8ec24f62604d3942969d47840c5a Mon Sep 17 00:00:00 2001 From: lyndsiWilliams Date: Thu, 26 Jan 2023 18:14:12 -0600 Subject: [PATCH] Fix bug: already existing datasets are not marked as "already exists" in dataset creation page --- .../CRUD/data/dataset/AddDataset/index.tsx | 36 +++++++--------- .../src/views/CRUD/data/hooks.ts | 42 +++++++++++++++---- 2 files changed, 51 insertions(+), 27 deletions(-) diff --git a/superset-frontend/src/views/CRUD/data/dataset/AddDataset/index.tsx b/superset-frontend/src/views/CRUD/data/dataset/AddDataset/index.tsx index 1cb4b0d5ae728..a6d84ff10ee60 100644 --- a/superset-frontend/src/views/CRUD/data/dataset/AddDataset/index.tsx +++ b/superset-frontend/src/views/CRUD/data/dataset/AddDataset/index.tsx @@ -25,7 +25,6 @@ import React, { } from 'react'; import { logging, t } from '@superset-ui/core'; import { UseGetDatasetsList } from 'src/views/CRUD/data/hooks'; -import rison from 'rison'; import { addDangerToast } from 'src/components/MessageToasts/actions'; import Header from './Header'; import DatasetPanel from './DatasetPanel'; @@ -89,26 +88,23 @@ export default function AddDataset() { ? encodeURIComponent(dataset?.schema) : undefined; - const queryParams = dataset?.schema - ? rison.encode_uri({ - filters: [ - { col: 'database', opr: 'rel_o_m', value: dataset?.db?.id }, - { col: 'schema', opr: 'eq', value: encodedSchema }, - { col: 'sql', opr: 'dataset_is_null_or_empty', value: '!t' }, - ], - }) - : undefined; - const getDatasetsList = useCallback(async () => { - await UseGetDatasetsList(queryParams) - .then(json => { - setDatasets(json?.result); - }) - .catch(error => { - addDangerToast(t('There was an error fetching dataset')); - logging.error(t('There was an error fetching dataset'), error); - }); - }, [queryParams]); + if (dataset?.schema) { + const filters = [ + { col: 'database', opr: 'rel_o_m', value: dataset?.db?.id }, + { col: 'schema', opr: 'eq', value: encodedSchema }, + { col: 'sql', opr: 'dataset_is_null_or_empty', value: true }, + ]; + await UseGetDatasetsList(filters) + .then(results => { + setDatasets(results); + }) + .catch(error => { + addDangerToast(t('There was an error fetching dataset')); + logging.error(t('There was an error fetching dataset'), error); + }); + } + }, [dataset?.db?.id, dataset?.schema, encodedSchema]); useEffect(() => { if (dataset?.schema) { diff --git a/superset-frontend/src/views/CRUD/data/hooks.ts b/superset-frontend/src/views/CRUD/data/hooks.ts index e15e4be957164..0c1528f07f437 100644 --- a/superset-frontend/src/views/CRUD/data/hooks.ts +++ b/superset-frontend/src/views/CRUD/data/hooks.ts @@ -19,6 +19,8 @@ import { useState, useEffect } from 'react'; import { SupersetClient, logging, t } from '@superset-ui/core'; import { addDangerToast } from 'src/components/MessageToasts/actions'; +import { DatasetObject } from 'src/views/CRUD/data/dataset/AddDataset/types'; +import rison from 'rison'; type BaseQueryObject = { id: number; @@ -76,12 +78,38 @@ export function useQueryPreviewState({ }; } -export const UseGetDatasetsList = (queryParams: string | undefined) => - SupersetClient.get({ - endpoint: `/api/v1/dataset/?q=${queryParams}`, - }) - .then(({ json }) => json) - .catch(error => { +/** + * Retrieves all pages of dataset results + */ +export const UseGetDatasetsList = async (filters: object[]) => { + let results: DatasetObject[] = []; + let page = 0; + let count; + + // If count is undefined or less than results, we need to + // asynchronously retrieve a page of dataset results + while (count === undefined || results.length < count) { + const queryParams = rison.encode_uri({ filters, page }); + try { + // eslint-disable-next-line no-await-in-loop + const response = await SupersetClient.get({ + endpoint: `/api/v1/dataset/?q=${queryParams}`, + }); + + // Reassign local count to response's count + ({ count } = response.json); + + const { + json: { result }, + } = response; + + results = [...results, ...result]; + + page += 1; + } catch (error) { addDangerToast(t('There was an error fetching dataset')); logging.error(t('There was an error fetching dataset'), error); - }); + } + } + return results; +};