Skip to content

Commit

Permalink
fix: Address regression introduced in #21284 (#21470)
Browse files Browse the repository at this point in the history
Co-authored-by: Elizabeth Thompson <[email protected]>
  • Loading branch information
john-bodley and eschutho authored Sep 16, 2022
1 parent b739e27 commit 8c16806
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 41 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.label,
text: table.value,
}))
: [],
[data],
Expand Down
31 changes: 2 additions & 29 deletions superset-frontend/src/hooks/apiResources/tables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,46 +103,19 @@ describe('useTables hook', () => {
});
expect(SupersetClient.get).toHaveBeenCalledTimes(1);
expect(SupersetClient.get).toHaveBeenCalledWith({
endpoint: `/superset/tables/${expectDbId}/${expectedSchema}/undefined/${forceRefresh}/`,
endpoint: `/superset/tables/${expectDbId}/${expectedSchema}/${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}/undefined/true/`,
endpoint: `/superset/tables/${expectDbId}/${expectedSchema}/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: 4 additions & 7 deletions superset-frontend/src/hooks/apiResources/tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export type FetchTablesQueryParams = {
dbId?: string | number;
schema?: string;
forceRefresh?: boolean;
keyword?: string;
};
export interface Table {
label: string;
Expand Down Expand Up @@ -52,26 +51,24 @@ 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}/${encodedKeyword}/${forceRefresh}/`;
}/${encodedSchema}/${forceRefresh}/`;
return SupersetClient.get({ endpoint }) as Promise<QueryData>;
}

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

export function useTables(options: Params) {
const { dbId, schema, keyword, onSuccess, onError } = options || {};
const { dbId, schema, onSuccess, onError } = options || {};
const forceRefreshRef = useRef(false);
const params = { dbId, schema, keyword };
const params = { dbId, schema };
const result = useQuery<QueryData, Error, Data>(
['tables', { dbId, schema, keyword }],
['tables', { dbId, schema }],
() => 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}/undefined/${refresh}/`,
`/superset/tables/${dbId}/${encodedSchema}/${refresh}/`,
);
getTablesList(endpoint);
}
Expand All @@ -188,10 +188,8 @@ export default function LeftPanel({
const search = useMemo(
() =>
debounce((value: string) => {
const encodeTableName =
value === '' ? undefined : encodeURIComponent(value);
const endpoint = encodeURI(
`/superset/tables/${dbId}/${encodedSchema}/${encodeTableName}/`,
`/superset/tables/${dbId}/${encodedSchema}/`,
);
getTablesList(endpoint);
}, FAST_DEBOUNCE),
Expand Down

0 comments on commit 8c16806

Please sign in to comment.