Skip to content

Commit

Permalink
Fix bug: already existing datasets are not marked as "already exists"…
Browse files Browse the repository at this point in the history
… in dataset creation page
  • Loading branch information
lyndsiWilliams committed Feb 1, 2023
1 parent ee0f2f0 commit 4bafc1a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 27 deletions.
36 changes: 16 additions & 20 deletions superset-frontend/src/views/CRUD/data/dataset/AddDataset/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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) {
Expand Down
42 changes: 35 additions & 7 deletions superset-frontend/src/views/CRUD/data/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -76,12 +78,38 @@ export function useQueryPreviewState<D extends BaseQueryObject = any>({
};
}

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;
};

0 comments on commit 4bafc1a

Please sign in to comment.