Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: hack db connection modal ctabtns #20017

Merged
merged 11 commits into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions superset-frontend/src/SqlLab/components/SqlEditorLeftBar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import React, {
Dispatch,
SetStateAction,
} from 'react';
import querystring from 'query-string';
import Button from 'src/components/Button';
import { t, styled, css, SupersetTheme } from '@superset-ui/core';
import Collapse from 'src/components/Collapse';
Expand All @@ -34,6 +35,11 @@ import { IconTooltip } from 'src/components/IconTooltip';
import { QueryEditor } from 'src/SqlLab/types';
import { DatabaseObject } from 'src/components/DatabaseSelector';
import { EmptyStateSmall } from 'src/components/EmptyState';
import {
getItem,
LocalStorageKeys,
setItem,
} from 'src/utils/localStorageHelpers';
import TableElement, { Table, TableElementProps } from '../TableElement';

interface ExtendedTable extends Table {
Expand Down Expand Up @@ -105,10 +111,26 @@ export default function SqlEditorLeftBar({
// that require and modify the queryEditor
const queryEditorRef = useRef<QueryEditor>(queryEditor);
const [emptyResultsWithSearch, setEmptyResultsWithSearch] = useState(false);
const [userSelectedDb, setUserSelected] = useState<DatabaseObject | null>(
null,
);

useEffect(() => {
const bool = querystring.parse(window.location.search).db;
const userSelected = getItem(
LocalStorageKeys.db,
null,
) as DatabaseObject | null;

if (bool && userSelected) {
setUserSelected(userSelected);
setItem(LocalStorageKeys.db, null);
} else setUserSelected(database);
}, []);

useEffect(() => {
queryEditorRef.current = queryEditor;
}, [queryEditor]);
}, [queryEditor, database]);

const onEmptyResults = (searchText?: string) => {
setEmptyResultsWithSearch(!!searchText);
Expand Down Expand Up @@ -222,7 +244,7 @@ export default function SqlEditorLeftBar({
<TableSelectorMultiple
onEmptyResults={onEmptyResults}
emptyState={emptyStateComponent}
database={database}
database={userSelectedDb}
getDbList={actions.setDatabases}
handleError={actions.addDangerToast}
onDbChange={onDbChange}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export type DatabaseObject = {
type SchemaValue = { label: string; value: string };

export interface DatabaseSelectorProps {
db?: DatabaseObject;
db?: DatabaseObject | null;
emptyState?: ReactNode;
formMode?: boolean;
getDbList?: (arg0: any) => {};
Expand Down
9 changes: 6 additions & 3 deletions superset-frontend/src/components/TableSelector/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const TableLabel = styled.span`

interface TableSelectorProps {
clearable?: boolean;
database?: DatabaseObject;
database?: DatabaseObject | null;
emptyState?: ReactNode;
formMode?: boolean;
getDbList?: (arg0: any) => {};
Expand Down Expand Up @@ -166,12 +166,11 @@ const TableSelector: FunctionComponent<TableSelectorProps> = ({
onTableSelectChange,
}) => {
const [currentDatabase, setCurrentDatabase] = useState<
DatabaseObject | undefined
DatabaseObject | null | undefined
>(database);
const [currentSchema, setCurrentSchema] = useState<string | undefined>(
schema,
);

const [tableOptions, setTableOptions] = useState<TableOption[]>([]);
const [tableSelectValue, setTableSelectValue] = useState<
SelectValue | undefined
Expand All @@ -190,6 +189,10 @@ const TableSelector: FunctionComponent<TableSelectorProps> = ({
}
}, [database, tableSelectMode]);

useEffect(() => {
setCurrentDatabase(database);
}, [database]);

useEffect(() => {
if (tableSelectMode === 'single') {
setTableSelectValue(
Expand Down
2 changes: 2 additions & 0 deletions superset-frontend/src/utils/localStorageHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export enum LocalStorageKeys {
* and therefore should be done in a major release.
*/
filter_box_transition_snoozed_at = 'filter_box_transition_snoozed_at',
db = 'db',
chart_split_sizes = 'chart_split_sizes',
controls_width = 'controls_width',
datasource_width = 'datasource_width',
Expand All @@ -54,6 +55,7 @@ export enum LocalStorageKeys {

export type LocalStorageValues = {
filter_box_transition_snoozed_at: Record<number, number>;
db: object | null;
chart_split_sizes: [number, number];
controls_width: number;
datasource_width: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ const ModalHeader = ({
<StyledFormHeader>
<p className="helper-top"> STEP 3 OF 3 </p>
<h4 className="step-3-text">
Your database was successfully connected! Here are some optional
settings for your database
Your database was successfully connected! Create a dataset to begin
visualizing your data as a chart or go to SQL Lab to query your data.
</h4>
<p className="helper-bottom">
Need help? Learn more about{' '}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import React, {
useReducer,
Reducer,
} from 'react';
import { setItem, LocalStorageKeys } from 'src/utils/localStorageHelpers';
import { UploadChangeParam, UploadFile } from 'antd/lib/upload/interface';
import Tabs from 'src/components/Tabs';
import { AntdSelect, Upload } from 'src/components';
Expand Down Expand Up @@ -188,6 +189,11 @@ type DBReducerActionType =
};
};

const StyledBtns = styled.div`
margin-bottom: ${({ theme }) => theme.gridUnit * 3}px;
margin-left: ${({ theme }) => theme.gridUnit * 3}px;
`;

function dbReducer(
state: Partial<DatabaseObject> | null,
action: DBReducerActionType,
Expand Down Expand Up @@ -1098,6 +1104,39 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
return <></>;
};

const fetchAndSetDB = () => {
setLoading(true);
fetchResource(dbFetched?.id as number).then(r => {
setItem(LocalStorageKeys.db, r);
});
};

const renderCTABtns = () => (
<StyledBtns>
<Button
// eslint-disable-next-line no-return-assign
buttonStyle="default"
onClick={() => {
fetchAndSetDB();
window.location.href = '/tablemodelview/list';
}}
>
{' '}
{t('CREATE A DATASET')}{' '}
</Button>
<Button
buttonStyle="default"
// eslint-disable-next-line no-return-assign
onClick={() => {
fetchAndSetDB();
window.location.href = `/superset/sqllab/?db=true`;
}}
>
{t('QUERY DATA IN SQL LAB')}{' '}
</Button>
</StyledBtns>
);

const renderFinishState = () => {
if (!editNewDb) {
return (
Expand Down Expand Up @@ -1430,6 +1469,7 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
dbModel={dbModel}
editNewDb={editNewDb}
/>
{renderCTABtns()}
{renderFinishState()}
</>
) : (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ export const marginBottom = (theme: SupersetTheme) => css`
`;

export const StyledFormHeader = styled.header`
border-bottom: ${({ theme }) => `${theme.gridUnit * 0.25}px solid
${theme.colors.grayscale.light2};`}
padding: ${({ theme }) => theme.gridUnit * 2}px
${({ theme }) => theme.gridUnit * 4}px;
line-height: ${({ theme }) => theme.gridUnit * 6}px;
Expand Down
14 changes: 14 additions & 0 deletions superset-frontend/src/views/CRUD/data/dataset/AddDatasetModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ import Modal from 'src/components/Modal';
import TableSelector from 'src/components/TableSelector';
import withToasts from 'src/components/MessageToasts/withToasts';
import { DatabaseObject } from 'src/components/DatabaseSelector';
import {
getItem,
LocalStorageKeys,
setItem,
} from 'src/utils/localStorageHelpers';

type DatasetAddObject = {
id: number;
Expand Down Expand Up @@ -66,6 +71,14 @@ const DatasetModal: FunctionComponent<DatasetModalProps> = ({
setDisableSave(currentDatabase === undefined || currentTableName === '');
}, [currentTableName, currentDatabase]);

useEffect(() => {
const currentUserSelectedDb = getItem(
LocalStorageKeys.db,
null,
) as DatabaseObject;
if (currentUserSelectedDb) setCurrentDatabase(currentUserSelectedDb);
}, []);

const onDbChange = (db: DatabaseObject) => {
setCurrentDatabase(db);
};
Expand All @@ -86,6 +99,7 @@ const DatasetModal: FunctionComponent<DatasetModalProps> = ({
};

const hide = () => {
setItem(LocalStorageKeys.db, null);
clearModal();
onHide();
};
Expand Down
8 changes: 8 additions & 0 deletions superset-frontend/src/views/CRUD/data/dataset/DatasetList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ import React, {
useState,
useMemo,
useCallback,
useEffect,
} from 'react';
import rison from 'rison';
import {
createFetchRelated,
createFetchDistinct,
createErrorHandler,
} from 'src/views/CRUD/utils';
import { getItem, LocalStorageKeys } from 'src/utils/localStorageHelpers';
import { ColumnObject } from 'src/views/CRUD/data/dataset/types';
import { useListViewResource } from 'src/views/CRUD/hooks';
import ConfirmStatusChange from 'src/components/ConfirmStatusChange';
Expand Down Expand Up @@ -177,6 +179,12 @@ const DatasetList: FunctionComponent<DatasetListProps> = ({
const canExport = hasPerm('can_export');

const initialSort = SORT_BY;
useEffect(() => {
const db = getItem(LocalStorageKeys.db, null);
if (!loading && db) {
setDatasetAddModalOpen(true);
}
}, [loading]);

const openDatasetEditModal = useCallback(
({ id }: Dataset) => {
Expand Down