Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

refactor to rely on tooltip times in parent component, cleanup

Ed Plato0823fd5a5740
diff --git a/web/js/components/toolbar/share/tooltips.js b/web/js/components/toolbar/share/tooltips.js
index f561e7aa8..4a75789bf 100644
--- a/web/js/components/toolbar/share/tooltips.js
+++ b/web/js/components/toolbar/share/tooltips.js
@@ -14,58 +14,46 @@ class ShareToolTips extends PureComponent {
super(props);
this.state = {
showErrorTooltip: false,
- tooltipOpen: false,
- tooltipToggleTime: 0,
+ showCopiedToolTip: false,
};
}
- componentDidMount() {
- const { showErrorTooltip } = this.props;
- if (showErrorTooltip) {
- this.setState({
- showErrorTooltip,
- });
- }
- }
-
componentDidUpdate(prevProps) {
- const { showErrorTooltip, tooltipToggleTime } = this.props;
- const tooltipToggleTimeChange = tooltipToggleTime !== this.state.tooltipToggleTime;
- const showErrorTooltipChange = showErrorTooltip && !prevProps.showErrorTooltip;
- if (tooltipToggleTimeChange || showErrorTooltipChange) {
- this.updateToolTipState();
+ const { tooltipToggleTime, tooltipErrorTime } = this.props;
+ const prevTooltipToggleTime = prevProps.tooltipToggleTime;
+ const prevTooltipErrorTime = prevProps.tooltipErrorTime;
+ const toolTipChange = tooltipToggleTime !== prevTooltipToggleTime;
+ const tooltipErrorChange = tooltipErrorTime !== prevTooltipErrorTime;
+ if (toolTipChange || tooltipErrorChange) {
+ this.updateToolTipState(toolTipChange, tooltipErrorChange);
}
}
componentWillUnmount() {
// clear pending timeouts
- clearTimeout(this.toolTipOpenTimeout);
+ clearTimeout(this.showCopiedToolTipTimeout);
clearTimeout(this.showErrorTimeout);
}
- updateToolTipState = () => {
- const { showErrorTooltip, tooltipToggleTime } = this.props;
- // eslint-disable-next-line react/no-access-state-in-setstate
- const toolTipChange = tooltipToggleTime !== this.state.tooltipToggleTime;
+ updateToolTipState = (toolTipChange, tooltipErrorChange) => {
this.setState({
- showErrorTooltip,
- tooltipToggleTime,
- tooltipOpen: toolTipChange,
+ showErrorTooltip: tooltipErrorChange,
+ showCopiedToolTip: toolTipChange,
});
}
render() {
- const { showErrorTooltip, tooltipOpen } = this.state;
+ const { showErrorTooltip, showCopiedToolTip } = this.state;
if (showErrorTooltip) {
clearTimeout(this.showErrorTimeout);
this.showErrorTimeout = setTimeout(() => {
this.setState({ showErrorTooltip: false });
}, 2000);
}
- if (tooltipOpen) {
- clearTimeout(this.toolTipOpenTimeout);
- this.toolTipOpenTimeout = setTimeout(() => {
- this.setState({ tooltipOpen: false });
+ if (showCopiedToolTip) {
+ clearTimeout(this.showCopiedToolTipTimeout);
+ this.showCopiedToolTipTimeout = setTimeout(() => {
+ this.setState({ showCopiedToolTip: false });
}, 2000);
}
return (
@@ -79,7 +67,7 @@ class ShareToolTips extends PureComponent {
</Tooltip>
<Tooltip
placement="right"
- isOpen={tooltipOpen}
+ isOpen={showCopiedToolTip}
target="copy-to-clipboard-button"
>
Copied!
@@ -90,7 +78,7 @@ class ShareToolTips extends PureComponent {
}
ShareToolTips.propTypes = {
- showErrorTooltip: PropTypes.bool,
+ tooltipErrorTime: PropTypes.number,
tooltipToggleTime: PropTypes.number,
};
diff --git a/web/js/containers/share.js b/web/js/containers/share.js
index 924632de0..71503404b 100644
--- a/web/js/containers/share.js
+++ b/web/js/containers/share.js
@@ -17,7 +17,7 @@ import Checkbox from '../components/util/checkbox';
import { requestShortLink } from '../modules/link/actions';
import history from '../main';
-const getShortenRequestString = function(mock, permalink) {
+const getShortenRequestString = (mock, permalink) => {
const mockStr = mock || '';
if (/localhost/.test(window.location)) {
return 'mock/short_link.json';
@@ -37,17 +37,22 @@ class ShareLinkContainer extends Component {
shortLinkKey: '',
isShort: false,
tooltipToggleTime: 0,
- showErrorTooltip: false,
+ tooltipErrorTime: 0,
queryString: history.location.search || '',
};
- this.onToggleShorten = this.onToggleShorten.bind(this);
- this.onLinkClick = this.onLinkClick.bind(this);
}
static getDerivedStateFromProps(nextProps, prevState) {
- if (nextProps.shortLink.error && prevState.isShort) {
- return { isShort: false, showErrorTooltip: true };
- } return null;
+ const { shortLink } = nextProps;
+ const errorNoResponse = shortLink.response === null;
+ const errorExplicit = shortLink.error;
+ if ((errorNoResponse || errorExplicit) && prevState.isShort) {
+ return {
+ isShort: false,
+ tooltipErrorTime: Date.now(),
+ };
+ }
+ return null;
}
componentDidMount() {
@@ -68,14 +73,14 @@ class ShareLinkContainer extends Component {
if (this.unlisten) this.unlisten();
}
- getShortLink() {
+ getShortLink = () => {
const { requestShortLink, mock } = this.props;
const link = this.getPermalink();
const location = getShortenRequestString(mock, link);
return requestShortLink(location);
}
- onToggleShorten() {
+ onToggleShorten = () => {
const { shortLinkKey, isShort, queryString } = this.state;
if (!isShort && shortLinkKey !== queryString) {
this.getShortLink();
@@ -87,11 +92,20 @@ class ShareLinkContainer extends Component {
isShort: !isShort,
});
} else {
- this.setState({ isShort: !isShort });
+ this.setState({
+ isShort: !isShort,
+ });
}
}
- getPermalink() {
+ // set copy tooltip time
+ onCopyToClipboard = () => {
+ this.setState({
+ tooltipToggleTime: Date.now(),
+ });
+ }
+
+ getPermalink = () => {
const { queryString } = this.state;
const { selectedDate } = this.props;
const url = window.location.href;
@@ -119,7 +133,7 @@ class ShareLinkContainer extends Component {
return permalink;
}
- onLinkClick(type) {
+ onLinkClick = (type) => {
const permalink = this.getPermalink();
googleTagManager.pushEvent({
event: 'social_share_platform',
@@ -152,7 +166,11 @@ class ShareLinkContainer extends Component {
render() {
const { shortLink } = this.props;
- const { isShort, showErrorTooltip, tooltipToggleTime } = this.state;
+ const {
+ isShort,
+ tooltipErrorTime,
+ tooltipToggleTime,
+ } = this.state;
const value = shortLink.isLoading && isShort
? 'Please wait...'
: isShort
@@ -165,8 +183,8 @@ class ShareLinkContainer extends Component {
<>
<div>
<ShareToolTips
+ tooltipErrorTime={tooltipErrorTime}
tooltipToggleTime={tooltipToggleTime}
- showErrorTooltip={showErrorTooltip}
/>
<InputGroup>
<Input
@@ -178,15 +196,10 @@ class ShareLinkContainer extends Component {
e.preventDefault();
}}
/>
-
<CopyToClipboard
options={window.clipboardData ? {} : { format: 'text/plain' }}
text={value}
- onCopy={() => {
- this.setState({
- tooltipToggleTime: Date.now(),
- });
- }}
+ onCopy={this.onCopyToClipboard}
>
<InputGroupAddon addonType="append">
<Button id="copy-to-clipboard-button">COPY</Button>