From 785abaa622984bcc9fc82d2f75596fbff609e769 Mon Sep 17 00:00:00 2001 From: Phillip Kelley-Dotson Date: Tue, 19 Jan 2021 15:11:10 -0800 Subject: [PATCH 1/6] set up timepicker to last when dataset is changed --- .../src/datasource/ChangeDatasourceModal.tsx | 3 + .../src/explore/actions/exploreActions.ts | 9 ++ .../components/ControlPanelsContainer.jsx | 3 +- .../components/ExploreViewContainer.jsx | 2 + .../components/controls/DatasourceControl.jsx | 1 + .../DateFilterControl/DateFilterControl.tsx | 104 ++++++++++++------ .../src/explore/reducers/exploreReducer.js | 3 + 7 files changed, 90 insertions(+), 35 deletions(-) diff --git a/superset-frontend/src/datasource/ChangeDatasourceModal.tsx b/superset-frontend/src/datasource/ChangeDatasourceModal.tsx index 1ab82492e8918..fbe1e7c5f2a84 100644 --- a/superset-frontend/src/datasource/ChangeDatasourceModal.tsx +++ b/superset-frontend/src/datasource/ChangeDatasourceModal.tsx @@ -57,6 +57,7 @@ interface ChangeDatasourceModalProps { onDatasourceSave: (datasource: object, errors?: Array) => {}; onHide: () => void; show: boolean; + setControlDefault: (arg0: boolean) => void; } const ConfirmModalStyled = styled.div` @@ -102,6 +103,7 @@ const ChangeDatasourceModal: FunctionComponent = ({ onDatasourceSave, onHide, show, + setControlDefault, }) => { const [filter, setFilter] = useState(undefined); const [confirmChange, setConfirmChange] = useState(false); @@ -186,6 +188,7 @@ const ChangeDatasourceModal: FunctionComponent = ({ }, ); }); + setControlDefault(true); onHide(); addSuccessToast('Successfully changed dataset!'); }; diff --git a/superset-frontend/src/explore/actions/exploreActions.ts b/superset-frontend/src/explore/actions/exploreActions.ts index 9d53f7506d519..cb468fa541841 100644 --- a/superset-frontend/src/explore/actions/exploreActions.ts +++ b/superset-frontend/src/explore/actions/exploreActions.ts @@ -147,8 +147,17 @@ export function sliceUpdated(slice: Slice) { return { type: SLICE_UPDATED, slice }; } +export const SET_CONTROL_PANEL_DEFAULTS = 'SET_CONTROL_PANEL_DEFAULTS'; +export function setControlPanelDefaults(defaultSelect: boolean) { + return { + type: SET_CONTROL_PANEL_DEFAULTS, + defaultSelect, + }; +} + export const exploreActions = { ...toastActions, + setControlPanelDefaults, setDatasourceType, setDatasource, setDatasources, diff --git a/superset-frontend/src/explore/components/ControlPanelsContainer.jsx b/superset-frontend/src/explore/components/ControlPanelsContainer.jsx index 70c347cffd00b..45b37b92de8ee 100644 --- a/superset-frontend/src/explore/components/ControlPanelsContainer.jsx +++ b/superset-frontend/src/explore/components/ControlPanelsContainer.jsx @@ -120,7 +120,7 @@ class ControlPanelsContainer extends React.Component { if (visibility && !visibility.call(config, this.props, controlData)) { return null; } - + const { defaultSelect } = this.props.exploreState; return ( ); diff --git a/superset-frontend/src/explore/components/ExploreViewContainer.jsx b/superset-frontend/src/explore/components/ExploreViewContainer.jsx index 1a701df8e180a..5e29ff0228164 100644 --- a/superset-frontend/src/explore/components/ExploreViewContainer.jsx +++ b/superset-frontend/src/explore/components/ExploreViewContainer.jsx @@ -478,6 +478,7 @@ function ExploreViewContainer(props) { controls={props.controls} datasource_type={props.datasource_type} isDatasourceMetaLoading={props.isDatasourceMetaLoading} + defaultSelect={props.defaultSelect} />
)} diff --git a/superset-frontend/src/explore/components/controls/DateFilterControl/DateFilterControl.tsx b/superset-frontend/src/explore/components/controls/DateFilterControl/DateFilterControl.tsx index 6903c2c98e9e4..462a9aca52d70 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,27 +170,39 @@ interface DateFilterLabelProps { onChange: (timeRange: string) => void; value?: string; endpoints?: TimeRangeEndpoints; + defaultSelect?: boolean; + actions: { + setControlPanelDefaults: (arg0: boolean) => void; + }; } export default function DateFilterControl(props: DateFilterLabelProps) { - const { value = 'Last week', endpoints, onChange } = props; + const { + value = 'Last week', + endpoints, + onChange, + defaultSelect, + actions: { setControlPanelDefaults }, + } = props; const [actualTimeRange, setActualTimeRange] = useState(value); const [show, setShow] = useState(false); const [frame, setFrame] = useState(guessFrame(value)); + const [resetRangeType, setRangeType] = useState(true); const [timeRangeValue, setTimeRangeValue] = useState(value); const [validTimeRange, setValidTimeRange] = useState(false); const [evalResponse, setEvalResponse] = useState(value); const [tooltipTitle, setTooltipTitle] = useState(value); useEffect(() => { - fetchTimeRange(value, endpoints).then(({ value: actualRange, error }) => { - if (error) { - setEvalResponse(error || ''); - setValidTimeRange(false); - setTooltipTitle(value || ''); - } else { - /* + if (!defaultSelect) { + fetchTimeRange(value, endpoints).then(({ value: actualRange, error }) => { + if (error) { + setEvalResponse(error || ''); + setValidTimeRange(false); + setTooltipTitle(value || ''); + } else { + /* HRT == human readable text ADR == actual datetime range +--------------+------+----------+--------+----------+-----------+ @@ -202,33 +213,52 @@ export default function DateFilterControl(props: DateFilterLabelProps) { | tooltip | ADR | ADR | HRT | HRT | HRT | +--------------+------+----------+--------+----------+-----------+ */ - const valueToLower = value.toLowerCase(); - if ( - valueToLower.startsWith('last') || - valueToLower.startsWith('next') || - valueToLower.startsWith('previous') - ) { - setActualTimeRange(value); - setTooltipTitle(actualRange || ''); - } else { - setActualTimeRange(actualRange || ''); - setTooltipTitle(value || ''); + const valueToLower = value.toLowerCase(); + if ( + valueToLower.startsWith('last') || + valueToLower.startsWith('next') || + valueToLower.startsWith('previous') + ) { + setActualTimeRange(value); + setTooltipTitle(actualRange || ''); + } else { + setActualTimeRange(actualRange || ''); + setTooltipTitle(value || ''); + } + setValidTimeRange(true); } - setValidTimeRange(true); - } - }); + }); + } }, [value]); useEffect(() => { - fetchTimeRange(timeRangeValue, endpoints).then(({ value, error }) => { - if (error) { - setEvalResponse(error || ''); - setValidTimeRange(false); - } else { - setEvalResponse(value || ''); - setValidTimeRange(true); - } - }); + if (defaultSelect) { + fetchTimeRange('Last week', endpoints).then( + ({ value: actualRange, error }) => { + setActualTimeRange('Last week'); + setTooltipTitle(actualRange || ''); + setEvalResponse(actualRange || ''); + setFrame('Common'); + setControlPanelDefaults(false); + }, + ); + // set + // se + } + }, [defaultSelect]); + + useEffect(() => { + if (!resetRangeType) { + fetchTimeRange(timeRangeValue, endpoints).then(({ value, error }) => { + if (error) { + setEvalResponse(error || ''); + setValidTimeRange(false); + } else { + setEvalResponse(value || ''); + setValidTimeRange(true); + } + }); + } }, [timeRangeValue]); function onSave() { @@ -237,7 +267,8 @@ export default function DateFilterControl(props: DateFilterLabelProps) { } function onHide() { - setFrame(guessFrame(value)); + if (resetRangeType) setFrame(guessFrame('Last week')); + else setFrame(guessFrame(value)); setTimeRangeValue(value); setShow(false); } @@ -251,6 +282,7 @@ export default function DateFilterControl(props: DateFilterLabelProps) { }; function onFrame(option: SelectOptionType) { + setRangeType(false); if (option.value === 'No filter') { setTimeRangeValue('No filter'); } @@ -262,7 +294,11 @@ export default function DateFilterControl(props: DateFilterLabelProps) {
{t('RANGE TYPE')}
value === frame) - } + value={FRAME_OPTIONS.filter(({ value }) => value === frame)} onChange={onFrame} className="frame-dropdown" /> diff --git a/superset-frontend/src/explore/reducers/exploreReducer.js b/superset-frontend/src/explore/reducers/exploreReducer.js index 3fcc25743f377..e8167f956c136 100644 --- a/superset-frontend/src/explore/reducers/exploreReducer.js +++ b/superset-frontend/src/explore/reducers/exploreReducer.js @@ -175,9 +175,6 @@ export default function exploreReducer(state = {}, action) { sliceName: action.slice.slice_name ?? state.sliceName, }; }, - [actions.SET_CONTROL_PANEL_DEFAULTS]() { - return { ...state, defaultSelect: action.defaultSelect }; - }, }; if (action.type in actionHandlers) { return actionHandlers[action.type](); From d8c10d651b0a826678fa2342169f52d97d493dd0 Mon Sep 17 00:00:00 2001 From: Phillip Kelley-Dotson Date: Fri, 22 Jan 2021 09:34:32 -0800 Subject: [PATCH 3/6] update modal code --- superset-frontend/src/datasource/ChangeDatasourceModal.tsx | 3 --- 1 file changed, 3 deletions(-) diff --git a/superset-frontend/src/datasource/ChangeDatasourceModal.tsx b/superset-frontend/src/datasource/ChangeDatasourceModal.tsx index fbe1e7c5f2a84..1ab82492e8918 100644 --- a/superset-frontend/src/datasource/ChangeDatasourceModal.tsx +++ b/superset-frontend/src/datasource/ChangeDatasourceModal.tsx @@ -57,7 +57,6 @@ interface ChangeDatasourceModalProps { onDatasourceSave: (datasource: object, errors?: Array) => {}; onHide: () => void; show: boolean; - setControlDefault: (arg0: boolean) => void; } const ConfirmModalStyled = styled.div` @@ -103,7 +102,6 @@ const ChangeDatasourceModal: FunctionComponent = ({ onDatasourceSave, onHide, show, - setControlDefault, }) => { const [filter, setFilter] = useState(undefined); const [confirmChange, setConfirmChange] = useState(false); @@ -188,7 +186,6 @@ const ChangeDatasourceModal: FunctionComponent = ({ }, ); }); - setControlDefault(true); onHide(); addSuccessToast('Successfully changed dataset!'); }; From e81628084ce2d8df997838fb27e4aa3fc5e345b5 Mon Sep 17 00:00:00 2001 From: Phillip Kelley-Dotson Date: Fri, 22 Jan 2021 15:50:23 -0800 Subject: [PATCH 4/6] remove uneeded code --- .../src/explore/components/controls/DatasourceControl.jsx | 1 - 1 file changed, 1 deletion(-) diff --git a/superset-frontend/src/explore/components/controls/DatasourceControl.jsx b/superset-frontend/src/explore/components/controls/DatasourceControl.jsx index f179c9af3b591..e30aebe8763cf 100644 --- a/superset-frontend/src/explore/components/controls/DatasourceControl.jsx +++ b/superset-frontend/src/explore/components/controls/DatasourceControl.jsx @@ -210,7 +210,6 @@ class DatasourceControl extends React.PureComponent { onHide={this.toggleChangeDatasourceModal} show={showChangeDatasourceModal} onChange={onChange} - setControlDefault={this.props.actions.setControlPanelDefaults} /> )} From 89f1ecd556c9db53c44055a20e4a7c00b002fba2 Mon Sep 17 00:00:00 2001 From: Phillip Kelley-Dotson Date: Fri, 22 Jan 2021 16:27:34 -0800 Subject: [PATCH 5/6] fix bug --- .../components/controls/DateFilterControl/DateFilterControl.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/superset-frontend/src/explore/components/controls/DateFilterControl/DateFilterControl.tsx b/superset-frontend/src/explore/components/controls/DateFilterControl/DateFilterControl.tsx index e1961a5ec3cb5..869dbf5bcad96 100644 --- a/superset-frontend/src/explore/components/controls/DateFilterControl/DateFilterControl.tsx +++ b/superset-frontend/src/explore/components/controls/DateFilterControl/DateFilterControl.tsx @@ -247,6 +247,7 @@ export default function DateFilterControl(props: DateFilterLabelProps) { function onHide() { setTimeRangeValue(value); + setFrame(guessFrame(value)); setShow(false); } From e899383ba430363bfdb1f7f4dcf9537867cc3772 Mon Sep 17 00:00:00 2001 From: Phillip Kelley-Dotson Date: Sun, 24 Jan 2021 22:42:09 -0800 Subject: [PATCH 6/6] fix bug --- .../controls/DateFilterControl/DateFilterControl.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/superset-frontend/src/explore/components/controls/DateFilterControl/DateFilterControl.tsx b/superset-frontend/src/explore/components/controls/DateFilterControl/DateFilterControl.tsx index 869dbf5bcad96..e7f4e758ce4a7 100644 --- a/superset-frontend/src/explore/components/controls/DateFilterControl/DateFilterControl.tsx +++ b/superset-frontend/src/explore/components/controls/DateFilterControl/DateFilterControl.tsx @@ -217,7 +217,6 @@ export default function DateFilterControl(props: DateFilterLabelProps) { setTooltipTitle(value || ''); } setValidTimeRange(true); - setFrame(guessFrame(value)); } }); }, [value]); @@ -225,6 +224,8 @@ export default function DateFilterControl(props: DateFilterLabelProps) { useEffect(() => { if (isMounted) { onChange('Last week'); + setTimeRangeValue('Last week'); + setFrame(guessFrame('Last week')); } }, [datasource]);