|
| 1 | +import React, { Component } from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import { FormattedMessage } from 'react-intl'; |
| 4 | +import { OverlayTrigger, Tooltip } from 'react-bootstrap'; |
| 5 | + |
| 6 | +import Button from '../../widgets/TheButton'; |
| 7 | +import Icon, { RefreshIcon } from '../../icons'; |
| 8 | +import { storageGetItem, storageSetItem, listenForChanges, removeListener } from '../../../helpers/localStorage'; |
| 9 | + |
| 10 | +const COUNTER_KEY = 'refreshButtonCounter'; |
| 11 | +const UPDATE_INTERVAL = 15; // s |
| 12 | +const CHANGE_PER_INTERVAL = 15; |
| 13 | +const COOL_DOWN_TIME = 60; |
| 14 | +const CLICK_LIMIT = 15; |
| 15 | + |
| 16 | +class RefreshButton extends Component { |
| 17 | + // >=0 - counter counts number of clicks; <0 - in cool down mode (returning to 0) |
| 18 | + state = { counter: 0 }; |
| 19 | + |
| 20 | + constructor(props) { |
| 21 | + super(props); |
| 22 | + this.intervalId = null; |
| 23 | + this.storageListenerId = null; |
| 24 | + } |
| 25 | + |
| 26 | + componentDidMount() { |
| 27 | + this.setState({ counter: Number(storageGetItem(COUNTER_KEY, 0)) }); |
| 28 | + |
| 29 | + this.storageListenerId = listenForChanges(COUNTER_KEY, newVal => { |
| 30 | + this.setState({ counter: newVal || 0 }); |
| 31 | + }); |
| 32 | + |
| 33 | + // periodic counter update |
| 34 | + this.intervalId = window.setInterval(() => { |
| 35 | + const newCounter = |
| 36 | + this.state.counter < 0 |
| 37 | + ? Math.min(this.state.counter + CHANGE_PER_INTERVAL, 0) // cool down |
| 38 | + : Math.max(this.state.counter - CHANGE_PER_INTERVAL, 0); // counting clicks |
| 39 | + |
| 40 | + if (newCounter !== this.state.counter) { |
| 41 | + this.setState({ counter: newCounter }); |
| 42 | + if (newCounter === 0) { |
| 43 | + // let's make sure all clients have their buttons enabled again |
| 44 | + storageSetItem(COUNTER_KEY, newCounter); |
| 45 | + } |
| 46 | + } |
| 47 | + }, UPDATE_INTERVAL * 1000); |
| 48 | + } |
| 49 | + |
| 50 | + componentWillUnmount() { |
| 51 | + if (this.intervalId !== null) { |
| 52 | + window.clearInterval(this.intervalId); |
| 53 | + this.intervalId = null; |
| 54 | + } |
| 55 | + if (this.storageListenerId !== null) { |
| 56 | + removeListener(this.storageListenerId); |
| 57 | + this.storageListenerId = null; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + clickHandler = () => { |
| 62 | + if (this.state.counter < 0) { |
| 63 | + return; // in cool down mode |
| 64 | + } |
| 65 | + |
| 66 | + const newCounter = this.state.counter >= CLICK_LIMIT ? -COOL_DOWN_TIME : this.state.counter + 1; |
| 67 | + this.setState({ counter: newCounter }); |
| 68 | + |
| 69 | + const storageCounter = Number(storageGetItem(COUNTER_KEY, 0)); |
| 70 | + if (newCounter < 0 || storageCounter < newCounter) { |
| 71 | + storageSetItem(COUNTER_KEY, newCounter); |
| 72 | + } |
| 73 | + |
| 74 | + this.props.onClick(); |
| 75 | + }; |
| 76 | + |
| 77 | + render() { |
| 78 | + const { onClick, ...props } = this.props; |
| 79 | + return this.state.counter < 0 ? ( |
| 80 | + <OverlayTrigger |
| 81 | + placement="bottom" |
| 82 | + overlay={ |
| 83 | + <Tooltip id="refreshButtonTooltip"> |
| 84 | + <FormattedMessage |
| 85 | + id="app.refreshButton.coolingDownTooltip" |
| 86 | + defaultMessage="The refresh button is too tired from all the refreshing. Please give it some time to recover." |
| 87 | + /> |
| 88 | + </Tooltip> |
| 89 | + }> |
| 90 | + <span> |
| 91 | + <Button {...props} disabled> |
| 92 | + <Icon icon="bed" gapRight={2} /> |
| 93 | + <FormattedMessage id="generic.refresh" defaultMessage="Refresh" /> |
| 94 | + </Button> |
| 95 | + </span> |
| 96 | + </OverlayTrigger> |
| 97 | + ) : ( |
| 98 | + <Button {...props} onClick={this.clickHandler}> |
| 99 | + <RefreshIcon gapRight={2} /> |
| 100 | + <FormattedMessage id="generic.refresh" defaultMessage="Refresh" /> |
| 101 | + </Button> |
| 102 | + ); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +RefreshButton.propTypes = { |
| 107 | + onClick: PropTypes.func.isRequired, |
| 108 | +}; |
| 109 | + |
| 110 | +export default RefreshButton; |
0 commit comments