Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add tabs to edit dataset page #22043

Merged
merged 20 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ jest.mock('react-router-dom', () => ({
useHistory: () => ({
push: mockHistoryPush,
}),
useParams: () => ({ datasetId: undefined }),
}));

describe('AddDataset', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import React from 'react';
import fetchMock from 'fetch-mock';
import { render, screen } from 'spec/helpers/testing-library';
import EditDataset from './index';

const DATASET_ENDPOINT = 'glob:*api/v1/dataset/1/related_objects';

const mockedProps = {
id: '1',
};

fetchMock.get(DATASET_ENDPOINT, { charts: { results: [], count: 2 } });

test('should render edit dataset view with tabs', async () => {
render(<EditDataset {...mockedProps} />);

const columnTab = await screen.findByRole('tab', { name: /columns/i });
const metricsTab = screen.getByRole('tab', { name: /metrics/i });
const usageTab = screen.getByRole('tab', { name: /usage/i });

expect(fetchMock.calls(DATASET_ENDPOINT)).toBeTruthy();
expect(columnTab).toBeInTheDocument();
expect(metricsTab).toBeInTheDocument();
expect(usageTab).toBeInTheDocument();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { styled, t } from '@superset-ui/core';
import React, { useEffect } from 'react';
import { useGetDatasetRelatedObjects } from 'src/views/CRUD/data/hooks';
import Badge from 'src/components/Badge';
import Tabs from 'src/components/Tabs';

const StyledTabs = styled(Tabs)`
${({ theme }) => `
margin-top: ${theme.gridUnit * 8.5}px;
padding-left: ${theme.gridUnit * 4}px;

.ant-tabs-top > .ant-tabs-nav::before {
width: ${theme.gridUnit * 50}px;
}
`}
`;

const TabStyles = styled.div`
${({ theme }) => `
.ant-badge {
width: ${theme.gridUnit * 8}px;
margin-left: ${theme.gridUnit * 2.5}px;
}
`}
`;

interface EditPageProps {
id: string;
}

const TRANSLATIONS = {
USAGE_TEXT: t('Usage'),
COLUMNS_TEXT: t('Columns'),
METRICS_TEXT: t('Metrics'),
};

const EditPage = ({ id }: EditPageProps) => {
const { getDatasetRelatedObjects, usageCount } =
useGetDatasetRelatedObjects(id);
useEffect(() => {
// Todo: this useEffect should be used to call all count methods conncurently
// when we populate data for the new tabs. For right separating out this
// api call for building the usage page.
if (id) {
getDatasetRelatedObjects();
}
}, [id, getDatasetRelatedObjects]);

const usageTab = (
<TabStyles>
<span>{TRANSLATIONS.USAGE_TEXT}</span>
{usageCount > 0 && <Badge count={usageCount} />}
</TabStyles>
);

return (
<StyledTabs moreIcon={null} fullWidth={false}>
<Tabs.TabPane tab={TRANSLATIONS.COLUMNS_TEXT} key="1" />
<Tabs.TabPane tab={TRANSLATIONS.METRICS_TEXT} key="2" />
<Tabs.TabPane tab={usageTab} key="3" />
</StyledTabs>
);
};

export default EditPage;
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ fetchMock.get(schemasEndpoint, {
});

fetchMock.get(tablesEndpoint, {
tableLength: 3,
count: 3,
result: [
{ value: 'Sheet1', type: 'table', extra: null },
{ value: 'Sheet2', type: 'table', extra: null },
Expand Down
59 changes: 27 additions & 32 deletions superset-frontend/src/views/CRUD/data/dataset/AddDataset/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,11 @@
* specific language governing permissions and limitations
* under the License.
*/
import React, {
useReducer,
Reducer,
useEffect,
useState,
useCallback,
} from 'react';
import { logging, t } from '@superset-ui/core';
import { UseGetDatasetsList } from 'src/views/CRUD/data/hooks';
import { addDangerToast } from 'src/components/MessageToasts/actions';
import React, { useReducer, Reducer, useEffect, useState } from 'react';
import { useParams } from 'react-router-dom';
import { useGetDatasetsList } from 'src/views/CRUD/data/hooks';
import Header from './Header';
import EditPage from './EditDataset';
import DatasetPanel from './DatasetPanel';
import LeftPanel from './LeftPanel';
import Footer from './Footer';
Expand Down Expand Up @@ -82,35 +76,31 @@ export default function AddDataset() {
Reducer<Partial<DatasetObject> | null, DSReducerActionType>
>(datasetReducer, null);
const [hasColumns, setHasColumns] = useState(false);
const [datasets, setDatasets] = useState<DatasetObject[]>([]);
const datasetNames = datasets.map(dataset => dataset.table_name);
const [editPageIsVisible, setEditPageIsVisible] = useState(false);
const encodedSchema = dataset?.schema
lyndsiWilliams marked this conversation as resolved.
Show resolved Hide resolved
? encodeURIComponent(dataset?.schema)
: undefined;

const getDatasetsList = useCallback(async () => {
const { getDatasetsList, datasets, datasetNames } = useGetDatasetsList();
lyndsiWilliams marked this conversation as resolved.
Show resolved Hide resolved

useEffect(() => {
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 },
];

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);
});
getDatasetsList(filters);
}
}, [dataset?.db?.id, dataset?.schema, encodedSchema]);
}, [dataset?.db?.id, dataset?.schema, encodedSchema, getDatasetsList]);

const { datasetId: id } = useParams<{ datasetId: string }>();
useEffect(() => {
if (dataset?.schema) {
getDatasetsList();
if (!Number.isNaN(parseInt(id, 10))) {
setEditPageIsVisible(true);
}
}, [dataset?.schema, getDatasetsList]);
}, [id]);

