This is an automated email from the ASF dual-hosted git repository.

yongjiezhao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/superset.git


The following commit(s) were added to refs/heads/master by this push:
     new a5df4c6  feat: default timepicker to last week when dataset is changed 
(#12609)
a5df4c6 is described below

commit a5df4c60a236a8cc2f84d8dd8e7a3a30794f0df9
Author: Phillip Kelley-Dotson <[email protected]>
AuthorDate: Mon Jan 25 19:14:52 2021 -0800

    feat: default timepicker to last week when dataset is changed (#12609)
    
    * set up timepicker to last when dataset is changed
    
    * simplify logic for dataset change
    
    * update modal code
    
    * remove uneeded code
    
    * fix bug
    
    * fix bug
---
 .../src/explore/components/ControlPanelsContainer.jsx    |  2 +-
 .../controls/DateFilterControl/DateFilterControl.tsx     | 16 +++++++++++++---
 2 files changed, 14 insertions(+), 4 deletions(-)

diff --git 
a/superset-frontend/src/explore/components/ControlPanelsContainer.jsx 
b/superset-frontend/src/explore/components/ControlPanelsContainer.jsx
index e7b036e..8c82d46 100644
--- a/superset-frontend/src/explore/components/ControlPanelsContainer.jsx
+++ b/superset-frontend/src/explore/components/ControlPanelsContainer.jsx
@@ -132,7 +132,6 @@ class ControlPanelsContainer extends React.Component {
     if (visibility && !visibility.call(config, this.props, controlData)) {
       return null;
     }
-
     return (
       <Control
         key={`control-${name}`}
@@ -140,6 +139,7 @@ class ControlPanelsContainer extends React.Component {
         validationErrors={validationErrors}
         actions={actions}
         formData={provideFormDataToProps ? formData : null}
+        datasource={formData?.datasource}
         {...restProps}
       />
     );
diff --git 
a/superset-frontend/src/explore/components/controls/DateFilterControl/DateFilterControl.tsx
 
b/superset-frontend/src/explore/components/controls/DateFilterControl/DateFilterControl.tsx
index 294ce99..3e4833d 100644
--- 
a/superset-frontend/src/explore/components/controls/DateFilterControl/DateFilterControl.tsx
+++ 
b/superset-frontend/src/explore/components/controls/DateFilterControl/DateFilterControl.tsx
@@ -74,7 +74,6 @@ const fetchTimeRange = async (
 ) => {
   const query = rison.encode(timeRange);
   const endpoint = `/api/v1/time_range/?q=${query}`;
-
   try {
     const response = await SupersetClient.get({ endpoint });
     const timeRangeString = buildTimeRangeString(
@@ -171,20 +170,23 @@ interface DateFilterLabelProps {
   onChange: (timeRange: string) => void;
   value?: string;
   endpoints?: TimeRangeEndpoints;
+  datasource?: string;
 }
 
 export default function DateFilterControl(props: DateFilterLabelProps) {
-  const { value = 'Last week', endpoints, onChange } = props;
+  const { value = 'Last week', endpoints, onChange, datasource } = props;
   const [actualTimeRange, setActualTimeRange] = useState<string>(value);
 
   const [show, setShow] = useState<boolean>(false);
   const [frame, setFrame] = useState<FrameType>(guessFrame(value));
+  const [isMounted, setIsMounted] = useState<boolean>(false);
   const [timeRangeValue, setTimeRangeValue] = useState(value);
   const [validTimeRange, setValidTimeRange] = useState<boolean>(false);
   const [evalResponse, setEvalResponse] = useState<string>(value);
   const [tooltipTitle, setTooltipTitle] = useState<string>(value);
 
   useEffect(() => {
+    if (!isMounted) setIsMounted(true);
     fetchTimeRange(value, endpoints).then(({ value: actualRange, error }) => {
       if (error) {
         setEvalResponse(error || '');
@@ -219,6 +221,14 @@ export default function DateFilterControl(props: 
DateFilterLabelProps) {
   }, [value]);
 
   useEffect(() => {
+    if (isMounted) {
+      onChange('Last week');
+      setTimeRangeValue('Last week');
+      setFrame(guessFrame('Last week'));
+    }
+  }, [datasource]);
+
+  useEffect(() => {
     fetchTimeRange(timeRangeValue, endpoints).then(({ value, error }) => {
       if (error) {
         setEvalResponse(error || '');
@@ -236,8 +246,8 @@ export default function DateFilterControl(props: 
DateFilterLabelProps) {
   }
 
   function onHide() {
-    setFrame(guessFrame(value));
     setTimeRangeValue(value);
+    setFrame(guessFrame(value));
     setShow(false);
   }
 

Reply via email to