Interactive interface for browsing global, full-resolution satellite imagery
Refactor to reduce code duplication and simplify
Jason Kentb947fe40c375
diff --git a/web/js/components/timeline/custom-interval-selector/interval-selector-widget.js b/web/js/components/timeline/custom-interval-selector/custom-interval-selector.jssimilarity index 52%rename from web/js/components/timeline/custom-interval-selector/interval-selector-widget.jsrename to web/js/components/timeline/custom-interval-selector/custom-interval-selector.jsindex 8caa3c736..9234a2892 100644--- a/web/js/components/timeline/custom-interval-selector/interval-selector-widget.js+++ b/web/js/components/timeline/custom-interval-selector/custom-interval-selector.js@@ -3,35 +3,55 @@ import PropTypes from 'prop-types';import { connect } from 'react-redux';import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';import DeltaInput from './delta-input';-import TimeScaleSelect from './interval-select';+import IntervalSelect from './interval-select';import {timeScaleFromNumberKey,timeScaleToNumberKey,} from '../../../modules/date/constants';-import { toggleCustomModal } from '../../../modules/date/actions';-+import {+ toggleCustomModal,+ selectInterval as selectIntervalAction,+ changeCustomInterval as changeCustomIntervalAction,+} from '../../../modules/date/actions';/*- * CustomIntervalSelectorWidget for Custom Interval Selector+ * CustomIntervalSelector for Custom Interval Selector* group. It is a parent component of this group.*- * @class CustomIntervalSelectorWidget+ * @class CustomIntervalSelector*/-class CustomIntervalSelectorWidget extends PureComponent {+class CustomIntervalSelector extends PureComponent {componentDidUpdate(prevProps) {- const { customIntervalModalOpen } = this.props;- // handle focus widget on opening- if (customIntervalModalOpen && !prevProps.customIntervalModalOpen) {+ const {+ changeCustomInterval,+ customInterval,+ hasSubdailyLayers,+ interval,+ selectInterval,+ modalOpen,+ } = this.props;++ if (modalOpen && !prevProps.modalOpen) {this.customIntervalWidget.focus();}++ const subdailyInterval = customInterval > 3 || interval > 3;+ if (!hasSubdailyLayers && prevProps.hasSubdailyLayers && subdailyInterval) {+ changeCustomInterval();+ selectInterval(1, timeScaleToNumberKey.day, false);+ }++ if (hasSubdailyLayers && !prevProps.hasSubdailyLayers) {+ changeCustomInterval(10, timeScaleToNumberKey.minute);+ }}changeDelta = (value) => {const {- changeCustomInterval, customIntervalZoomLevel,+ changeCustomInterval, customInterval,} = this.props;if (value >= 0 && value <= 1000) {- changeCustomInterval(value, customIntervalZoomLevel);+ changeCustomInterval(value, customInterval);}}@@ -49,13 +69,13 @@ class CustomIntervalSelectorWidget extends PureComponent {render() {const {- customIntervalModalOpen,+ modalOpen,hasSubdailyLayers,customDelta,- customIntervalZoomLevel,+ customInterval,closeModal,} = this.props;- return customIntervalModalOpen && (+ return modalOpen && (<divonKeyDown={this.handleKeyPress}className={`custom-interval-widget ${hasSubdailyLayers ? 'subdaily' : ''}`}@@ -68,9 +88,9 @@ class CustomIntervalSelectorWidget extends PureComponent {deltaValue={customDelta}changeDelta={this.changeDelta}/>- <TimeScaleSelect+ <IntervalSelecthasSubdailyLayers={hasSubdailyLayers}- zoomLevel={timeScaleFromNumberKey[customIntervalZoomLevel]}+ zoomLevel={timeScaleFromNumberKey[customInterval]}changeZoomLevel={this.changeZoomLevel}/></div>@@ -84,18 +104,38 @@ const mapDispatchToProps = (dispatch) => ({closeModal: () => {dispatch(toggleCustomModal(false, undefined));},+ changeCustomInterval: (delta, timeScale) => {+ dispatch(changeCustomIntervalAction(delta, timeScale));+ },+ selectInterval: (delta, timeScale, customSelected) => {+ dispatch(selectIntervalAction(delta, timeScale, customSelected));+ },});+const mapStateToProps = (state) => {+ const { date } = state;+ const {+ interval, customInterval, customDelta,+ } = date;+ return {+ customDelta: customDelta || 1,+ customInterval: customInterval || interval,+ interval,+ };+};+export default connect(- null,+ mapStateToProps,mapDispatchToProps,-)(CustomIntervalSelectorWidget);+)(CustomIntervalSelector);-CustomIntervalSelectorWidget.propTypes = {+CustomIntervalSelector.propTypes = {changeCustomInterval: PropTypes.func,closeModal: PropTypes.func,customDelta: PropTypes.number,- customIntervalModalOpen: PropTypes.bool,- customIntervalZoomLevel: PropTypes.number,+ customInterval: PropTypes.number,hasSubdailyLayers: PropTypes.bool,+ interval: PropTypes.number,+ selectInterval: PropTypes.func,+ modalOpen: PropTypes.bool,};diff --git a/web/js/components/timeline/custom-interval-selector/interval-select.js b/web/js/components/timeline/custom-interval-selector/interval-select.jsindex 86fb67156..17f8c6477 100644--- a/web/js/components/timeline/custom-interval-selector/interval-select.js+++ b/web/js/components/timeline/custom-interval-selector/interval-select.js@@ -7,7 +7,7 @@ import PropTypes from 'prop-types';** @class TimeScaleSelect*/-class TimeScaleSelect extends PureComponent {+class IntervalSelect extends PureComponent {handleChangeZoomLevel = (e) => {const { changeZoomLevel } = this.props;const zoomLevel = e.target.value;@@ -50,10 +50,10 @@ class TimeScaleSelect extends PureComponent {}}-TimeScaleSelect.propTypes = {+IntervalSelect.propTypes = {changeZoomLevel: PropTypes.func,hasSubdailyLayers: PropTypes.bool,zoomLevel: PropTypes.string,};-export default TimeScaleSelect;+export default IntervalSelect;diff --git a/web/js/components/timeline/timeline-controls/interval-timescale-change.js b/web/js/components/timeline/timeline-controls/timescale-interval-change.jssimilarity index 71%rename from web/js/components/timeline/timeline-controls/interval-timescale-change.jsrename to web/js/components/timeline/timeline-controls/timescale-interval-change.jsindex ffa1f1d56..212bee8a3 100644--- a/web/js/components/timeline/timeline-controls/interval-timescale-change.js+++ b/web/js/components/timeline/timeline-controls/timescale-interval-change.js@@ -1,5 +1,14 @@import React, { PureComponent } from 'react';import PropTypes from 'prop-types';+import { connect } from 'react-redux';+import {+ timeScaleToNumberKey,+ timeScaleFromNumberKey,+} from '../../../modules/date/constants';+import {+ toggleCustomModal as toggleCustomModalAction,+ selectInterval as selectIntervalAction,+} from '../../../modules/date/actions';/** Change timescale interval (time unit) by selecting from default timescales@@ -19,8 +28,8 @@ class TimeScaleIntervalChange extends PureComponent {}componentDidMount() {- const { customDelta, customIntervalZoomLevel } = this.props;- if (customDelta !== 1 && customIntervalZoomLevel) {+ const { customDelta, customInterval } = this.props;+ if (customDelta !== 1 && customInterval) {this.setCustomIntervalText();}}@@ -48,14 +57,12 @@ class TimeScaleIntervalChange extends PureComponent {}}- // Interval select tooltip ontoolTipHoverOn = () => {this.setState({toolTipHovered: true,});}- // Interval select tooltip offtoolTipHoverOff = () => {this.setState({toolTipHovered: false,@@ -69,20 +76,46 @@ class TimeScaleIntervalChange extends PureComponent {});}- // handle click of timescale intervalshandleClickInterval = (timescale, openModal = false) => {- const { setTimeScaleIntervalChangeUnit } = this.props;// send props function to change timescale interval throughout appthis.setState({toolTipHovered: false,- }, setTimeScaleIntervalChangeUnit(timescale, openModal));+ }, this.setTimeScaleIntervalChangeUnit(timescale, openModal));}+ /**+ * @desc handle SELECT of LEFT/RIGHT interval selection+ * @param {String} timeScale+ * @param {Boolean} modalOpen - is custom interval modal open+ */+ setTimeScaleIntervalChangeUnit = (timeScale, openModal) => {+ const {+ customInterval, customDelta, selectInterval, toggleCustomModal, modalType,+ } = this.props;+ const customSelected = timeScale === 'custom';+ let delta;+ let newTimeScale = timeScale;++ if (openModal) {+ toggleCustomModal(openModal, modalType);+ return;+ }++ if (customSelected && customInterval && customDelta) {+ newTimeScale = customInterval;+ delta = customDelta;+ } else {+ newTimeScale = Number(timeScaleToNumberKey[newTimeScale]);+ delta = 1;+ }+ selectInterval(delta, newTimeScale, customSelected);+ };+// set custom text for custom intervalsetCustomIntervalText = () => {- const { customDelta, customIntervalZoomLevel } = this.props;+ const { customDelta, customInterval } = this.props;this.setState({- customIntervalText: `${customDelta} ${customIntervalZoomLevel}`,+ customIntervalText: `${customDelta} ${timeScaleFromNumberKey[customInterval]}`,});}@@ -101,7 +134,7 @@ class TimeScaleIntervalChange extends PureComponent {const {customSelected,hasSubdailyLayers,- timeScaleChangeUnit,+ interval,} = this.props;return (<>@@ -117,7 +150,7 @@ class TimeScaleIntervalChange extends PureComponent {id="current-interval"className={`no-drag interval-btn interval-btn-active${customSelected ? ' custom-interval-text' : ''}`}>- {customSelected ? customIntervalText : `${1} ${timeScaleChangeUnit}`}+ {customSelected ? customIntervalText : `${1} ${timeScaleFromNumberKey[interval]}`}</span>{/* hover timeScale unit dialog / entry point to Custom selector */}@@ -188,13 +221,41 @@ class TimeScaleIntervalChange extends PureComponent {}}+const mapDispatchToProps = (dispatch) => ({+ toggleCustomModal: (isOpen, modalType) => {+ dispatch(toggleCustomModalAction(isOpen, modalType));+ },+ selectInterval: (delta, timeScale, customSelected) => {+ dispatch(selectIntervalAction(delta, timeScale, customSelected));+ },+});++const mapStateToProps = (state) => {+ const { date } = state;+ const {+ interval, customInterval, customDelta, customSelected,+ } = date;+ return {+ interval,+ customInterval,+ customDelta,+ customSelected,+ };+};+TimeScaleIntervalChange.propTypes = {customDelta: PropTypes.number,- customIntervalZoomLevel: PropTypes.string,+ customInterval: PropTypes.number,customSelected: PropTypes.bool,hasSubdailyLayers: PropTypes.bool,- setTimeScaleIntervalChangeUnit: PropTypes.func,+ interval: PropTypes.number,+ selectInterval: PropTypes.func,timeScaleChangeUnit: PropTypes.string,+ toggleCustomModal: PropTypes.func,+ modalType: PropTypes.string,};-export default TimeScaleIntervalChange;+export default connect(+ mapStateToProps,+ mapDispatchToProps,+)(TimeScaleIntervalChange);diff --git a/web/js/containers/animation-widget.js b/web/js/containers/animation-widget.jsindex a020ae71c..534a72159 100644--- a/web/js/containers/animation-widget.js+++ b/web/js/containers/animation-widget.js@@ -17,8 +17,8 @@ import ErrorBoundary from './error-boundary';import DateRangeSelector from '../components/date-selector/date-range-selector';import LoopButton from '../components/animation-widget/loop-button';import PlayButton from '../components/animation-widget/play-button';-import TimeScaleIntervalChange from '../components/timeline/timeline-controls/interval-timescale-change';-import CustomIntervalSelectorWidget from '../components/timeline/custom-interval-selector/interval-selector-widget';+import TimeScaleIntervalChange from '../components/timeline/timeline-controls/timescale-interval-change';+import CustomIntervalSelector from '../components/timeline/custom-interval-selector/custom-interval-selector';import PlayQueue from '../components/animation-widget/play-queue';import Notify from '../components/image-download/notify';import { promiseImageryForTime } from '../modules/map/util';@@ -26,7 +26,6 @@ import GifContainer from './gif';import {selectDate,selectInterval,- changeCustomInterval,toggleCustomModal,} from '../modules/date/actions';import {@@ -344,17 +343,6 @@ class AnimationWidget extends React.Component {toggleCustomModal(isOpen, customModalType.ANIMATION);};- /**- * @desc handle SET of custom time scale panel- * @param {Number} delta- * @param {Number} timeScale- * @returns {void}- */- changeCustomInterval = (delta, timeScale) => {- const { changeCustomInterval } = this.props;- changeCustomInterval(delta, timeScale);- };-renderCollapsedWidget() {const {onClose,@@ -479,9 +467,6 @@ class AnimationWidget extends React.Component {onPushPause,subDailyMode,interval,- customSelected,- customDelta,- customInterval,animationCustomModalOpen,hasSubdailyLayers,} = this.props;@@ -505,16 +490,19 @@ class AnimationWidget extends React.Component {<div className="wv-animation-widget-header">{'Animate Map in '}<TimeScaleIntervalChange- setTimeScaleIntervalChangeUnit={this.onIntervalSelect}- customIntervalZoomLevel={timeScaleFromNumberKey[customInterval]}- customSelected={customSelected}- customDelta={customDelta}timeScaleChangeUnit={interval}hasSubdailyLayers={hasSubdailyLayers}+ modalType={customModalType.ANIMATION}/>{' Increments'}</div>+ {/* Custom time interval selection */}+ <CustomIntervalSelector+ modalOpen={animationCustomModalOpen}+ hasSubdailyLayers={hasSubdailyLayers}+ />+<PlayButtonplaying={isPlaying}play={this.onPushPlay}@@ -554,14 +542,6 @@ class AnimationWidget extends React.Component {<FontAwesomeIcon icon="chevron-down" className="wv-minimize" onClick={this.toggleCollapse} /><FontAwesomeIcon icon="times" className="wv-close" onClick={onClose} />- {/* Custom time interval selection */}- <CustomIntervalSelectorWidget- customDelta={customDelta}- customIntervalZoomLevel={customInterval}- customIntervalModalOpen={animationCustomModalOpen}- changeCustomInterval={this.changeCustomInterval}- hasSubdailyLayers={hasSubdailyLayers}- /></div></div></Draggable>@@ -822,9 +802,6 @@ const mapDispatchToProps = (dispatch) => ({onIntervalSelect: (delta, timeScale, customSelected) => {dispatch(selectInterval(delta, timeScale, customSelected));},- changeCustomInterval: (delta, timeScale) => {- dispatch(changeCustomInterval(delta, timeScale));- },onUpdateStartDate(date) {dispatch(changeStartDate(date));},@@ -851,11 +828,9 @@ AnimationWidget.propTypes = {activePalettes: PropTypes.object,animationCustomModalOpen: PropTypes.bool,visibleLayersForProj: PropTypes.array,- changeCustomInterval: PropTypes.func,currentDate: PropTypes.object,customDelta: PropTypes.number,customInterval: PropTypes.number,- customSelected: PropTypes.bool,delta: PropTypes.number,endDate: PropTypes.object,hasCustomPalettes: PropTypes.bool,diff --git a/web/js/containers/timeline/timeline.js b/web/js/containers/timeline/timeline.jsindex 3d0292ee8..aeae5ae72 100644--- a/web/js/containers/timeline/timeline.js+++ b/web/js/containers/timeline/timeline.js@@ -15,11 +15,11 @@ import MobileDatePicker from '../../components/timeline/mobile-date-picker';import TimelineAxis from '../../components/timeline/timeline-axis/timeline-axis';import TimelineLayerCoveragePanel from '../../components/timeline/timeline-coverage/timeline-coverage';-import TimeScaleIntervalChange from '../../components/timeline/timeline-controls/interval-timescale-change';+import TimeScaleIntervalChange from '../../components/timeline/timeline-controls/timescale-interval-change';import DraggerContainer from '../../components/timeline/timeline-draggers/dragger-container';import AxisHoverLine from '../../components/timeline/timeline-axis/date-tooltip/axis-hover-line';import DateTooltip from '../../components/timeline/timeline-axis/date-tooltip/date-tooltip';-import CustomIntervalSelectorWidget from '../../components/timeline/custom-interval-selector/interval-selector-widget';+import CustomIntervalSelector from '../../components/timeline/custom-interval-selector/custom-interval-selector';import DateSelector from '../../components/date-selector/date-selector';import DateChangeArrows from '../../components/timeline/timeline-controls/date-change-arrows';@@ -559,44 +559,6 @@ class Timeline extends React.Component {}}- /**- * @desc handle SET of custom time scale panel- * @param {Number} delta- * @param {Number} timeScale- * @returns {void}- */- changeCustomInterval = (delta, timeScale) => {- const { changeCustomInterval } = this.props;- changeCustomInterval(delta, timeScale);- };-- /**- * @desc handle SELECT of LEFT/RIGHT interval selection- * @param {String} timeScale- * @param {Boolean} modalOpen - is custom interval modal open- * @returns {void}- */- setTimeScaleIntervalChangeUnit = (timeScale, openModal) => {- const { customIntervalZoomLevel, customIntervalValue, selectInterval } = this.props;- const customSelected = timeScale === 'custom';- let delta;- let newTimeScale = timeScale;-- if (openModal) {- this.toggleCustomIntervalModal(openModal);- return;- }-- if (customSelected && customIntervalZoomLevel && customIntervalValue) {- newTimeScale = customIntervalZoomLevel;- delta = customIntervalValue;- } else {- newTimeScale = Number(timeScaleToNumberKey[newTimeScale]);- delta = 1;- }- selectInterval(delta, newTimeScale, customSelected);- };-/*** @desc open animation widget* @returns {void}@@ -990,9 +952,6 @@ class Timeline extends React.Component {animStartLocationDate,appNow,axisWidth,- customIntervalValue,- customIntervalZoomLevel,- customSelected,dateA,dateB,draggerSelected,@@ -1098,14 +1057,13 @@ class Timeline extends React.Component {/></div><div id="zoom-buttons-group">+<TimeScaleIntervalChange- setTimeScaleIntervalChangeUnit={this.setTimeScaleIntervalChangeUnit}- customIntervalZoomLevel={timeScaleFromNumberKey[customIntervalZoomLevel]}- customSelected={customSelected}- customDelta={customIntervalValue}timeScaleChangeUnit={timeScaleChangeUnit}hasSubdailyLayers={hasSubdailyLayers}+ modalType={customModalType.TIMELINE}/>+{this.renderDateChangeArrows()}</div><AnimationButton@@ -1267,12 +1225,10 @@ class Timeline extends React.Component {)}</div>)}+{/* Custom Interval Selector Widget */}- <CustomIntervalSelectorWidget- customDelta={customIntervalValue}- customIntervalZoomLevel={customIntervalZoomLevel}- changeCustomInterval={this.changeCustomInterval}- customIntervalModalOpen={timelineCustomModalOpen}+ <CustomIntervalSelector+ modalOpen={timelineCustomModalOpen}hasSubdailyLayers={hasSubdailyLayers}/>@@ -1511,11 +1467,8 @@ Timeline.propTypes = {animEndLocationDate: PropTypes.object,animStartLocationDate: PropTypes.object,axisWidth: PropTypes.number,- changeCustomInterval: PropTypes.func,changeTimeScale: PropTypes.func,closeAnimation: PropTypes.func,- customIntervalValue: PropTypes.number,- customIntervalZoomLevel: PropTypes.number,customSelected: PropTypes.bool,dateA: PropTypes.string,dateB: PropTypes.string,@@ -1546,7 +1499,6 @@ Timeline.propTypes = {screenWidth: PropTypes.number,selectDate: PropTypes.func,selectedDate: PropTypes.object,- selectInterval: PropTypes.func,timelineCustomModalOpen: PropTypes.bool,timelineEndDateLimit: PropTypes.string,timelineStartDateLimit: PropTypes.string,diff --git a/web/js/modules/date/actions.js b/web/js/modules/date/actions.jsindex 4d88d79bb..c305fcccc 100644--- a/web/js/modules/date/actions.js+++ b/web/js/modules/date/actions.js@@ -86,7 +86,7 @@ export function changeCustomInterval(delta, customInterval) {dispatch(clearPreload());dispatch({type: CHANGE_CUSTOM_INTERVAL,- value: customInterval,+ interval: customInterval,delta,});};@@ -96,7 +96,7 @@ export function selectInterval(delta, interval, customSelected) {dispatch(clearPreload());dispatch({type: CHANGE_INTERVAL,- value: interval,+ interval,delta,customSelected,});diff --git a/web/js/modules/date/actions.test.js b/web/js/modules/date/actions.test.jsindex 5b25d79eb..b9a447a3a 100644--- a/web/js/modules/date/actions.test.js+++ b/web/js/modules/date/actions.test.js@@ -111,7 +111,7 @@ describe('Date timescale changes', () => {expect(store.getActions()[1]).toEqual(expectedSecond);});- test(`selectDate action returns ${SELECT_DATE}as type and selectedB as activeString and ${mockDate} as value`,+ test(`selectDate action returns ${SELECT_DATE} as type and selectedB as activeString and ${mockDate} as value`,() => {const expectedFirst = {type: CLEAR_PRELOAD,@@ -163,7 +163,7 @@ describe('Date timescale changes', () => {};const expectedSecond = {type: CHANGE_CUSTOM_INTERVAL,- value: customInterval,+ interval: customInterval,delta,};store.dispatch(changeCustomInterval(delta, customInterval));@@ -188,7 +188,7 @@ describe('Date timescale changes', () => {};const expectedSecond = {type: CHANGE_INTERVAL,- value: interval,+ interval,delta,customSelected: true,};diff --git a/web/js/modules/date/reducers.js b/web/js/modules/date/reducers.jsindex eb93bf56c..e20bf1a41 100644--- a/web/js/modules/date/reducers.js+++ b/web/js/modules/date/reducers.js@@ -51,17 +51,19 @@ export function dateReducer(state = dateReducerState, action) {...state,selectedB: util.dateAdd(state.selected, 'day', -7),};- case CHANGE_CUSTOM_INTERVAL:+ case CHANGE_CUSTOM_INTERVAL: {+ const { interval, delta } = action;return {...state,- customInterval: action.value,- customDelta: action.delta,- customSelected: true,+ customInterval: interval,+ customDelta: delta,+ customSelected: !(!interval && !delta),};+ }case CHANGE_INTERVAL:return {...state,- interval: action.value,+ interval: action.interval,delta: action.delta,customSelected: action.customSelected,};diff --git a/web/js/modules/date/reducers.test.js b/web/js/modules/date/reducers.test.jsindex 75926e9ff..af40f5221 100644--- a/web/js/modules/date/reducers.test.js+++ b/web/js/modules/date/reducers.test.js@@ -45,7 +45,7 @@ describe('dateReducer', () => {expect(dateReducer(dateReducerState, {type: CHANGE_CUSTOM_INTERVAL,- value: 4,+ interval: 4,delta: 10,}),).toEqual({@@ -64,7 +64,7 @@ describe('dateReducer', () => {expect(dateReducer(dateReducerState, {type: CHANGE_INTERVAL,- value: 2,+ interval: 2,delta: 1,customSelected: false,}),