From 07b2fa4ef876ecca21d9ac9a92bab4e0eda3e42e Mon Sep 17 00:00:00 2001 From: Anton Rusinov Date: Tue, 27 Nov 2018 02:04:59 +0200 Subject: [PATCH] fix: make sure time indicator is updated after navigation (#1082) * fix: make sure time indicator is updated after navigation * Replace setInterval with setTimeout --- src/DayColumn.js | 49 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/src/DayColumn.js b/src/DayColumn.js index de823199f..3a675297b 100644 --- a/src/DayColumn.js +++ b/src/DayColumn.js @@ -54,7 +54,7 @@ class DayColumn extends React.Component { timeslots: 2, } - state = { selecting: false } + state = { selecting: false, timeIndicatorPosition: null } constructor(...args) { super(...args) @@ -66,14 +66,13 @@ class DayColumn extends React.Component { this.props.selectable && this._selectable() if (this.props.isNow) { - this.positionTimeIndicator() - this.triggerTimeIndicatorUpdate() + this.setTimeIndicatorPositionUpdateInterval() } } componentWillUnmount() { this._teardownSelectable() - window.clearTimeout(this._timeIndicatorTimeout) + this.clearTimeIndicatorInterval() } componentWillReceiveProps(nextProps) { @@ -84,22 +83,49 @@ class DayColumn extends React.Component { this.slotMetrics = this.slotMetrics.update(nextProps) } - triggerTimeIndicatorUpdate() { - // Update the position of the time indicator every minute + componentDidUpdate(prevProps, prevState) { + if (prevProps.isNow !== this.props.isNow) { + this.clearTimeIndicatorInterval() + + if (this.props.isNow) { + this.setTimeIndicatorPositionUpdateInterval( + prevState.timeIndicatorPosition === this.state.timeIndicatorPosition + ) + } + } + } + + intervalTriggered = false + /** + * @param tail {Boolean} - whether `positionTimeIndicator` call should be + * deferred or called upon setting interval (`true` - if deferred); + */ + setTimeIndicatorPositionUpdateInterval(tail = false) { + if (!this.intervalTriggered && !tail) { + this.positionTimeIndicator() + } + this._timeIndicatorTimeout = window.setTimeout(() => { + this.intervalTriggered = true this.positionTimeIndicator() - this.triggerTimeIndicatorUpdate() + this.setTimeIndicatorPositionUpdateInterval() }, 60000) } + clearTimeIndicatorInterval() { + this.intervalTriggered = false + window.clearTimeout(this._timeIndicatorTimeout) + } + positionTimeIndicator() { const { min, max, getNow } = this.props const current = getNow() - const timeIndicator = this.refs.timeIndicator if (current >= min && current <= max) { const { top } = this.slotMetrics.getRange(current, current) - timeIndicator.style.top = `${top}%` + this.setState({ timeIndicatorPosition: top }) + } else { + this.clearTimeIndicatorInterval() } } @@ -162,7 +188,10 @@ class DayColumn extends React.Component { )} {isNow && ( -
+
)}
)