From 219048f2817d3f2ce708b8fbe1edbbf082c6a6c2 Mon Sep 17 00:00:00 2001 From: Mayur Date: Wed, 21 Sep 2022 10:49:11 +0530 Subject: [PATCH] Revert "fix: Address regression introduced in #21284 (#21470)" This reverts commit 8c16806f5759ecc53ecef88a2e96e2e0964bffc6. --- .../src/components/TableSelector/index.tsx | 2 +- .../src/hooks/apiResources/tables.test.ts | 31 +++++++++++++++++-- .../src/hooks/apiResources/tables.ts | 11 ++++--- .../dataset/AddDataset/LeftPanel/index.tsx | 6 ++-- 4 files changed, 41 insertions(+), 9 deletions(-) diff --git a/superset-frontend/src/components/TableSelector/index.tsx b/superset-frontend/src/components/TableSelector/index.tsx index 691590b95e4b3..acf9f67f7cdb2 100644 --- a/superset-frontend/src/components/TableSelector/index.tsx +++ b/superset-frontend/src/components/TableSelector/index.tsx @@ -196,7 +196,7 @@ const TableSelector: FunctionComponent = ({ ? data.options.map(table => ({ value: table.value, label: , - text: table.value, + text: table.label, })) : [], [data], diff --git a/superset-frontend/src/hooks/apiResources/tables.test.ts b/superset-frontend/src/hooks/apiResources/tables.test.ts index 6ff19cb401fb3..11afef6b12c1a 100644 --- a/superset-frontend/src/hooks/apiResources/tables.test.ts +++ b/superset-frontend/src/hooks/apiResources/tables.test.ts @@ -103,7 +103,7 @@ describe('useTables hook', () => { }); expect(SupersetClient.get).toHaveBeenCalledTimes(1); expect(SupersetClient.get).toHaveBeenCalledWith({ - endpoint: `/superset/tables/${expectDbId}/${expectedSchema}/${forceRefresh}/`, + endpoint: `/superset/tables/${expectDbId}/${expectedSchema}/undefined/${forceRefresh}/`, }); expect(result.current.data).toEqual(expectedData); await act(async () => { @@ -111,11 +111,38 @@ describe('useTables hook', () => { }); expect(SupersetClient.get).toHaveBeenCalledTimes(2); expect(SupersetClient.get).toHaveBeenCalledWith({ - endpoint: `/superset/tables/${expectDbId}/${expectedSchema}/true/`, + endpoint: `/superset/tables/${expectDbId}/${expectedSchema}/undefined/true/`, }); expect(result.current.data).toEqual(expectedData); }); + it('returns api response for search keyword', async () => { + const expectDbId = 'db1'; + const expectedSchema = 'schemaA'; + const expectedKeyword = 'my work'; + const forceRefresh = false; + renderHook( + () => + useTables({ + dbId: expectDbId, + schema: expectedSchema, + keyword: expectedKeyword, + }), + { + wrapper: QueryProvider, + }, + ); + await act(async () => { + jest.runAllTimers(); + }); + expect(SupersetClient.get).toHaveBeenCalledTimes(1); + expect(SupersetClient.get).toHaveBeenCalledWith({ + endpoint: `/superset/tables/${expectDbId}/${expectedSchema}/${encodeURIComponent( + expectedKeyword, + )}/${forceRefresh}/`, + }); + }); + it('returns hasMore when total is larger than result size', async () => { (SupersetClient.get as jest.Mock).mockResolvedValueOnce( fakeHasMoreApiResult, diff --git a/superset-frontend/src/hooks/apiResources/tables.ts b/superset-frontend/src/hooks/apiResources/tables.ts index 994a7380f496f..80dd5001aceac 100644 --- a/superset-frontend/src/hooks/apiResources/tables.ts +++ b/superset-frontend/src/hooks/apiResources/tables.ts @@ -24,6 +24,7 @@ export type FetchTablesQueryParams = { dbId?: string | number; schema?: string; forceRefresh?: boolean; + keyword?: string; }; export interface Table { label: string; @@ -51,12 +52,14 @@ export function fetchTables({ dbId, schema, forceRefresh, + keyword, }: FetchTablesQueryParams) { const encodedSchema = schema ? encodeURIComponent(schema) : ''; + const encodedKeyword = keyword ? encodeURIComponent(keyword) : 'undefined'; // TODO: Would be nice to add pagination in a follow-up. Needs endpoint changes. const endpoint = `/superset/tables/${ dbId ?? 'undefined' - }/${encodedSchema}/${forceRefresh}/`; + }/${encodedSchema}/${encodedKeyword}/${forceRefresh}/`; return SupersetClient.get({ endpoint }) as Promise; } @@ -64,11 +67,11 @@ type Params = FetchTablesQueryParams & Pick; export function useTables(options: Params) { - const { dbId, schema, onSuccess, onError } = options || {}; + const { dbId, schema, keyword, onSuccess, onError } = options || {}; const forceRefreshRef = useRef(false); - const params = { dbId, schema }; + const params = { dbId, schema, keyword }; const result = useQuery( - ['tables', { dbId, schema }], + ['tables', { dbId, schema, keyword }], () => fetchTables({ ...params, forceRefresh: forceRefreshRef.current }), { select: ({ json }) => ({ diff --git a/superset-frontend/src/views/CRUD/data/dataset/AddDataset/LeftPanel/index.tsx b/superset-frontend/src/views/CRUD/data/dataset/AddDataset/LeftPanel/index.tsx index 1b40e42299a45..9d79d5cc8f6e9 100644 --- a/superset-frontend/src/views/CRUD/data/dataset/AddDataset/LeftPanel/index.tsx +++ b/superset-frontend/src/views/CRUD/data/dataset/AddDataset/LeftPanel/index.tsx @@ -172,7 +172,7 @@ export default function LeftPanel({ useEffect(() => { if (loadTables) { const endpoint = encodeURI( - `/superset/tables/${dbId}/${encodedSchema}/${refresh}/`, + `/superset/tables/${dbId}/${encodedSchema}/undefined/${refresh}/`, ); getTablesList(endpoint); } @@ -188,8 +188,10 @@ export default function LeftPanel({ const search = useMemo( () => debounce((value: string) => { + const encodeTableName = + value === '' ? undefined : encodeURIComponent(value); const endpoint = encodeURI( - `/superset/tables/${dbId}/${encodedSchema}/`, + `/superset/tables/${dbId}/${encodedSchema}/${encodeTableName}/`, ); getTablesList(endpoint); }, FAST_DEBOUNCE),