From 2ac745a1a0d83fd6c96e9b59da9c83dc623c3f11 Mon Sep 17 00:00:00 2001 From: Rusinov Anton Date: Fri, 2 Nov 2018 16:16:51 +0200 Subject: [PATCH 1/2] fix: make sure time indicator is updated after navigation --- src/DayColumn.js | 49 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/src/DayColumn.js b/src/DayColumn.js index 75eeedeaf..2145d5033 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,45 @@ class DayColumn extends React.Component { this.slotMetrics = this.slotMetrics.update(nextProps) } - triggerTimeIndicatorUpdate() { - // Update the position of the time indicator every minute - this._timeIndicatorTimeout = window.setTimeout(() => { + componentDidUpdate(prevProps, prevState) { + if (prevProps.isNow !== this.props.isNow) { + this.clearTimeIndicatorInterval() + + if (this.props.isNow) { + this.setTimeIndicatorPositionUpdateInterval( + prevState.timeIndicatorPosition === this.state.timeIndicatorPosition + ) + } + } + } + + /** + * @param tail {Boolean} - whether `positionTimeIndicator` call should be + * deferred or called upon setting interval (`true` - if deferred); + */ + setTimeIndicatorPositionUpdateInterval(tail = false) { + if (!tail) { + this.positionTimeIndicator() + } + + this._timeIndicatorInterval = window.setInterval(() => { this.positionTimeIndicator() - this.triggerTimeIndicatorUpdate() }, 60000) } + clearTimeIndicatorInterval() { + window.clearInterval(this._timeIndicatorInterval) + } + 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 +184,10 @@ class DayColumn extends React.Component { )} {isNow && ( -
+
)}
) @@ -188,7 +213,7 @@ class DayColumn extends React.Component { events, accessors, slotMetrics, - minimumStartDifference: Math.ceil(step * timeslots / 2), + minimumStartDifference: Math.ceil((step * timeslots) / 2), }) return styledEvents.map(({ event, style }, idx) => { From 90d436cc9d282589b40104cd442b28213a6e39ca Mon Sep 17 00:00:00 2001 From: Rusinov Anton Date: Wed, 7 Nov 2018 20:09:56 +0200 Subject: [PATCH 2/2] Replace setInterval with setTimeout --- src/DayColumn.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/DayColumn.js b/src/DayColumn.js index 2145d5033..96f6a34e1 100644 --- a/src/DayColumn.js +++ b/src/DayColumn.js @@ -95,22 +95,26 @@ class DayColumn extends React.Component { } } + intervalTriggered = false /** * @param tail {Boolean} - whether `positionTimeIndicator` call should be * deferred or called upon setting interval (`true` - if deferred); */ setTimeIndicatorPositionUpdateInterval(tail = false) { - if (!tail) { + if (!this.intervalTriggered && !tail) { this.positionTimeIndicator() } - this._timeIndicatorInterval = window.setInterval(() => { + this._timeIndicatorTimeout = window.setTimeout(() => { + this.intervalTriggered = true this.positionTimeIndicator() + this.setTimeIndicatorPositionUpdateInterval() }, 60000) } clearTimeIndicatorInterval() { - window.clearInterval(this._timeIndicatorInterval) + this.intervalTriggered = false + window.clearTimeout(this._timeIndicatorTimeout) } positionTimeIndicator() {