Skip to content

Commit

Permalink
chore: Use queryEditorId in SqlEditor child components (#21650)
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneTorap committed Nov 16, 2022
1 parent 2f0d5f1 commit d76f305
Show file tree
Hide file tree
Showing 17 changed files with 229 additions and 243 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ import configureStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import { render } from 'spec/helpers/testing-library';
import { Store } from 'redux';
import { initialState, defaultQueryEditor } from 'src/SqlLab/fixtures';
import {
initialState,
defaultQueryEditor,
extraQueryEditor1,
} from 'src/SqlLab/fixtures';

import EstimateQueryCostButton, {
EstimateQueryCostButtonProps,
Expand All @@ -43,7 +47,7 @@ jest.mock('src/components/Select/AsyncSelect', () => () => (
const setup = (props: Partial<EstimateQueryCostButtonProps>, store?: Store) =>
render(
<EstimateQueryCostButton
queryEditor={defaultQueryEditor}
queryEditorId={defaultQueryEditor.id}
getEstimate={jest.fn()}
{...props}
/>,
Expand All @@ -61,12 +65,8 @@ describe('EstimateQueryCostButton', () => {
});

it('renders label for selected query', async () => {
const queryEditorWithSelectedText = {
...defaultQueryEditor,
selectedText: 'SELECT',
};
const { queryByText } = setup(
{ queryEditor: queryEditorWithSelectedText },
{ queryEditorId: extraQueryEditor1.id },
mockStore(initialState),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,37 @@
* under the License.
*/
import React, { useMemo } from 'react';
import Alert from 'src/components/Alert';
import { useSelector } from 'react-redux';
import { t } from '@superset-ui/core';

import Alert from 'src/components/Alert';
import TableView from 'src/components/TableView';
import Button from 'src/components/Button';
import Loading from 'src/components/Loading';
import ModalTrigger from 'src/components/ModalTrigger';
import { EmptyWrapperType } from 'src/components/TableView/TableView';
import {
SqlLabRootState,
QueryCostEstimate,
QueryEditor,
} from 'src/SqlLab/types';
import { getUpToDateQuery } from 'src/SqlLab/actions/sqlLab';
import { useSelector } from 'react-redux';
import useQueryEditor from 'src/SqlLab/hooks/useQueryEditor';
import { SqlLabRootState, QueryCostEstimate } from 'src/SqlLab/types';

export interface EstimateQueryCostButtonProps {
getEstimate: Function;
queryEditor: QueryEditor;
queryEditorId: string;
tooltip?: string;
disabled?: boolean;
}

const EstimateQueryCostButton = ({
getEstimate,
queryEditor,
queryEditorId,
tooltip = '',
disabled = false,
}: EstimateQueryCostButtonProps) => {
const queryCostEstimate = useSelector<
SqlLabRootState,
QueryCostEstimate | undefined
>(state => state.sqlLab.queryCostEstimates?.[queryEditor.id]);
const selectedText = useSelector<SqlLabRootState, string | undefined>(
rootState =>
(getUpToDateQuery(rootState, queryEditor) as unknown as QueryEditor)
.selectedText,
);
>(state => state.sqlLab.queryCostEstimates?.[queryEditorId]);

const { selectedText } = useQueryEditor(queryEditorId, ['selectedText']);
const { cost } = queryCostEstimate || {};
const tableData = useMemo(() => (Array.isArray(cost) ? cost : []), [cost]);
const columns = useMemo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import React from 'react';
import { useDispatch } from 'react-redux';
import { styled, useTheme } from '@superset-ui/core';

import { AntdDropdown } from 'src/components';
import { Menu } from 'src/components/Menu';
import Icons from 'src/components/Icons';
Expand Down Expand Up @@ -83,12 +84,13 @@ const QueryLimitSelect = ({
maxRow,
defaultQueryLimit,
}: QueryLimitSelectProps) => {
const theme = useTheme();
const dispatch = useDispatch();

const queryEditor = useQueryEditor(queryEditorId, ['id', 'queryLimit']);
const queryLimit = queryEditor.queryLimit || defaultQueryLimit;
const dispatch = useDispatch();
const setQueryLimit = (updatedQueryLimit: number) =>
dispatch(queryEditorSetQueryLimit(queryEditor, updatedQueryLimit));
const theme = useTheme();

return (
<LimitSelectStyled>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { Store } from 'redux';
import { render, fireEvent, waitFor } from 'spec/helpers/testing-library';
import { initialState, defaultQueryEditor } from 'src/SqlLab/fixtures';
import RunQueryActionButton, {
Props,
RunQueryActionButtonProps,
} from 'src/SqlLab/components/RunQueryActionButton';

const middlewares = [thunk];
Expand All @@ -51,7 +51,7 @@ const defaultProps = {
overlayCreateAsMenu: null,
};

const setup = (props?: Partial<Props>, store?: Store) =>
const setup = (props?: Partial<RunQueryActionButtonProps>, store?: Store) =>
render(<RunQueryActionButton {...defaultProps} {...props} />, {
useRedux: true,
...(store && { store }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { detectOS } from 'src/utils/common';
import { QueryButtonProps } from 'src/SqlLab/types';
import useQueryEditor from 'src/SqlLab/hooks/useQueryEditor';

export interface Props {
export interface RunQueryActionButtonProps {
queryEditorId: string;
allowAsync: boolean;
queryState?: string;
Expand Down Expand Up @@ -81,14 +81,14 @@ const StyledButton = styled.span`
}
`;

const RunQueryActionButton: React.FC<Props> = ({
const RunQueryActionButton = ({
allowAsync = false,
queryEditorId,
queryState,
overlayCreateAsMenu,
runQuery,
stopQuery,
}) => {
}: RunQueryActionButtonProps) => {
const theme = useTheme();
const userOS = detectOS();

Expand Down
8 changes: 5 additions & 3 deletions superset-frontend/src/SqlLab/components/SaveQuery/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ const Styles = styled.span`
}
`;

export default function SaveQuery({
const SaveQuery = ({
queryEditorId,
onSave = () => {},
onUpdate,
saveQueryWarning = null,
database,
columns,
}: SaveQueryProps) {
}: SaveQueryProps) => {
const queryEditor = useQueryEditor(queryEditorId, [
'autorun',
'name',
Expand Down Expand Up @@ -230,4 +230,6 @@ export default function SaveQuery({
</Modal>
</Styles>
);
}
};

export default SaveQuery;
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { getClientErrorObject } from 'src/utils/getClientErrorObject';
import { FeatureFlag, isFeatureEnabled } from 'src/featureFlags';
import useQueryEditor from 'src/SqlLab/hooks/useQueryEditor';

interface ShareSqlLabQueryPropTypes {
interface ShareSqlLabQueryProps {
queryEditorId: string;
addDangerToast: (msg: string) => void;
}
Expand All @@ -42,10 +42,10 @@ const StyledIcon = styled(Icons.Link)`
}
`;

function ShareSqlLabQuery({
const ShareSqlLabQuery = ({
queryEditorId,
addDangerToast,
}: ShareSqlLabQueryPropTypes) {
}: ShareSqlLabQueryProps) => {
const theme = useTheme();

const { dbId, name, schema, autorun, sql, remoteId, templateParams } =
Expand Down Expand Up @@ -126,6 +126,6 @@ function ShareSqlLabQuery({
)}
</>
);
}
};

export default withToasts(ShareSqlLabQuery);
7 changes: 3 additions & 4 deletions superset-frontend/src/SqlLab/components/SqlEditor/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ const SqlEditor = ({
onChange={params => {
dispatch(queryEditorSetTemplateParams(qe, params));
}}
queryEditor={qe}
queryEditorId={qe.id}
/>
</Menu.Item>
)}
Expand Down Expand Up @@ -543,7 +543,7 @@ const SqlEditor = ({
<span>
<EstimateQueryCostButton
getEstimate={getQueryCostEstimate}
queryEditor={queryEditor}
queryEditorId={queryEditor.id}
tooltip={t('Estimate the cost before running a query')}
/>
</span>
Expand Down Expand Up @@ -657,9 +657,8 @@ const SqlEditor = ({
>
<SqlEditorLeftBar
database={database}
queryEditor={queryEditor}
queryEditorId={queryEditor.id}
tables={tables}
actions={actions}
setEmptyState={bool => setShowEmptyState(bool)}
/>
</StyledSidebar>
Expand Down

0 comments on commit d76f305

Please sign in to comment.