Interactive interface for browsing global, full-resolution satellite imagery
WV-2764: Modernizing custom-interval-selector component (#4501)
christof-wittreich95f547419f8b
diff --git a/web/js/components/timeline/custom-interval-selector/custom-interval-selector.js b/web/js/components/timeline/custom-interval-selector/custom-interval-selector.jsindex 258eb83ad..4514992d5 100644--- a/web/js/components/timeline/custom-interval-selector/custom-interval-selector.js+++ b/web/js/components/timeline/custom-interval-selector/custom-interval-selector.js@@ -1,6 +1,6 @@-import React, { PureComponent } from 'react';+import React, { useEffect } from 'react';+import { useDispatch, useSelector } from 'react-redux';import PropTypes from 'prop-types';-import { connect } from 'react-redux';import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';import DeltaInput from './delta-input';import IntervalSelect from './interval-select';@@ -19,103 +19,70 @@ import {** @class CustomIntervalSelector*/-class CustomIntervalSelector extends PureComponent {- componentDidUpdate(prevProps) {- const {- modalOpen,- } = this.props;+function CustomIntervalSelector(props) {+ const {+ modalOpen,+ hasSubdailyLayers,+ } = props;++ let customIntervalWidget;++ const customDelta = useSelector((state) => state.date.customDelta || 1);+ const customInterval = useSelector((state) => state.date.customInterval || state.date.interval);- if (modalOpen && !prevProps.modalOpen) {- this.customIntervalWidget.focus();+ const dispatch = useDispatch();+ const closeModal = () => dispatch(toggleCustomModal(false, undefined));+ const changeCustomInterval = (delta, timeScale) => dispatch(changeCustomIntervalAction(delta, timeScale));++ useEffect(() => {+ if (modalOpen) {+ customIntervalWidget.focus();}- }+ }, [modalOpen]);- changeDelta = (value) => {- const {- changeCustomInterval, customInterval,- } = this.props;+ const changeDelta = (value) => {if (value >= 0 && value <= 1000) {changeCustomInterval(value, customInterval);}};- changeZoomLevel = (zoomLevel) => {- const { changeCustomInterval, customDelta } = this.props;+ const changeZoomLevel = (zoomLevel) => {changeCustomInterval(customDelta, TIME_SCALE_TO_NUMBER[zoomLevel]);};- handleKeyPress = (e) => {- const { closeModal } = this.props;+ const handleKeyPress = (e) => {if (e.key === 'Escape') {closeModal();}};- render() {- const {- modalOpen,- hasSubdailyLayers,- customDelta,- customInterval,- closeModal,- } = this.props;- return modalOpen && (- <div- onKeyDown={this.handleKeyPress}- className={`custom-interval-widget ${hasSubdailyLayers ? 'subdaily' : ''}`}- tabIndex={0}- ref={(customIntervalWidget) => { this.customIntervalWidget = customIntervalWidget; }}- >- <h3 className="custom-interval-widget-header">Custom Interval Selector</h3>- <div className="custom-interval-widget-controls-container">- <DeltaInput- deltaValue={customDelta}- changeDelta={this.changeDelta}- />- <IntervalSelect- hasSubdailyLayers={hasSubdailyLayers}- zoomLevel={TIME_SCALE_FROM_NUMBER[customInterval]}- changeZoomLevel={this.changeZoomLevel}- />- </div>- <FontAwesomeIcon icon="times" className="wv-close" onClick={closeModal} />+ return modalOpen && (+ <div+ onKeyDown={handleKeyPress}+ className={`custom-interval-widget ${hasSubdailyLayers ? 'subdaily' : ''}`}+ tabIndex={0}+ ref={(widget) => { customIntervalWidget = widget; }}+ >+ <h3 className="custom-interval-widget-header">Custom Interval Selector</h3>+ <div className="custom-interval-widget-controls-container">+ <DeltaInput+ deltaValue={customDelta}+ changeDelta={changeDelta}+ />+ <IntervalSelect+ hasSubdailyLayers={hasSubdailyLayers}+ zoomLevel={TIME_SCALE_FROM_NUMBER[customInterval]}+ changeZoomLevel={changeZoomLevel}+ /></div>- );- }+ <FontAwesomeIcon icon="times" className="wv-close" onClick={closeModal} />+ </div>+ );}-const mapDispatchToProps = (dispatch) => ({- closeModal: () => {- dispatch(toggleCustomModal(false, undefined));- },- changeCustomInterval: (delta, timeScale) => {- dispatch(changeCustomIntervalAction(delta, timeScale));- },-});--const mapStateToProps = (state) => {- const { date } = state;- const {- interval, customInterval, customDelta, customSelected,- } = date;- return {- customDelta: customDelta || 1,- customInterval: customInterval || interval,- customSelected,- interval,- };-};--export default connect(- mapStateToProps,- mapDispatchToProps,-)(CustomIntervalSelector);-CustomIntervalSelector.propTypes = {- changeCustomInterval: PropTypes.func,- closeModal: PropTypes.func,- customDelta: PropTypes.number,- customInterval: PropTypes.number,hasSubdailyLayers: PropTypes.bool,modalOpen: PropTypes.bool,};++export default CustomIntervalSelector;