Skip to content

Commit

Permalink
Revert "fix: Address regression introduced in apache#21284 (apache#21470
Browse files Browse the repository at this point in the history
)"

This reverts commit 8c16806.
  • Loading branch information
mayurnewase committed Sep 21, 2022
1 parent c6dc8fb commit 219048f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
2 changes: 1 addition & 1 deletion superset-frontend/src/components/TableSelector/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ const TableSelector: FunctionComponent<TableSelectorProps> = ({
? data.options.map(table => ({
value: table.value,
label: <TableOption table={table} />,
text: table.value,
text: table.label,
}))
: [],
[data],
Expand Down
31 changes: 29 additions & 2 deletions superset-frontend/src/hooks/apiResources/tables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,46 @@ 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 () => {
result.current.refetch();
});
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,
Expand Down
11 changes: 7 additions & 4 deletions superset-frontend/src/hooks/apiResources/tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export type FetchTablesQueryParams = {
dbId?: string | number;
schema?: string;
forceRefresh?: boolean;
keyword?: string;
};
export interface Table {
label: string;
Expand Down Expand Up @@ -51,24 +52,26 @@ 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<QueryData>;
}

type Params = FetchTablesQueryParams &
Pick<UseQueryOptions, 'onSuccess' | 'onError'>;

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<QueryData, Error, Data>(
['tables', { dbId, schema }],
['tables', { dbId, schema, keyword }],
() => fetchTables({ ...params, forceRefresh: forceRefreshRef.current }),
{
select: ({ json }) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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),
Expand Down

0 comments on commit 219048f

Please sign in to comment.