lyndsiWilliams commented on code in PR #21186:
URL: https://github.com/apache/superset/pull/21186#discussion_r956430949
##########
superset-frontend/src/SqlLab/components/ResultSet/index.tsx:
##########
@@ -121,115 +113,96 @@ const LimitMessage = styled.span`
margin-left: ${({ theme }) => theme.gridUnit * 2}px;
`;
-export default class ResultSet extends React.PureComponent<
- ResultSetProps,
- ResultSetState
-> {
- static defaultProps = {
- cache: false,
- csv: true,
- database: {},
- search: true,
- showSql: false,
- visualize: true,
- };
-
- constructor(props: ResultSetProps) {
- super(props);
- this.state = {
- searchText: '',
- showExploreResultsButton: false,
- data: [],
- showSaveDatasetModal: false,
- alertIsOpen: false,
- };
- this.changeSearch = this.changeSearch.bind(this);
- this.fetchResults = this.fetchResults.bind(this);
- this.popSelectStar = this.popSelectStar.bind(this);
- this.reFetchQueryResults = this.reFetchQueryResults.bind(this);
- this.toggleExploreResultsButton =
- this.toggleExploreResultsButton.bind(this);
- }
-
- async componentDidMount() {
+const ResultSet = ({
+ actions,
+ cache = false,
+ csv = true,
+ database = {},
+ displayLimit,
+ height,
+ query,
+ search = true,
+ showSql = false,
+ visualize = true,
+ user,
+ defaultQueryLimit,
+}: ResultSetProps) => {
+ const [searchText, setSearchText] = useState('');
+ const [cachedData, setCachedData] = useState<Record<string, unknown>[]>([]);
+ const [showSaveDatasetModal, setShowSaveDatasetModal] = useState(false);
+ const [alertIsOpen, setAlertIsOpen] = useState(false);
+
+ useEffect(() => {
// only do this the first time the component is rendered/mounted
- this.reRunQueryIfSessionTimeoutErrorOnMount();
- }
+ reRunQueryIfSessionTimeoutErrorOnMount();
+ }, []);
- UNSAFE_componentWillReceiveProps(nextProps: ResultSetProps) {
- // when new results comes in, save them locally and clear in store
+ const prevQuery = usePrevious(query);
+ useEffect(() => {
if (
- this.props.cache &&
- !nextProps.query.cached &&
- nextProps.query.results &&
- nextProps.query.results.data &&
- nextProps.query.results.data.length > 0
+ cache &&
+ query.cached &&
+ query?.results?.data &&
+ query.results.data.length > 0
) {
- this.setState({ data: nextProps.query.results.data }, () =>
- this.clearQueryResults(nextProps.query),
- );
+ setCachedData(query.results.data);
+ clearQueryResults(query);
}
if (
- nextProps.query.resultsKey &&
- nextProps.query.resultsKey !== this.props.query.resultsKey
+ query.resultsKey &&
+ prevQuery?.resultsKey &&
+ query.resultsKey !== prevQuery.resultsKey
) {
- this.fetchResults(nextProps.query);
+ fetchResults(query);
}
- }
+ }, [query, cache]);
- calculateAlertRefHeight = (alertElement: HTMLElement | null) => {
+ const calculateAlertRefHeight = (alertElement: HTMLElement | null) => {
if (alertElement) {
- this.setState({ alertIsOpen: true });
+ setAlertIsOpen(true);
} else {
- this.setState({ alertIsOpen: false });
+ setAlertIsOpen(false);
}
};
- clearQueryResults(query: QueryResponse) {
- this.props.actions.clearQueryResults(query);
- }
+ const clearQueryResults = (query: QueryResponse) => {
+ actions.clearQueryResults(query);
+ };
- popSelectStar(tempSchema: string | null, tempTable: string) {
+ const popSelectStar = (tempSchema: string | null, tempTable: string) => {
const qe = {
id: shortid.generate(),
name: tempTable,
autorun: false,
- dbId: this.props.query.dbId,
+ dbId: query.dbId,
sql: `SELECT * FROM ${tempSchema ? `${tempSchema}.` : ''}${tempTable}`,
};
- this.props.actions.addQueryEditor(qe);
- }
-
- toggleExploreResultsButton() {
- this.setState(prevState => ({
- showExploreResultsButton: !prevState.showExploreResultsButton,
- }));
- }
+ actions.addQueryEditor(qe);
+ };
- changeSearch(event: React.ChangeEvent<HTMLInputElement>) {
- this.setState({ searchText: event.target.value });
- }
+ const changeSearch = (event: React.ChangeEvent<HTMLInputElement>) => {
+ setSearchText(event.target.value);
+ };
- fetchResults(query: QueryResponse) {
- this.props.actions.fetchQueryResults(query, this.props.displayLimit);
- }
+ const fetchResults = (query: QueryResponse) => {
+ actions.fetchQueryResults(query, displayLimit);
+ };
- reFetchQueryResults(query: QueryResponse) {
- this.props.actions.reFetchQueryResults(query);
- }
+ const reFetchQueryResults = (query: QueryResponse) => {
+ actions.reFetchQueryResults(query);
+ };
- reRunQueryIfSessionTimeoutErrorOnMount() {
- const { query } = this.props;
+ const reRunQueryIfSessionTimeoutErrorOnMount = () => {
if (
query.errorMessage &&
query.errorMessage.indexOf('session timed out') > 0
Review Comment:
Oh I see! This is fine as is, in that case.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]