Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

hook up unit of measure toggle to state so that it persists across projections and when the menu modal is destroyed/reopened

Jason Kenta5c24efa66f6
diff --git a/web/js/components/measure-tool/measure-menu.js b/web/js/components/measure-tool/measure-menu.js
index 5d2fe4234..1a685784f 100644
--- a/web/js/components/measure-tool/measure-menu.js
+++ b/web/js/components/measure-tool/measure-menu.js
@@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { onToggle } from '../../modules/modal/actions';
import IconList from '../util/list';
+import { changeUnits } from '../../modules/measure/actions';
// import googleTagManager from 'googleTagManager';
const OPTIONS_ARRAY = [
@@ -27,6 +28,13 @@ const OPTIONS_ARRAY = [
];
class MeasureMenu extends Component {
+ constructor(props) {
+ super(props);
+ this.state = {
+ units: props.units
+ };
+ }
+
triggerEvent(eventName) {
const { map, onCloseModal } = this.props;
map.ui.events.trigger(eventName);
@@ -34,10 +42,9 @@ class MeasureMenu extends Component {
}
unitToggle(evt) {
- const { map } = this.props;
const { checked } = evt.target;
- const value = checked ? 'mi' : 'km';
- map.ui.events.trigger('measure-toggle-units', value);
+ const units = checked ? 'mi' : 'km';
+ this.props.toggleUnits(units);
}
render() {
@@ -49,7 +56,8 @@ class MeasureMenu extends Component {
id="unit-toggle"
className="custom-control-input"
type="checkbox"
- onChange={this.unitToggle.bind(this)}/>
+ onChange={this.unitToggle.bind(this)}
+ defaultChecked={this.state.units === 'mi'}/>
<label className="custom-control-label" htmlFor="unit-toggle">mi</label>
</div>
<IconList
@@ -62,8 +70,14 @@ class MeasureMenu extends Component {
}
}
-const mapStateToProps = (state, ownProps) => ({ map: state.map });
+const mapStateToProps = (state, ownProps) => ({
+ map: state.map,
+ units: state.measure.units
+});
const mapDispatchToProps = (dispatch, ownProps) => ({
+ toggleUnits: (units) => {
+ dispatch(changeUnits(units));
+ },
onCloseModal: () => {
dispatch(onToggle());
}
@@ -75,7 +89,8 @@ export default connect(
)(MeasureMenu);
MeasureMenu.propTypes = {
- dispatchAction: PropTypes.func,
map: PropTypes.object,
- onCloseModal: PropTypes.func
+ onCloseModal: PropTypes.func,
+ toggleUnits: PropTypes.func,
+ units: PropTypes.string
};
diff --git a/web/js/map/ui.js b/web/js/map/ui.js
index 87a02c5fd..6847cf333 100644
--- a/web/js/map/ui.js
+++ b/web/js/map/ui.js
@@ -32,6 +32,7 @@ import { LOCATION_POP_ACTION } from '../redux-location-state-customs';
import { CHANGE_PROJECTION } from '../modules/projection/constants';
import { SELECT_DATE } from '../modules/date/constants';
import { openCustomContent } from '../modules/modal/actions';
+import { CHANGE_UNITS } from '../modules/measure/constants';
import VectorMetaTable from '../components/vector-metadata/table';
import Cache from 'cachai';
import * as layerConstants from '../modules/layers/constants';
@@ -136,6 +137,8 @@ export function mapui(models, config, store, ui) {
case vectorStyleConstants.CLEAR_VECTORSTYLE:
case CALCULATE_RESPONSIVE_STATE:
return onResize();
+ case CHANGE_UNITS:
+ return toggleMeasurementUnits(action.value);
case SELECT_DATE:
return updateDate();
}
@@ -160,7 +163,6 @@ export function mapui(models, config, store, ui) {
self.events.on('measure-clear', clearMeasurements);
self.events.on('measure-distance', measureDistance);
self.events.on('measure-area', measureArea);
- self.events.on('measure-toggle-units', toggleMeasurementUnits);
self.events.on('disable-click-zoom', () => {
self.selected.removeInteraction(doubleClickZoom);
});
@@ -836,8 +838,9 @@ export function mapui(models, config, store, ui) {
};
const toggleMeasurementUnits = (units) => {
- const proj = self.selected.getView().getProjection().getCode();
- measureTools[proj].toggleUnits(units);
+ for (const proj in measureTools) {
+ measureTools[proj].toggleUnits(units);
+ }
};
/*
diff --git a/web/js/modules/combine-reducers.js b/web/js/modules/combine-reducers.js
index 258fbe21e..c028c3c25 100644
--- a/web/js/modules/combine-reducers.js
+++ b/web/js/modules/combine-reducers.js
@@ -37,7 +37,7 @@ import {
import dataDownloadReducer from './data/reducers';
import { get as lodashGet, assign as lodashAssign } from 'lodash';
import { imageDownloadReducer } from './image-download/reducers';
-import { measureReducer } from './measure/reducers';
+import measureReducer from './measure/reducers';
import { LOCATION_POP_ACTION } from '../redux-location-state-customs';
function lastAction(state = null, action) {
diff --git a/web/js/modules/measure/actions.js b/web/js/modules/measure/actions.js
new file mode 100644
index 000000000..782cfa026
--- /dev/null
+++ b/web/js/modules/measure/actions.js
@@ -0,0 +1,8 @@
+import { CHANGE_UNITS } from './constants';
+
+export function changeUnits(unit) {
+ return {
+ type: CHANGE_UNITS,
+ value: unit
+ };
+}
diff --git a/web/js/modules/measure/constants.js b/web/js/modules/measure/constants.js
new file mode 100644
index 000000000..63dd43a24
--- /dev/null
+++ b/web/js/modules/measure/constants.js
@@ -0,0 +1 @@
+export const CHANGE_UNITS = 'MEASURE/CHANGE_UNITS';
diff --git a/web/js/modules/measure/reducers.js b/web/js/modules/measure/reducers.js
new file mode 100644
index 000000000..a49e45808
--- /dev/null
+++ b/web/js/modules/measure/reducers.js
@@ -0,0 +1,17 @@
+import { assign as lodashAssign } from 'lodash';
+import { CHANGE_UNITS } from './constants';
+
+const defaultState = {
+ units: 'km'
+};
+
+export default function measureReducer(state = defaultState, action) {
+ switch (action.type) {
+ case CHANGE_UNITS:
+ return lodashAssign({}, state, {
+ units: action.value
+ });
+ default:
+ return state;
+ }
+}