lyndsiWilliams commented on code in PR #21075:
URL: https://github.com/apache/superset/pull/21075#discussion_r958712154
##########
superset-frontend/src/views/CRUD/data/dataset/AddDataset/LeftPanel/index.tsx:
##########
@@ -16,18 +16,227 @@
* specific language governing permissions and limitations
* under the License.
*/
-import React from 'react';
-import { t } from '@superset-ui/core';
+import React, {
+ useEffect,
+ useState,
+ useMemo,
+ SetStateAction,
+ Dispatch,
+} from 'react';
+import { SupersetClient, t, styled, FAST_DEBOUNCE } from '@superset-ui/core';
+import { Input } from 'src/components/Input';
+import { Form } from 'src/components/Form';
+import { TableOption, Table } from 'src/components/TableSelector';
+import RefreshLabel from 'src/components/RefreshLabel';
+import Loading from 'src/components/Loading';
+import DatabaseSelector from 'src/components/DatabaseSelector';
+import { debounce } from 'lodash';
import { EmptyStateMedium } from 'src/components/EmptyState';
+import { useToasts } from 'src/components/MessageToasts/withToasts';
+import { DatasetActionType, DatasetObject } from '../types';
+
+interface LeftPanelProps {
+ setDataset: Dispatch<SetStateAction<object>>;
+ schema?: string | undefined | null;
+ dbId?: number;
+}
+
+const LeftPanelStyle = styled.div`
+ ${({ theme }) => `
+ max-width: ${theme.gridUnit * 87.5}px;
+ padding: ${theme.gridUnit * 4}px;
+ height: 100%;
+ background-color: ${theme.colors.grayscale.light5};
+ position: relative;
+ .emptystate {
+ height: auto;
+ margin-top: 70px;
Review Comment:
```suggestion
margin-top: ${theme.gridUnit * 17.5}px;
```
Unit values should stay within the Superset theme
##########
superset-frontend/src/views/CRUD/data/dataset/AddDataset/LeftPanel/index.tsx:
##########
@@ -16,18 +16,227 @@
* specific language governing permissions and limitations
* under the License.
*/
-import React from 'react';
-import { t } from '@superset-ui/core';
+import React, {
+ useEffect,
+ useState,
+ useMemo,
+ SetStateAction,
+ Dispatch,
+} from 'react';
+import { SupersetClient, t, styled, FAST_DEBOUNCE } from '@superset-ui/core';
+import { Input } from 'src/components/Input';
+import { Form } from 'src/components/Form';
+import { TableOption, Table } from 'src/components/TableSelector';
+import RefreshLabel from 'src/components/RefreshLabel';
+import Loading from 'src/components/Loading';
+import DatabaseSelector from 'src/components/DatabaseSelector';
+import { debounce } from 'lodash';
import { EmptyStateMedium } from 'src/components/EmptyState';
+import { useToasts } from 'src/components/MessageToasts/withToasts';
+import { DatasetActionType, DatasetObject } from '../types';
+
+interface LeftPanelProps {
+ setDataset: Dispatch<SetStateAction<object>>;
+ schema?: string | undefined | null;
+ dbId?: number;
+}
+
+const LeftPanelStyle = styled.div`
+ ${({ theme }) => `
+ max-width: ${theme.gridUnit * 87.5}px;
+ padding: ${theme.gridUnit * 4}px;
+ height: 100%;
+ background-color: ${theme.colors.grayscale.light5};
+ position: relative;
+ .emptystate {
+ height: auto;
+ margin-top: 70px;
+ }
+ .refresh {
+ position: absolute;
+ top: ${theme.gridUnit * 43.25}px;
+ left: ${theme.gridUnit * 16.75}px;
+ span[role="button"]{
+ font-size: ${theme.gridUnit * 4.25}px;
+ }
+ }
+ .section-title {
+ margin-top: ${theme.gridUnit * 11}px;
+ margin-bottom: ${theme.gridUnit * 11}px;
+ font-weight: ${theme.typography.weights.bold};
+ }
+ .table-title {
+ margin-top: ${theme.gridUnit * 11}px;
+ margin-bottom: ${theme.gridUnit * 6}px;
+ font-weight: ${theme.typography.weights.bold};
+ }
+ .options-list {
+ overflow: auto;
+ position: absolute;
+ bottom: 0px;
Review Comment:
```suggestion
bottom: 0;
```
Nit: `px` isn't needed here
##########
superset-frontend/src/views/CRUD/data/dataset/AddDataset/LeftPanel/index.tsx:
##########
@@ -16,18 +16,227 @@
* specific language governing permissions and limitations
* under the License.
*/
-import React from 'react';
-import { t } from '@superset-ui/core';
+import React, {
+ useEffect,
+ useState,
+ useMemo,
+ SetStateAction,
+ Dispatch,
+} from 'react';
+import { SupersetClient, t, styled, FAST_DEBOUNCE } from '@superset-ui/core';
+import { Input } from 'src/components/Input';
+import { Form } from 'src/components/Form';
+import { TableOption, Table } from 'src/components/TableSelector';
+import RefreshLabel from 'src/components/RefreshLabel';
+import Loading from 'src/components/Loading';
+import DatabaseSelector from 'src/components/DatabaseSelector';
+import { debounce } from 'lodash';
import { EmptyStateMedium } from 'src/components/EmptyState';
+import { useToasts } from 'src/components/MessageToasts/withToasts';
+import { DatasetActionType, DatasetObject } from '../types';
+
+interface LeftPanelProps {
+ setDataset: Dispatch<SetStateAction<object>>;
+ schema?: string | undefined | null;
+ dbId?: number;
+}
+
+const LeftPanelStyle = styled.div`
+ ${({ theme }) => `
+ max-width: ${theme.gridUnit * 87.5}px;
+ padding: ${theme.gridUnit * 4}px;
+ height: 100%;
+ background-color: ${theme.colors.grayscale.light5};
+ position: relative;
+ .emptystate {
+ height: auto;
+ margin-top: 70px;
+ }
+ .refresh {
+ position: absolute;
+ top: ${theme.gridUnit * 43.25}px;
+ left: ${theme.gridUnit * 16.75}px;
+ span[role="button"]{
+ font-size: ${theme.gridUnit * 4.25}px;
+ }
+ }
+ .section-title {
+ margin-top: ${theme.gridUnit * 11}px;
+ margin-bottom: ${theme.gridUnit * 11}px;
+ font-weight: ${theme.typography.weights.bold};
+ }
+ .table-title {
+ margin-top: ${theme.gridUnit * 11}px;
+ margin-bottom: ${theme.gridUnit * 6}px;
+ font-weight: ${theme.typography.weights.bold};
+ }
+ .options-list {
+ overflow: auto;
+ position: absolute;
+ bottom: 0px;
+ top: 390px;
+ left: 13px;
+ right: 0;
+ .options {
+ padding: ${theme.gridUnit * 1.75}px;
+ border-radius: ${theme.borderRadius}px;
+ }
+ }
+ form > span {
+ position: absolute;
+ top: ${theme.gridUnit * 73}px;
+ left: ${theme.gridUnit * 42.75}px;
+ font-size: ${theme.gridUnit * 4.25}px;
+ }
+ .table-form {
+ margin-bottom: ${theme.gridUnit * 8}px;
+ }
+ .loading {
+ position: absolute;
+ bottom: ${theme.gridUnit * 95}px;
+ img {
+ position: absolute;
+ top: -${theme.gridUnit * 13.25}px;
+ right: -${theme.gridUnit * 3.75}px;
+ width: ${theme.gridUnit * 17.75}px;
+ }
+ }
+ }
+`}
+`;
+
+export default function LeftPanel({
+ setDataset,
+ schema,
+ dbId,
+}: LeftPanelProps) {
+ const [tableOptions, setTableOptions] = useState<Array<TableOption>>([]);
+ const [resetTables, setResetTables] = useState(false);
+ const [loadTables, setLoadTables] = useState(false);
+ const [searchVal, setSearchVal] = useState('');
+ const [refresh, setRefresh] = useState(false);
+
+ const { addDangerToast } = useToasts();
+
+ const setDatabase = (db: Partial<DatasetObject>) => {
+ setDataset({ type: DatasetActionType.selectDatabase, payload: db });
+ setResetTables(true);
+ };
+
+ const getTablesList = (url: string) => {
+ SupersetClient.get({ url })
+ .then(({ json }) => {
+ const options: TableOption[] = json.options.map((table: Table) => {
+ const option: TableOption = {
+ value: table.value,
+ label: <TableOption table={table} />,
+ text: table.label,
+ };
+
+ return option;
+ });
+
+ setTableOptions(options);
+ setLoadTables(false);
+ setResetTables(false);
+ setRefresh(false);
+ })
+ .catch(e => {
+ console.log('error', e);
+ });
+ };
+
+ const setSchema = (schema: string) => {
+ if (schema) {
+ setDataset({ type: DatasetActionType.selectSchema, payload: schema });
Review Comment:
```suggestion
setDataset({
type: DatasetActionType.selectSchema,
payload: { name: 'schema', value: schema },
});
```
I think the state/types for this need to be adjusted so that you don't need
to set `as string` for schema in the reducer, this change happens in 3 parts of
the code. Here, in AddDataset, and in the types file.
(1/3): This will need to be set differently so we can use it with
`DatasetReducerPayloadType` in types
##########
superset-frontend/src/views/CRUD/data/dataset/AddDataset/index.tsx:
##########
@@ -42,7 +47,7 @@ export function datasetReducer(
case DatasetActionType.selectSchema:
return {
...trimmedState,
- ...action.payload,
+ schema: action.payload as string,
Review Comment:
```suggestion
[action.payload.name]: action.payload.value,
```
(2/3): By setting this with `DatasetReducerPayloadType` in types, it'll
already be defined as a `string`
##########
superset-frontend/src/views/CRUD/data/dataset/AddDataset/index.tsx:
##########
@@ -63,14 +64,22 @@ export function datasetReducer(
export default function AddDataset() {
// this is commented out for now, but can be commented in as the component
Review Comment:
+1 on this, remove the "this is commented out for now..." comment
##########
superset-frontend/src/views/CRUD/data/dataset/AddDataset/LeftPanel/index.tsx:
##########
@@ -16,18 +16,227 @@
* specific language governing permissions and limitations
* under the License.
*/
-import React from 'react';
-import { t } from '@superset-ui/core';
+import React, {
+ useEffect,
+ useState,
+ useMemo,
+ SetStateAction,
+ Dispatch,
+} from 'react';
+import { SupersetClient, t, styled, FAST_DEBOUNCE } from '@superset-ui/core';
+import { Input } from 'src/components/Input';
+import { Form } from 'src/components/Form';
+import { TableOption, Table } from 'src/components/TableSelector';
+import RefreshLabel from 'src/components/RefreshLabel';
+import Loading from 'src/components/Loading';
+import DatabaseSelector from 'src/components/DatabaseSelector';
+import { debounce } from 'lodash';
import { EmptyStateMedium } from 'src/components/EmptyState';
+import { useToasts } from 'src/components/MessageToasts/withToasts';
+import { DatasetActionType, DatasetObject } from '../types';
+
+interface LeftPanelProps {
+ setDataset: Dispatch<SetStateAction<object>>;
+ schema?: string | undefined | null;
+ dbId?: number;
+}
+
+const LeftPanelStyle = styled.div`
+ ${({ theme }) => `
+ max-width: ${theme.gridUnit * 87.5}px;
+ padding: ${theme.gridUnit * 4}px;
+ height: 100%;
+ background-color: ${theme.colors.grayscale.light5};
+ position: relative;
+ .emptystate {
+ height: auto;
+ margin-top: 70px;
+ }
+ .refresh {
+ position: absolute;
+ top: ${theme.gridUnit * 43.25}px;
+ left: ${theme.gridUnit * 16.75}px;
+ span[role="button"]{
+ font-size: ${theme.gridUnit * 4.25}px;
+ }
+ }
+ .section-title {
+ margin-top: ${theme.gridUnit * 11}px;
+ margin-bottom: ${theme.gridUnit * 11}px;
+ font-weight: ${theme.typography.weights.bold};
+ }
+ .table-title {
+ margin-top: ${theme.gridUnit * 11}px;
+ margin-bottom: ${theme.gridUnit * 6}px;
+ font-weight: ${theme.typography.weights.bold};
+ }
+ .options-list {
+ overflow: auto;
+ position: absolute;
+ bottom: 0px;
+ top: 390px;
+ left: 13px;
+ right: 0;
+ .options {
+ padding: ${theme.gridUnit * 1.75}px;
+ border-radius: ${theme.borderRadius}px;
+ }
+ }
+ form > span {
+ position: absolute;
+ top: ${theme.gridUnit * 73}px;
+ left: ${theme.gridUnit * 42.75}px;
+ font-size: ${theme.gridUnit * 4.25}px;
+ }
+ .table-form {
+ margin-bottom: ${theme.gridUnit * 8}px;
+ }
+ .loading {
+ position: absolute;
+ bottom: ${theme.gridUnit * 95}px;
+ img {
+ position: absolute;
+ top: -${theme.gridUnit * 13.25}px;
+ right: -${theme.gridUnit * 3.75}px;
+ width: ${theme.gridUnit * 17.75}px;
+ }
+ }
+ }
+`}
+`;
+
+export default function LeftPanel({
+ setDataset,
+ schema,
+ dbId,
+}: LeftPanelProps) {
+ const [tableOptions, setTableOptions] = useState<Array<TableOption>>([]);
+ const [resetTables, setResetTables] = useState(false);
+ const [loadTables, setLoadTables] = useState(false);
+ const [searchVal, setSearchVal] = useState('');
+ const [refresh, setRefresh] = useState(false);
+
+ const { addDangerToast } = useToasts();
+
+ const setDatabase = (db: Partial<DatasetObject>) => {
+ setDataset({ type: DatasetActionType.selectDatabase, payload: db });
+ setResetTables(true);
+ };
+
+ const getTablesList = (url: string) => {
+ SupersetClient.get({ url })
+ .then(({ json }) => {
+ const options: TableOption[] = json.options.map((table: Table) => {
+ const option: TableOption = {
+ value: table.value,
+ label: <TableOption table={table} />,
+ text: table.label,
+ };
+
+ return option;
+ });
+
+ setTableOptions(options);
+ setLoadTables(false);
+ setResetTables(false);
+ setRefresh(false);
+ })
+ .catch(e => {
+ console.log('error', e);
+ });
+ };
+
+ const setSchema = (schema: string) => {
+ if (schema) {
+ setDataset({ type: DatasetActionType.selectSchema, payload: schema });
+ setLoadTables(true);
+ }
+ setResetTables(true);
+ };
+
+ const encodedSchema = encodeURIComponent(schema as string);
+
+ useEffect(() => {
+ if (loadTables) {
+ const endpoint = encodeURI(
+ `/superset/tables/${dbId}/${encodedSchema}/undefined/${refresh}/`,
+ );
+ getTablesList(endpoint);
+ }
+ }, [loadTables]);
+
+ useEffect(() => {
+ if (resetTables) {
+ setTableOptions([]);
+ setResetTables(false);
+ }
+ }, [resetTables]);
+
+ const search = useMemo(
+ () =>
+ debounce((value: string) => {
+ const encodeTableName =
+ value === '' ? undefined : encodeURIComponent(value);
+ const endpoint = encodeURI(
+ `/superset/tables/${dbId}/${encodedSchema}/${encodeTableName}/`,
+ );
+ getTablesList(endpoint);
+ }, FAST_DEBOUNCE),
+ [dbId, encodedSchema],
+ );
-export default function LeftPanel() {
return (
- <>
- <EmptyStateMedium
- image="empty-table.svg"
- title={t('No database tables found')}
- description={t('Try selecting a different schema')}
+ <LeftPanelStyle>
+ <p className="section-title db-schema"> Select Database & Schema</p>
Review Comment:
```suggestion
<p className="section-title db-schema">Select Database & Schema</p>
```
Is this space meant to be here?
##########
superset-frontend/src/views/CRUD/data/dataset/AddDataset/LeftPanel/index.tsx:
##########
@@ -16,18 +16,227 @@
* specific language governing permissions and limitations
* under the License.
*/
-import React from 'react';
-import { t } from '@superset-ui/core';
+import React, {
+ useEffect,
+ useState,
+ useMemo,
+ SetStateAction,
+ Dispatch,
+} from 'react';
+import { SupersetClient, t, styled, FAST_DEBOUNCE } from '@superset-ui/core';
+import { Input } from 'src/components/Input';
+import { Form } from 'src/components/Form';
+import { TableOption, Table } from 'src/components/TableSelector';
+import RefreshLabel from 'src/components/RefreshLabel';
+import Loading from 'src/components/Loading';
+import DatabaseSelector from 'src/components/DatabaseSelector';
+import { debounce } from 'lodash';
import { EmptyStateMedium } from 'src/components/EmptyState';
+import { useToasts } from 'src/components/MessageToasts/withToasts';
+import { DatasetActionType, DatasetObject } from '../types';
+
+interface LeftPanelProps {
+ setDataset: Dispatch<SetStateAction<object>>;
+ schema?: string | undefined | null;
+ dbId?: number;
+}
+
+const LeftPanelStyle = styled.div`
+ ${({ theme }) => `
+ max-width: ${theme.gridUnit * 87.5}px;
+ padding: ${theme.gridUnit * 4}px;
+ height: 100%;
+ background-color: ${theme.colors.grayscale.light5};
+ position: relative;
+ .emptystate {
+ height: auto;
+ margin-top: 70px;
+ }
+ .refresh {
+ position: absolute;
+ top: ${theme.gridUnit * 43.25}px;
+ left: ${theme.gridUnit * 16.75}px;
+ span[role="button"]{
+ font-size: ${theme.gridUnit * 4.25}px;
+ }
+ }
+ .section-title {
+ margin-top: ${theme.gridUnit * 11}px;
+ margin-bottom: ${theme.gridUnit * 11}px;
+ font-weight: ${theme.typography.weights.bold};
+ }
+ .table-title {
+ margin-top: ${theme.gridUnit * 11}px;
+ margin-bottom: ${theme.gridUnit * 6}px;
+ font-weight: ${theme.typography.weights.bold};
+ }
+ .options-list {
+ overflow: auto;
+ position: absolute;
+ bottom: 0px;
+ top: 390px;
+ left: 13px;
+ right: 0;
+ .options {
+ padding: ${theme.gridUnit * 1.75}px;
+ border-radius: ${theme.borderRadius}px;
+ }
+ }
+ form > span {
+ position: absolute;
+ top: ${theme.gridUnit * 73}px;
+ left: ${theme.gridUnit * 42.75}px;
+ font-size: ${theme.gridUnit * 4.25}px;
+ }
+ .table-form {
+ margin-bottom: ${theme.gridUnit * 8}px;
+ }
+ .loading {
+ position: absolute;
+ bottom: ${theme.gridUnit * 95}px;
+ img {
+ position: absolute;
+ top: -${theme.gridUnit * 13.25}px;
+ right: -${theme.gridUnit * 3.75}px;
+ width: ${theme.gridUnit * 17.75}px;
+ }
+ }
+ }
+`}
+`;
+
+export default function LeftPanel({
+ setDataset,
+ schema,
+ dbId,
+}: LeftPanelProps) {
+ const [tableOptions, setTableOptions] = useState<Array<TableOption>>([]);
+ const [resetTables, setResetTables] = useState(false);
+ const [loadTables, setLoadTables] = useState(false);
+ const [searchVal, setSearchVal] = useState('');
+ const [refresh, setRefresh] = useState(false);
+
+ const { addDangerToast } = useToasts();
+
+ const setDatabase = (db: Partial<DatasetObject>) => {
+ setDataset({ type: DatasetActionType.selectDatabase, payload: db });
+ setResetTables(true);
+ };
+
+ const getTablesList = (url: string) => {
+ SupersetClient.get({ url })
+ .then(({ json }) => {
+ const options: TableOption[] = json.options.map((table: Table) => {
+ const option: TableOption = {
+ value: table.value,
+ label: <TableOption table={table} />,
+ text: table.label,
+ };
+
+ return option;
+ });
+
+ setTableOptions(options);
+ setLoadTables(false);
+ setResetTables(false);
+ setRefresh(false);
+ })
+ .catch(e => {
+ console.log('error', e);
+ });
+ };
+
+ const setSchema = (schema: string) => {
+ if (schema) {
+ setDataset({ type: DatasetActionType.selectSchema, payload: schema });
+ setLoadTables(true);
+ }
+ setResetTables(true);
+ };
+
+ const encodedSchema = encodeURIComponent(schema as string);
Review Comment:
```suggestion
const encodedSchema = encodeURIComponent(schema ?? '');
```
I pulled this down and it looks like TS is erroring because it doesn't like
that `schema` could be undefined. I think it would be better to create a safety
net in case schema comes in as not a string for some reason.
##########
superset-frontend/src/views/CRUD/data/dataset/AddDataset/types.tsx:
##########
@@ -39,12 +37,16 @@ interface DatasetReducerPayloadType {
value?: string;
}
+export type Schema = {
+ schema?: string | null | undefined;
+};
+
export type DSReducerActionType =
Review Comment:
```suggestion
export type DSReducerActionType =
| {
type: DatasetActionType.selectDatabase | DatasetActionType.selectTable;
payload: Partial<DatasetObject>;
}
| {
type: DatasetActionType.changeDataset | DatasetActionType.selectSchema;
payload: DatasetReducerPayloadType;
};
```
(3/3): If you change the `DSReducerActionType` to look like this, the other
2 parts of this state/type change should work and schema will be properly
described as a string without needing `as string`
##########
superset-frontend/src/views/CRUD/data/dataset/AddDataset/LeftPanel/index.tsx:
##########
@@ -16,18 +16,227 @@
* specific language governing permissions and limitations
* under the License.
*/
-import React from 'react';
-import { t } from '@superset-ui/core';
+import React, {
+ useEffect,
+ useState,
+ useMemo,
+ SetStateAction,
+ Dispatch,
+} from 'react';
+import { SupersetClient, t, styled, FAST_DEBOUNCE } from '@superset-ui/core';
+import { Input } from 'src/components/Input';
+import { Form } from 'src/components/Form';
+import { TableOption, Table } from 'src/components/TableSelector';
+import RefreshLabel from 'src/components/RefreshLabel';
+import Loading from 'src/components/Loading';
+import DatabaseSelector from 'src/components/DatabaseSelector';
+import { debounce } from 'lodash';
import { EmptyStateMedium } from 'src/components/EmptyState';
+import { useToasts } from 'src/components/MessageToasts/withToasts';
+import { DatasetActionType, DatasetObject } from '../types';
+
+interface LeftPanelProps {
+ setDataset: Dispatch<SetStateAction<object>>;
+ schema?: string | undefined | null;
+ dbId?: number;
+}
+
+const LeftPanelStyle = styled.div`
+ ${({ theme }) => `
+ max-width: ${theme.gridUnit * 87.5}px;
+ padding: ${theme.gridUnit * 4}px;
+ height: 100%;
+ background-color: ${theme.colors.grayscale.light5};
+ position: relative;
+ .emptystate {
+ height: auto;
+ margin-top: 70px;
+ }
+ .refresh {
+ position: absolute;
+ top: ${theme.gridUnit * 43.25}px;
+ left: ${theme.gridUnit * 16.75}px;
+ span[role="button"]{
+ font-size: ${theme.gridUnit * 4.25}px;
+ }
+ }
+ .section-title {
+ margin-top: ${theme.gridUnit * 11}px;
+ margin-bottom: ${theme.gridUnit * 11}px;
+ font-weight: ${theme.typography.weights.bold};
+ }
+ .table-title {
+ margin-top: ${theme.gridUnit * 11}px;
+ margin-bottom: ${theme.gridUnit * 6}px;
+ font-weight: ${theme.typography.weights.bold};
+ }
+ .options-list {
+ overflow: auto;
+ position: absolute;
+ bottom: 0px;
+ top: 390px;
+ left: 13px;
Review Comment:
```suggestion
top: ${theme.gridUnit * 97.5}px;
left: ${theme.gridUnit * 3.25}px;
```
Unit values should stay within the Superset theme
##########
superset-frontend/src/views/CRUD/data/dataset/AddDataset/LeftPanel/LeftPanel.test.tsx:
##########
@@ -17,15 +17,69 @@
* under the License.
*/
import React from 'react';
+import { SupersetClient } from '@superset-ui/core';
import { render, screen } from 'spec/helpers/testing-library';
import LeftPanel from 'src/views/CRUD/data/dataset/AddDataset/LeftPanel';
describe('LeftPanel', () => {
- it('renders a blank state LeftPanel', () => {
- render(<LeftPanel />);
+ const mockFun = jest.fn();
+
+ const SupersetClientGet = jest.spyOn(SupersetClient, 'get');
+
+ const getSchemaMockFunction = async () =>
+ ({
+ json: {
+ result: ['schema_a', 'schema_b'],
+ },
+ } as any);
+
+ const getTableMockFunction = async () =>
+ ({
+ json: {
+ options: [
+ { label: 'table_a', value: 'table_a' },
+ { label: 'table_b', value: 'table_b' },
+ { label: 'table_c', value: 'table_c' },
+ { label: 'table_d', value: 'table_d' },
+ ],
+ },
+ } as any);
+
+ it('should render', () => {
+ const { container } = render(<LeftPanel setDataset={mockFun} />, {
+ useRedux: true,
+ });
+ expect(container).toBeInTheDocument();
+ });
+
+ it('should render tableselector and databaselector container and selects',
() => {
+ render(<LeftPanel setDataset={mockFun} />, { useRedux: true });
+
+ expect(screen.getByText(/select database & schema/i)).toBeVisible();
+
+ const databaseSelect = screen.getByRole('combobox', {
+ name: 'Select database or type database name',
+ });
+ const schemaSelect = screen.getByRole('combobox', {
+ name: 'Select schema or type schema name',
+ });
+ expect(databaseSelect).toBeInTheDocument();
+ expect(schemaSelect).toBeInTheDocument();
+ });
+ it('renders a blank state LeftPanel if there is no schema selected', () => {
+ render(<LeftPanel setDataset={mockFun} />, { useRedux: true });
expect(screen.getByRole('img', { name: /empty/i })).toBeVisible();
expect(screen.getByText(/no database tables found/i)).toBeVisible();
expect(screen.getByText(/try selecting a different
schema/i)).toBeVisible();
});
+ it('renders list of options when user clicks on schema', () => {
+ render(<LeftPanel setDataset={mockFun} schema="schema_a" dbId={1} />, {
+ useRedux: true,
+ });
+ // screen.debug('container', container)
Review Comment:
Remove this
##########
superset-frontend/src/views/CRUD/data/dataset/AddDataset/LeftPanel/index.tsx:
##########
@@ -16,18 +16,227 @@
* specific language governing permissions and limitations
* under the License.
*/
-import React from 'react';
-import { t } from '@superset-ui/core';
+import React, {
+ useEffect,
+ useState,
+ useMemo,
+ SetStateAction,
+ Dispatch,
+} from 'react';
+import { SupersetClient, t, styled, FAST_DEBOUNCE } from '@superset-ui/core';
+import { Input } from 'src/components/Input';
+import { Form } from 'src/components/Form';
+import { TableOption, Table } from 'src/components/TableSelector';
+import RefreshLabel from 'src/components/RefreshLabel';
+import Loading from 'src/components/Loading';
+import DatabaseSelector from 'src/components/DatabaseSelector';
+import { debounce } from 'lodash';
import { EmptyStateMedium } from 'src/components/EmptyState';
+import { useToasts } from 'src/components/MessageToasts/withToasts';
+import { DatasetActionType, DatasetObject } from '../types';
+
+interface LeftPanelProps {
+ setDataset: Dispatch<SetStateAction<object>>;
+ schema?: string | undefined | null;
+ dbId?: number;
+}
+
+const LeftPanelStyle = styled.div`
+ ${({ theme }) => `
+ max-width: ${theme.gridUnit * 87.5}px;
+ padding: ${theme.gridUnit * 4}px;
+ height: 100%;
+ background-color: ${theme.colors.grayscale.light5};
+ position: relative;
+ .emptystate {
+ height: auto;
+ margin-top: 70px;
+ }
+ .refresh {
+ position: absolute;
+ top: ${theme.gridUnit * 43.25}px;
+ left: ${theme.gridUnit * 16.75}px;
+ span[role="button"]{
+ font-size: ${theme.gridUnit * 4.25}px;
+ }
+ }
+ .section-title {
+ margin-top: ${theme.gridUnit * 11}px;
+ margin-bottom: ${theme.gridUnit * 11}px;
+ font-weight: ${theme.typography.weights.bold};
+ }
+ .table-title {
+ margin-top: ${theme.gridUnit * 11}px;
+ margin-bottom: ${theme.gridUnit * 6}px;
+ font-weight: ${theme.typography.weights.bold};
+ }
+ .options-list {
+ overflow: auto;
+ position: absolute;
+ bottom: 0px;
+ top: 390px;
+ left: 13px;
+ right: 0;
+ .options {
+ padding: ${theme.gridUnit * 1.75}px;
+ border-radius: ${theme.borderRadius}px;
+ }
+ }
+ form > span {
+ position: absolute;
+ top: ${theme.gridUnit * 73}px;
+ left: ${theme.gridUnit * 42.75}px;
+ font-size: ${theme.gridUnit * 4.25}px;
+ }
+ .table-form {
+ margin-bottom: ${theme.gridUnit * 8}px;
+ }
+ .loading {
+ position: absolute;
+ bottom: ${theme.gridUnit * 95}px;
+ img {
+ position: absolute;
+ top: -${theme.gridUnit * 13.25}px;
+ right: -${theme.gridUnit * 3.75}px;
+ width: ${theme.gridUnit * 17.75}px;
+ }
+ }
+ }
+`}
+`;
+
+export default function LeftPanel({
+ setDataset,
+ schema,
+ dbId,
+}: LeftPanelProps) {
+ const [tableOptions, setTableOptions] = useState<Array<TableOption>>([]);
+ const [resetTables, setResetTables] = useState(false);
+ const [loadTables, setLoadTables] = useState(false);
+ const [searchVal, setSearchVal] = useState('');
+ const [refresh, setRefresh] = useState(false);
+
+ const { addDangerToast } = useToasts();
+
+ const setDatabase = (db: Partial<DatasetObject>) => {
+ setDataset({ type: DatasetActionType.selectDatabase, payload: db });
+ setResetTables(true);
+ };
+
+ const getTablesList = (url: string) => {
+ SupersetClient.get({ url })
+ .then(({ json }) => {
+ const options: TableOption[] = json.options.map((table: Table) => {
+ const option: TableOption = {
+ value: table.value,
+ label: <TableOption table={table} />,
+ text: table.label,
+ };
+
+ return option;
+ });
+
+ setTableOptions(options);
+ setLoadTables(false);
+ setResetTables(false);
+ setRefresh(false);
+ })
+ .catch(e => {
+ console.log('error', e);
+ });
+ };
+
+ const setSchema = (schema: string) => {
+ if (schema) {
+ setDataset({ type: DatasetActionType.selectSchema, payload: schema });
+ setLoadTables(true);
+ }
+ setResetTables(true);
+ };
+
+ const encodedSchema = encodeURIComponent(schema as string);
+
+ useEffect(() => {
+ if (loadTables) {
+ const endpoint = encodeURI(
+ `/superset/tables/${dbId}/${encodedSchema}/undefined/${refresh}/`,
+ );
+ getTablesList(endpoint);
+ }
+ }, [loadTables]);
+
+ useEffect(() => {
+ if (resetTables) {
+ setTableOptions([]);
+ setResetTables(false);
+ }
+ }, [resetTables]);
+
+ const search = useMemo(
+ () =>
+ debounce((value: string) => {
+ const encodeTableName =
+ value === '' ? undefined : encodeURIComponent(value);
+ const endpoint = encodeURI(
+ `/superset/tables/${dbId}/${encodedSchema}/${encodeTableName}/`,
+ );
+ getTablesList(endpoint);
+ }, FAST_DEBOUNCE),
+ [dbId, encodedSchema],
+ );
-export default function LeftPanel() {
return (
- <>
- <EmptyStateMedium
- image="empty-table.svg"
- title={t('No database tables found')}
- description={t('Try selecting a different schema')}
+ <LeftPanelStyle>
+ <p className="section-title db-schema"> Select Database & Schema</p>
+ <DatabaseSelector
+ handleError={addDangerToast}
+ onDbChange={setDatabase}
+ onSchemaChange={setSchema}
/>
- </>
+ {loadTables && (
+ <div className="loading">
+ <Loading position="inline" />
+ <p>loading schemas ...</p>
Review Comment:
It looks like the Shortcut ticket wants this text to say "Tables loading"
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]