You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by GitBox <gi...@apache.org> on 2022/01/31 13:35:26 UTC

[GitHub] [superset] villebro commented on a change in pull request #18215: feat(explore): Keep or reset chart config after datasource change

villebro commented on a change in pull request #18215:
URL: https://github.com/apache/superset/pull/18215#discussion_r795642206



##########
File path: superset-frontend/src/explore/components/ControlPanelAlert.tsx
##########
@@ -0,0 +1,98 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import React from 'react';
+import { styled } from '@superset-ui/core';
+import Button from 'src/components/Button';
+
+interface ControlPanelAlertI {

Review comment:
       The `I` here gave me a horrible C# flashback 👻 https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/interface (granted, it's trailing, not leading). I did a quick search of the codebase, and it seems like we usually suffix these with `Interface` when there would otherwise be overlap with a component name. At the risk of being awarded the nitpicker of the year award, maybe we could change this to `ControlPanelAlertInterface` to keep with conventions..? 😄 

##########
File path: superset-frontend/src/explore/components/ControlPanelsContainer.tsx
##########
@@ -190,57 +174,79 @@ function getState(
     expandedCustomizeSections,
     querySections,
     customizeSections,
-    loading: false,
   };
 }
 
-export class ControlPanelsContainer extends React.Component<
-  ControlPanelsContainerProps,
-  ControlPanelsContainerState
-> {
-  // trigger updates to the component when async plugins load
-  static contextType = PluginContext;
-
-  constructor(props: ControlPanelsContainerProps) {
-    super(props);
-    this.state = {
-      expandedQuerySections: [],
-      expandedCustomizeSections: [],
-      querySections: [],
-      customizeSections: [],
-      loading: false,
-    };
-    this.renderControl = this.renderControl.bind(this);
-    this.renderControlPanelSection = this.renderControlPanelSection.bind(this);
-  }
+export const ControlPanelsContainer = (props: ControlPanelsContainerProps) => {
+  const pluginContext = useContext(PluginContext);
 
-  componentDidUpdate(prevProps: ControlPanelsContainerProps) {
-    if (
-      this.props.form_data.datasource !== prevProps.form_data.datasource ||
-      this.props.form_data.viz_type !== prevProps.form_data.viz_type
-    ) {
-      // eslint-disable-next-line react/no-did-update-set-state
-      this.setState(getState(this.props));
-    }
-  }
+  const prevDatasource = usePrevious(props.exploreState.datasource);
 
-  // required for an Antd bug that would otherwise malfunction re-rendering
-  // a collapsed panel after changing the datasource or viz type
-  UNSAFE_componentWillReceiveProps(nextProps: ControlPanelsContainerProps) {
+  const [expandedQuerySections, setExpandedQuerySections] = useState<string[]>(
+    [],
+  );
+  const [expandedCustomizeSections, setExpandedCustomizeSections] = useState<
+    string[]
+  >([]);
+  const [querySections, setQuerySections] = useState<
+    ControlPanelSectionConfig[]
+  >([]);
+  const [customizeSections, setCustomizeSections] = useState<
+    ControlPanelSectionConfig[]
+  >([]);
+  const [showDatasourceAlert, setShowDatasourceAlert] = useState(false);
+
+  useEffect(() => {
     if (
-      this.props.form_data.datasource !== nextProps.form_data.datasource ||
-      this.props.form_data.viz_type !== nextProps.form_data.viz_type
+      prevDatasource &&
+      (props.exploreState.datasource?.id !== prevDatasource.id ||
+        props.exploreState.datasource?.type !== prevDatasource.type)
     ) {
-      this.setState({ loading: true });
+      setShowDatasourceAlert(true);
     }
-  }
+  }, [
+    props.exploreState.datasource?.id,
+    props.exploreState.datasource?.type,
+    prevDatasource,
+  ]);
+
+  useEffect(() => {
+    const {
+      expandedQuerySections: newExpandedQuerySections,
+      expandedCustomizeSections: newExpandedCustomizeSections,
+      querySections: newQuerySections,
+      customizeSections: newCustomizeSections,
+    } = getState(
+      props.form_data.viz_type,
+      props.exploreState.datasource,
+      props.datasource_type,
+    );
+    setExpandedQuerySections(newExpandedQuerySections);
+    setExpandedCustomizeSections(newExpandedCustomizeSections);
+    setQuerySections(newQuerySections);
+    setCustomizeSections(newCustomizeSections);
+  }, [props.form_data.datasource, props.form_data.viz_type]);
+
+  const resetTransferredControls = useCallback(() => {
+    ensureIsArray(props.exploreState.controlsTransferred).forEach(controlName =>
+      props.actions.setControlValue(
+        controlName,
+        props.controls[controlName].default,
+      ),
+    );
+  }, [props.actions, props.exploreState.controlsTransferred, props.controls]);
 
-  componentDidMount() {
-    this.setState(getState(this.props));
-  }
+  const handleClearFormClick = useCallback(() => {
+    resetTransferredControls();
+    setShowDatasourceAlert(false);
+  }, [resetTransferredControls]);
+
+  const handleContinueClick = useCallback(() => {
+    setShowDatasourceAlert(false);
+  }, []);
 
-  renderControl({ name, config }: CustomControlItem) {
-    const { actions, controls, chart, exploreState } = this.props;
+  const renderControl = ({ name, config }: CustomControlItem) => {
+    const { controls, chart } = props;

Review comment:
       It seems we're missing `exploreState` here (needed down on line 257):
   ```suggestion
       const { controls, chart, exploreState } = props;
   ```




-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org