const HeaderComponent = () => (
<Header setDataset={setDataset} title={dataset?.table_name} />
Expand All @@ -124,6 +114,8 @@ export default function AddDataset() {
/>
);

const EditPageComponent = () => <EditPage id={id} />;

const DatasetPanelComponent = () => (
<DatasetPanel
tableName={dataset?.table_name}
Expand All @@ -146,9 +138,12 @@ export default function AddDataset() {
return (
<DatasetLayout
header={HeaderComponent()}
leftPanel={LeftPanelComponent()}
datasetPanel={DatasetPanelComponent()}
leftPanel={editPageIsVisible ? null : LeftPanelComponent()}
datasetPanel={
editPageIsVisible ? EditPageComponent() : DatasetPanelComponent()
}
footer={FooterComponent()}
editPageIsVisible={editPageIsVisible}
/>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ interface DatasetLayoutProps {
datasetPanel?: ReactElement<any, string | JSXElementConstructor<any>> | null;
rightPanel?: ReactElement<any, string | JSXElementConstructor<any>> | null;
footer?: ReactElement<any, string | JSXElementConstructor<any>> | null;
editPageIsVisible?: boolean;
}

export default function DatasetLayout({
Expand All @@ -45,17 +46,19 @@ export default function DatasetLayout({
datasetPanel,
rightPanel,
footer,
editPageIsVisible,
}: DatasetLayoutProps) {
return (
<StyledLayoutWrapper data-test="dataset-layout-wrapper">
{header && <StyledLayoutHeader>{header}</StyledLayoutHeader>}
<OuterRow>
<LeftColumn>
{leftPanel && (
<StyledLayoutLeftPanel>{leftPanel}</StyledLayoutLeftPanel>
)}
</LeftColumn>

{!editPageIsVisible && (
<LeftColumn>
{leftPanel && (
<StyledLayoutLeftPanel>{leftPanel}</StyledLayoutLeftPanel>
)}
</LeftColumn>
)}
lyndsiWilliams marked this conversation as resolved.
Show resolved Hide resolved
<RightColumn>
<PanelRow>
{datasetPanel && (
Expand Down
84 changes: 57 additions & 27 deletions superset-frontend/src/views/CRUD/data/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
import { useState, useEffect } from 'react';
import { useState, useEffect, useCallback } 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';
Expand Down Expand Up @@ -81,35 +81,65 @@ export function useQueryPreviewState<D extends BaseQueryObject = any>({
/**
* 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}`,
});
export const useGetDatasetsList = () => {
const [datasets, setDatasets] = useState<DatasetObject[]>([]);

const getDatasetsList = useCallback(async (filters: object[]) => {
let results: DatasetObject[] = [];
let page = 0;
let count;

// Reassign local count to response's count
({ count } = response.json);
// 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}`,
});

const {
json: { result },
} = response;
// Reassign local count to response's count
({ count } = response.json);

results = [...results, ...result];
const {
json: { result },
} = response;

page += 1;
} catch (error) {
addDangerToast(t('There was an error fetching dataset'));
logging.error(t('There was an error fetching dataset'), error);
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;

setDatasets(results);
return results;
lyndsiWilliams marked this conversation as resolved.
Show resolved Hide resolved
}, []);

const datasetNames = datasets?.map(dataset => dataset.table_name);

return { getDatasetsList, datasets, datasetNames };
};

export const useGetDatasetRelatedObjects = (id: string) => {
const [usageCount, setUsageCount] = useState(0);

const getDatasetRelatedObjects = () =>
lyndsiWilliams marked this conversation as resolved.
Show resolved Hide resolved
SupersetClient.get({
endpoint: `/api/v1/dataset/${id}/related_objects`,
})
.then(({ json }) => {
setUsageCount(json?.charts.count);
})
.catch(error => {
addDangerToast(
t(`There was an error fetching dataset's related objects`),
);
logging.error(error);
});

return { getDatasetRelatedObjects, usageCount };
};