From 73077b1e939d8b01ec18b4f64cb9f358929fa509 Mon Sep 17 00:00:00 2001 From: EugeneTorap Date: Sun, 23 Jan 2022 14:57:27 +0300 Subject: [PATCH 1/9] Move ExploreResultsButton to FC & tsx --- .../{index.jsx => index.tsx} | 135 ++++++++---------- 1 file changed, 58 insertions(+), 77 deletions(-) rename superset-frontend/src/SqlLab/components/ExploreResultsButton/{index.jsx => index.tsx} (65%) diff --git a/superset-frontend/src/SqlLab/components/ExploreResultsButton/index.jsx b/superset-frontend/src/SqlLab/components/ExploreResultsButton/index.tsx similarity index 65% rename from superset-frontend/src/SqlLab/components/ExploreResultsButton/index.jsx rename to superset-frontend/src/SqlLab/components/ExploreResultsButton/index.tsx index 969a7255a2340..dfe7e005bc901 100644 --- a/superset-frontend/src/SqlLab/components/ExploreResultsButton/index.jsx +++ b/superset-frontend/src/SqlLab/components/ExploreResultsButton/index.tsx @@ -16,39 +16,41 @@ * specific language governing permissions and limitations * under the License. */ +import React, { FC } from 'react'; +import { useDispatch, useSelector } from 'react-redux'; import moment from 'moment'; -import React from 'react'; -import PropTypes from 'prop-types'; -import { bindActionCreators } from 'redux'; -import { connect } from 'react-redux'; import Alert from 'src/components/Alert'; import { t } from '@superset-ui/core'; import { InfoTooltipWithTrigger } from '@superset-ui/chart-controls'; import shortid from 'shortid'; import Button from 'src/components/Button'; -import * as actions from 'src/SqlLab/actions/sqlLab'; +import rootReducer from 'src/SqlLab/reducers'; -const propTypes = { - actions: PropTypes.object.isRequired, - query: PropTypes.object, - errorMessage: PropTypes.string, - timeout: PropTypes.number, - database: PropTypes.object.isRequired, - onClick: PropTypes.func.isRequired, -}; -const defaultProps = { - query: {}, -}; +type RootState = ReturnType; + +interface ExploreResultsButtonProps { + actions: { + createCtasDatasource: Function; + addInfoToast: Function; + addDangerToast: Function; + }; + query?: object; + database: object; + onClick: Function; +} -class ExploreResultsButton extends React.PureComponent { - constructor(props) { - super(props); - this.getInvalidColumns = this.getInvalidColumns.bind(this); - this.renderInvalidColumnMessage = - this.renderInvalidColumnMessage.bind(this); - } +const ExploreResultsButton: FC = ({ + actions, + query = {}, + database, + onClick, +}) => { + const dispatch = useDispatch(); + const errorMessage = useSelector( + (state: RootState) => state.sqlLab.errorMessage, + ); - getColumns() { + const getColumns = () => { const { props } = this; if ( props.query && @@ -58,24 +60,24 @@ class ExploreResultsButton extends React.PureComponent { return props.query.results.selected_columns; } return []; - } + }; - getQueryDuration() { + const getQueryDuration = () => { return moment .duration(this.props.query.endDttm - this.props.query.startDttm) .asSeconds(); - } + }; - getInvalidColumns() { + const getInvalidColumns = () => { const re1 = /__\d+$/; // duplicate column name pattern const re2 = /^__timestamp/i; // reserved temporal column alias return this.props.query.results.selected_columns .map(col => col.name) .filter(col => re1.test(col) || re2.test(col)); - } + }; - datasourceName() { + const datasourceName = () => { const { query } = this.props; const uniqueId = shortid.generate(); let datasourceName = uniqueId; @@ -84,9 +86,9 @@ class ExploreResultsButton extends React.PureComponent { datasourceName += `${query.tab}-${uniqueId}`; } return datasourceName; - } + }; - buildVizOptions() { + const buildVizOptions = () => { const { schema, sql, dbId, templateParams } = this.props.query; return { dbId, @@ -96,9 +98,9 @@ class ExploreResultsButton extends React.PureComponent { datasourceName: this.datasourceName(), columns: this.getColumns(), }; - } + }; - renderTimeoutWarning() { + const renderTimeoutWarning = () => { return ( ); - } + }; - renderInvalidColumnMessage() { + const renderInvalidColumnMessage = () => { const invalidColumns = this.getInvalidColumns(); if (invalidColumns.length === 0) { return null; @@ -147,47 +149,26 @@ class ExploreResultsButton extends React.PureComponent { invalid column names.`)} ); - } - - render() { - const allowsSubquery = - this.props.database && this.props.database.allows_subquery; - return ( - <> - - - ); - } -} -ExploreResultsButton.propTypes = propTypes; -ExploreResultsButton.defaultProps = defaultProps; - -function mapStateToProps({ sqlLab, common }) { - return { - errorMessage: sqlLab.errorMessage, - timeout: common.conf ? common.conf.SUPERSET_WEBSERVER_TIMEOUT : null, }; -} -function mapDispatchToProps(dispatch) { - return { - actions: bindActionCreators(actions, dispatch), - }; -} + const allowsSubquery = this.props.database && this.props.database.allows_subquery; + return ( + <> + + + ); +}; -export default connect( - mapStateToProps, - mapDispatchToProps, -)(ExploreResultsButton); +export default ExploreResultsButton; From 04eae5d7b1d4a3a5918ae950710616c24c8c6adc Mon Sep 17 00:00:00 2001 From: EugeneTorap Date: Sun, 23 Jan 2022 15:05:52 +0300 Subject: [PATCH 2/9] Refactoring --- .../components/ExploreResultsButton/index.tsx | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/superset-frontend/src/SqlLab/components/ExploreResultsButton/index.tsx b/superset-frontend/src/SqlLab/components/ExploreResultsButton/index.tsx index dfe7e005bc901..666d0234d70ab 100644 --- a/superset-frontend/src/SqlLab/components/ExploreResultsButton/index.tsx +++ b/superset-frontend/src/SqlLab/components/ExploreResultsButton/index.tsx @@ -51,20 +51,19 @@ const ExploreResultsButton: FC = ({ ); const getColumns = () => { - const { props } = this; if ( - props.query && - props.query.results && - props.query.results.selected_columns + query && + query.results && + query.results.selected_columns ) { - return props.query.results.selected_columns; + return query.results.selected_columns; } return []; }; const getQueryDuration = () => { return moment - .duration(this.props.query.endDttm - this.props.query.startDttm) + .duration(query.endDttm - query.startDttm) .asSeconds(); }; @@ -72,13 +71,12 @@ const ExploreResultsButton: FC = ({ const re1 = /__\d+$/; // duplicate column name pattern const re2 = /^__timestamp/i; // reserved temporal column alias - return this.props.query.results.selected_columns + return query.results.selected_columns .map(col => col.name) .filter(col => re1.test(col) || re2.test(col)); }; const datasourceName = () => { - const { query } = this.props; const uniqueId = shortid.generate(); let datasourceName = uniqueId; if (query) { @@ -89,14 +87,14 @@ const ExploreResultsButton: FC = ({ }; const buildVizOptions = () => { - const { schema, sql, dbId, templateParams } = this.props.query; + const { schema, sql, dbId, templateParams } = query; return { dbId, schema, sql, templateParams, - datasourceName: this.datasourceName(), - columns: this.getColumns(), + datasourceName: datasourceName(), + columns: getColumns(), }; }; @@ -108,7 +106,7 @@ const ExploreResultsButton: FC = ({ <> {t( 'This query took %s seconds to run, ', - Math.round(this.getQueryDuration()), + Math.round(getQueryDuration()), ) + t( 'and the explore view times out at %s seconds ', @@ -132,7 +130,7 @@ const ExploreResultsButton: FC = ({ }; const renderInvalidColumnMessage = () => { - const invalidColumns = this.getInvalidColumns(); + const invalidColumns = getInvalidColumns(); if (invalidColumns.length === 0) { return null; } @@ -151,12 +149,12 @@ const ExploreResultsButton: FC = ({ ); }; - const allowsSubquery = this.props.database && this.props.database.allows_subquery; + const allowsSubquery = database && database.allows_subquery; return ( <> - + ); }; From 190b2cbada161c759e309ce89f1927812107733b Mon Sep 17 00:00:00 2001 From: EugeneTorap Date: Fri, 28 Jan 2022 11:10:21 +0300 Subject: [PATCH 8/9] Refactoring --- .../src/SqlLab/components/ExploreResultsButton/index.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/superset-frontend/src/SqlLab/components/ExploreResultsButton/index.tsx b/superset-frontend/src/SqlLab/components/ExploreResultsButton/index.tsx index 535c15edad81c..ed08028f43d70 100644 --- a/superset-frontend/src/SqlLab/components/ExploreResultsButton/index.tsx +++ b/superset-frontend/src/SqlLab/components/ExploreResultsButton/index.tsx @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -import React, { FC } from 'react'; +import React from 'react'; import { t } from '@superset-ui/core'; import { InfoTooltipWithTrigger } from '@superset-ui/chart-controls'; import Button, { OnClickHandler } from 'src/components/Button'; @@ -28,10 +28,10 @@ interface ExploreResultsButtonProps { onClick: OnClickHandler; } -const ExploreResultsButton: FC = ({ +const ExploreResultsButton = ({ database, onClick, -}) => { +}: ExploreResultsButtonProps) => { const allowsSubquery = database?.allows_subquery ?? false; return (