Skip to content
Snippets Groups Projects
Unverified Commit a9481be9 authored by Paul Rosenzweig's avatar Paul Rosenzweig Committed by GitHub
Browse files

keep refreshElapsed state in refresh widget (#10505)

parent f3b90bf0
No related branches found
No related tags found
No related merge requests found
...@@ -90,7 +90,7 @@ type Props = { ...@@ -90,7 +90,7 @@ type Props = {
editingParameter: ?Parameter, editingParameter: ?Parameter,
refreshPeriod: number, refreshPeriod: number,
refreshElapsed: number, setRefreshElapsedHook: Function,
isFullscreen: boolean, isFullscreen: boolean,
isNightMode: boolean, isNightMode: boolean,
hideParameters: ?string, hideParameters: ?string,
......
...@@ -13,7 +13,7 @@ export const getDashboardActions = ({ ...@@ -13,7 +13,7 @@ export const getDashboardActions = ({
onNightModeChange, onNightModeChange,
onFullscreenChange, onFullscreenChange,
refreshPeriod, refreshPeriod,
refreshElapsed, setRefreshElapsedHook,
onRefreshPeriodChange, onRefreshPeriodChange,
}) => { }) => {
const buttons = []; const buttons = [];
...@@ -25,7 +25,7 @@ export const getDashboardActions = ({ ...@@ -25,7 +25,7 @@ export const getDashboardActions = ({
data-metabase-event="Dashboard;Refresh Menu Open" data-metabase-event="Dashboard;Refresh Menu Open"
className="text-brand-hover" className="text-brand-hover"
period={refreshPeriod} period={refreshPeriod}
elapsed={refreshElapsed} setRefreshElapsedHook={setRefreshElapsedHook}
onChangePeriod={onRefreshPeriodChange} onChangePeriod={onRefreshPeriodChange}
/>, />,
); );
......
...@@ -49,7 +49,7 @@ type Props = { ...@@ -49,7 +49,7 @@ type Props = {
isNightMode: boolean, isNightMode: boolean,
refreshPeriod: ?number, refreshPeriod: ?number,
refreshElapsed: ?number, setRefreshElapsedHook: Function,
parametersWidget: React$Element<*>, parametersWidget: React$Element<*>,
...@@ -90,7 +90,7 @@ export default class DashboardHeader extends Component { ...@@ -90,7 +90,7 @@ export default class DashboardHeader extends Component {
isNightMode: PropTypes.bool.isRequired, isNightMode: PropTypes.bool.isRequired,
refreshPeriod: PropTypes.number, refreshPeriod: PropTypes.number,
refreshElapsed: PropTypes.number, setRefreshElapsedHook: PropTypes.func.isRequired,
addCardToDashboard: PropTypes.func.isRequired, addCardToDashboard: PropTypes.func.isRequired,
addTextDashCardToDashboard: PropTypes.func.isRequired, addTextDashCardToDashboard: PropTypes.func.isRequired,
......
...@@ -20,8 +20,28 @@ const OPTIONS = [ ...@@ -20,8 +20,28 @@ const OPTIONS = [
]; ];
export default class RefreshWidget extends Component { export default class RefreshWidget extends Component {
state = { elapsed: null };
componentWillMount() {
const { setRefreshElapsedHook } = this.props;
if (setRefreshElapsedHook) {
setRefreshElapsedHook(elapsed => this.setState({ elapsed }));
}
}
componentDidUpdate(prevProps) {
const { setRefreshElapsedHook } = this.props;
if (
setRefreshElapsedHook &&
prevProps.setRefreshElapsedHook !== setRefreshElapsedHook
) {
setRefreshElapsedHook(elapsed => this.setState({ elapsed }));
}
}
render() { render() {
const { period, elapsed, onChangePeriod, className } = this.props; const { period, onChangePeriod, className } = this.props;
const { elapsed } = this.state;
const remaining = period - elapsed; const remaining = period - elapsed;
return ( return (
<PopoverWithTrigger <PopoverWithTrigger
......
...@@ -25,11 +25,10 @@ type State = { ...@@ -25,11 +25,10 @@ type State = {
isFullscreen: boolean, isFullscreen: boolean,
isNightMode: boolean, isNightMode: boolean,
refreshPeriod: ?number, refreshPeriod: ?number,
refreshElapsed: ?number,
hideParameters: ?string, hideParameters: ?string,
}; };
const TICK_PERIOD = 0.25; // seconds const TICK_PERIOD = 1; // seconds
/* This contains some state for dashboard controls on both private and embedded dashboards. /* This contains some state for dashboard controls on both private and embedded dashboards.
* It should probably be in Redux? * It should probably be in Redux?
...@@ -51,13 +50,15 @@ export default (ComposedComponent: ReactClass<any>) => ...@@ -51,13 +50,15 @@ export default (ComposedComponent: ReactClass<any>) =>
isNightMode: false, isNightMode: false,
refreshPeriod: null, refreshPeriod: null,
refreshElapsed: null,
hideParameters: null, hideParameters: null,
}; };
_interval: ?number; _interval: ?number;
_refreshElapsed: ?number;
_refreshElapsedHook: ?Function;
componentWillMount() { componentWillMount() {
if (screenfull.enabled) { if (screenfull.enabled) {
document.addEventListener( document.addEventListener(
...@@ -139,17 +140,16 @@ export default (ComposedComponent: ReactClass<any>) => ...@@ -139,17 +140,16 @@ export default (ComposedComponent: ReactClass<any>) =>
this._tickRefreshClock, this._tickRefreshClock,
TICK_PERIOD * 1000, TICK_PERIOD * 1000,
); );
this.setState({ refreshPeriod, refreshElapsed: 0 }); this.setState({ refreshPeriod });
this.setRefreshElapsed(0);
MetabaseAnalytics.trackEvent( MetabaseAnalytics.trackEvent(
"Dashboard", "Dashboard",
"Set Refresh", "Set Refresh",
refreshPeriod, refreshPeriod,
); );
} else { } else {
this.setState({ this.setState({ refreshPeriod: null });
refreshPeriod: null, this.setRefreshElapsed(null);
refreshElapsed: null,
});
} }
}; };
...@@ -177,12 +177,10 @@ export default (ComposedComponent: ReactClass<any>) => ...@@ -177,12 +177,10 @@ export default (ComposedComponent: ReactClass<any>) =>
}; };
_tickRefreshClock = async () => { _tickRefreshClock = async () => {
const refreshElapsed = (this.state.refreshElapsed || 0) + TICK_PERIOD; this._refreshElapsed = (this._refreshElapsed || 0) + TICK_PERIOD;
if ( const { refreshPeriod } = this.state;
this.state.refreshPeriod && if (refreshPeriod && this._refreshElapsed >= refreshPeriod) {
refreshElapsed >= this.state.refreshPeriod this._refreshElapsed = 0;
) {
this.setState({ refreshElapsed: 0 });
await this.props.fetchDashboard( await this.props.fetchDashboard(
this.props.dashboardId, this.props.dashboardId,
this.props.location.query, this.props.location.query,
...@@ -191,9 +189,8 @@ export default (ComposedComponent: ReactClass<any>) => ...@@ -191,9 +189,8 @@ export default (ComposedComponent: ReactClass<any>) =>
reload: true, reload: true,
clear: false, clear: false,
}); });
} else {
this.setState({ refreshElapsed });
} }
this.setRefreshElapsed(this._refreshElapsed);
}; };
_clearRefreshInterval() { _clearRefreshInterval() {
...@@ -219,11 +216,22 @@ export default (ComposedComponent: ReactClass<any>) => ...@@ -219,11 +216,22 @@ export default (ComposedComponent: ReactClass<any>) =>
this.setState({ isFullscreen: !!screenfull.isFullscreen }); this.setState({ isFullscreen: !!screenfull.isFullscreen });
}; };
setRefreshElapsedHook = hook => {
this._refreshElapsedHook = hook;
};
setRefreshElapsed = elapsed => {
if (this._refreshElapsedHook) {
this._refreshElapsedHook(elapsed);
}
};
render() { render() {
return ( return (
<ComposedComponent <ComposedComponent
{...this.props} {...this.props}
{...this.state} {...this.state}
setRefreshElapsedHook={this.setRefreshElapsedHook}
loadDashboardParams={this.loadDashboardParams} loadDashboardParams={this.loadDashboardParams}
updateDashboardParams={this.updateDashboardParams} updateDashboardParams={this.updateDashboardParams}
onNightModeChange={this.setNightMode} onNightModeChange={this.setNightMode}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment