Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

simplify and fix existing layer&palette action/reducers #1623

Benjamin Kinga95541da866a
diff --git a/web/js/components/layer/product-picker/product-picker.js b/web/js/components/layer/product-picker/product-picker.js
index b5f197b52..3b85a59b0 100644
--- a/web/js/components/layer/product-picker/product-picker.js
+++ b/web/js/components/layer/product-picker/product-picker.js
@@ -267,9 +267,9 @@ const mapDispatchToProps = dispatch => ({
}
});
function mapStateToProps(state, ownProps) {
- const { config, browser, proj, layers } = state;
+ const { config, browser, proj, layers, compare } = state;
const { screenWidth, screenHeight } = browser;
- const { activeString } = layers;
+ const activeString = compare.isActiveA ? 'active' : 'activeB';
const height = screenHeight - 100;
const width = getModalWidth(screenWidth);
const allLayers = getLayersForProjection(config, proj.id);
diff --git a/web/js/components/layer/settings/settings.js b/web/js/components/layer/settings/settings.js
index 6e6736a67..23f2f8571 100644
--- a/web/js/components/layer/settings/settings.js
+++ b/web/js/components/layer/settings/settings.js
@@ -22,6 +22,7 @@ import {
setCustom,
clearCustom
} from '../../../modules/palettes/actions';
+import { setOpacity } from '../../../modules/layers/actions';
class LayerSettings extends React.Component {
constructor(props) {
@@ -257,6 +258,9 @@ const mapDispatchToProps = dispatch => ({
},
clearCustom: (layerId, index, groupName) => {
dispatch(clearCustom(layerId, index, groupName));
+ },
+ setOpacity: (id, opacity) => {
+ dispatch(setOpacity(id, opacity));
}
});
diff --git a/web/js/components/sidebar/data/data.js b/web/js/components/sidebar/data/data.js
index 7b1c502e5..8d7f9a3d9 100644
--- a/web/js/components/sidebar/data/data.js
+++ b/web/js/components/sidebar/data/data.js
@@ -70,9 +70,9 @@ const mapDispatchToProps = dispatch => ({
});
function mapStateToProps(state, ownProps) {
const { tabTypes } = ownProps;
- const { layers, proj, data, config } = state;
+ const { layers, proj, data, config, compare } = state;
const { selectedProduct, selectedGranules } = data;
- const { activeString } = layers;
+ const activeString = compare.isCompareA ? 'active' : 'activeB';
const activeLayers = layers[activeString];
const counts = getSelectionCounts(activeLayers, selectedGranules);
const products = getDataProductsFromActiveLayers(
diff --git a/web/js/components/sidebar/footer-content.js b/web/js/components/sidebar/footer-content.js
index 791b5f759..a1c34695c 100644
--- a/web/js/components/sidebar/footer-content.js
+++ b/web/js/components/sidebar/footer-content.js
@@ -149,12 +149,12 @@ const mapDispatchToProps = dispatch => ({
});
function mapStateToProps(state, ownProps) {
const { activeTab } = ownProps;
- const { requestedEvents, config, layers, data } = state;
+ const { requestedEvents, config, layers, data, compare } = state;
const { showAll } = state.events;
+ const { selectedGranules } = data;
const showListAllButton = !showAll;
const events = lodashGet(requestedEvents, 'response');
- const { activeString } = layers;
- const { selectedGranules } = data;
+ const activeString = compare.isCompareA ? 'active' : 'activeB';
const activeLayers = layers[activeString];
const counts = getSelectionCounts(activeLayers, selectedGranules);
const dataSelectionSize = getDataSelectionSize(selectedGranules);
diff --git a/web/js/components/sidebar/products/layer.js b/web/js/components/sidebar/products/layer.js
index df3838839..9002e5074 100644
--- a/web/js/components/sidebar/products/layer.js
+++ b/web/js/components/sidebar/products/layer.js
@@ -166,7 +166,7 @@ class Layer extends React.Component {
: visibilityButtonClasses + ' layer-enabled layer-visible'
}
id={'hide' + util.encodeId(layer.id)}
- onClick={() => toggleVisibility(layer.id)}
+ onClick={() => toggleVisibility(layer.id, !isVisible)}
title={
!isVisible && !isDisabled
? 'Show Layer'
@@ -314,8 +314,8 @@ const mapDispatchToProps = dispatch => ({
hover: (id, value) => {
dispatch(layerHover(id, value));
},
- toggleVisibility: id => {
- dispatch(toggleVisibility(id));
+ toggleVisibility: (id, isVisible) => {
+ dispatch(toggleVisibility(id, isVisible));
},
onRemoveClick: id => {
dispatch(removeLayer(id));
diff --git a/web/js/modules/layers/actions.js b/web/js/modules/layers/actions.js
index b219879fa..b15f4c1c2 100644
--- a/web/js/modules/layers/actions.js
+++ b/web/js/modules/layers/actions.js
@@ -1,3 +1,8 @@
+import { findIndex as lodashFindIndex } from 'lodash';
+import {
+ addLayer as addLayerSelector,
+ resetLayers as resetLayersSelector
+} from './selectors';
import {
RESET_LAYERS,
ADD_LAYER,
@@ -6,19 +11,42 @@ import {
TOGGLE_ACTIVE_STATE,
ON_LAYER_HOVER,
TOGGLE_LAYER_VISIBILITY,
- REMOVE_LAYER
+ REMOVE_LAYER,
+ UPDATE_OPACITY
} from './constants';
-export function addLayer(layerId) {
- return {
- type: ADD_LAYER,
- id: layerId
+export function resetLayers(activeString) {
+ return (dispatch, getState) => {
+ const { config } = getState();
+ const newLayers = resetLayersSelector(
+ config.defaults.startingLayers,
+ config.layers
+ );
+
+ dispatch({
+ type: RESET_LAYERS,
+ activeString,
+ layers: newLayers
+ });
};
}
-export function resetLayers(stateStr) {
- return {
- type: RESET_LAYERS,
- stateStr: stateStr || 'active'
+export function addLayer(id) {
+ return (dispatch, getState) => {
+ const { layers, compare } = getState();
+ const activeString = compare.isActiveA ? 'active' : 'activeB';
+ const newLayers = addLayerSelector(
+ id,
+ {},
+ layers[activeString],
+ layers.layerConfig
+ );
+
+ dispatch({
+ type: ADD_LAYER,
+ id,
+ activeString,
+ layers: newLayers
+ });
};
}
export function initSecondLayerGroup() {
@@ -44,15 +72,59 @@ export function layerHover(id, isMouseOver) {
active: isMouseOver
};
}
-export function toggleVisibility(id) {
- return {
- type: TOGGLE_LAYER_VISIBILITY,
- id: id
+export function toggleVisibility(id, visible) {
+ return (dispatch, getState) => {
+ const { layers, compare } = getState();
+ const activeString = compare.isActiveA ? 'active' : 'activeB';
+ const index = lodashFindIndex(layers[activeString], {
+ id: id
+ });
+
+ dispatch({
+ type: TOGGLE_LAYER_VISIBILITY,
+ id,
+ index,
+ visible,
+ activeString
+ });
};
}
export function removeLayer(id) {
- return {
- type: REMOVE_LAYER,
- id: id
+ return (dispatch, getState) => {
+ const { layers, compare } = getState();
+ const activeString = compare.isActiveA ? 'active' : 'activeB';
+ const index = lodashFindIndex(layers[activeString], {
+ id: id
+ });
+ if (index === -1) {
+ throw new Error('Invalid layer ID: ' + id);
+ }
+
+ dispatch({
+ type: REMOVE_LAYER,
+ id,
+ index,
+ activeString
+ });
+ };
+}
+export function setOpacity(id, opacity) {
+ return (dispatch, getState) => {
+ const { layers, compare } = getState();
+ const activeString = compare.isActiveA ? 'active' : 'activeB';
+ const index = lodashFindIndex(layers[activeString], {
+ id: id
+ });
+ if (index === -1) {
+ throw new Error('Invalid layer ID: ' + id);
+ }
+
+ dispatch({
+ type: UPDATE_OPACITY,
+ id,
+ index,
+ opacity,
+ activeString
+ });
};
}
diff --git a/web/js/modules/layers/constants.js b/web/js/modules/layers/constants.js
index 828ac1a85..8eba6b4da 100644
--- a/web/js/modules/layers/constants.js
+++ b/web/js/modules/layers/constants.js
@@ -6,3 +6,4 @@ export const TOGGLE_ACTIVE_STATE = 'LAYERS/TOGGLE_ACTIVE_LAYER_STATE';
export const ON_LAYER_HOVER = 'LAYERS/ON_LAYER_HOVER';
export const TOGGLE_LAYER_VISIBILITY = 'LAYERS/TOGGLE_LAYER_VISIBILITY';
export const REMOVE_LAYER = 'LAYERS/REMOVE_LAYER';
+export const UPDATE_OPACITY = 'LAYERS/UPDATE_OPACITY';
diff --git a/web/js/modules/layers/reducers.js b/web/js/modules/layers/reducers.js
index 3f0052a2f..c2ab88718 100644
--- a/web/js/modules/layers/reducers.js
+++ b/web/js/modules/layers/reducers.js
@@ -1,22 +1,21 @@
import {
RESET_LAYERS,
- TOGGLE_ACTIVE_STATE,
ADD_LAYER,
INIT_SECOND_LAYER_GROUP,
REORDER_LAYER_GROUP,
ON_LAYER_HOVER,
TOGGLE_LAYER_VISIBILITY,
- REMOVE_LAYER
+ REMOVE_LAYER,
+ UPDATE_OPACITY
} from './constants';
-import { resetLayers, addLayer } from './selectors';
-import { toggleVisibility, removeLayer } from './util';
+import { resetLayers } from './selectors';
import { cloneDeep as lodashCloneDeep, assign as lodashAssign } from 'lodash';
+import update from 'immutability-helper';
export const initialState = {
active: [],
activeB: [],
layersConfig: {},
- activeString: 'active',
hoveredLayer: '',
layerConfig: {},
startingLayers: []
@@ -30,30 +29,11 @@ export function getInitialState(config) {
}
export function layerReducer(state = initialState, action) {
- const layerGroupStr = state.activeString;
+ const layerGroupStr = action.activeString;
switch (action.type) {
- case RESET_LAYERS:
- if (
- (action.stateStr && action.stateStr === 'activeB') ||
- state.activeString === 'activeB'
- ) {
- return lodashAssign({}, state, {
- layersB: resetLayers()
- });
- } else {
- return lodashAssign({}, state, {
- layersA: resetLayers()
- });
- }
-
- case ADD_LAYER:
+ case RESET_LAYERS || ADD_LAYER:
return lodashAssign({}, state, {
- [layerGroupStr]: addLayer(
- action.id,
- {},
- state[layerGroupStr],
- state.layerConfig
- )
+ [layerGroupStr]: action.layers
});
case INIT_SECOND_LAYER_GROUP:
if (state.layersB.length > 0) return state;
@@ -64,21 +44,25 @@ export function layerReducer(state = initialState, action) {
return lodashAssign({}, state, {
[layerGroupStr]: action.layerArray
});
- case TOGGLE_ACTIVE_STATE:
- return lodashAssign({}, state, {
- activeString: state.activeString === 'active' ? 'activeB' : 'active'
- });
case ON_LAYER_HOVER:
return lodashAssign({}, state, {
hoveredLayer: action.active ? action.id : ''
});
case TOGGLE_LAYER_VISIBILITY:
- return lodashAssign({}, state, {
- [layerGroupStr]: toggleVisibility(action.id, state[layerGroupStr])
+ return update(state, {
+ [layerGroupStr]: {
+ [action.index]: { visible: { $set: action.visible } }
+ }
});
case REMOVE_LAYER:
- return lodashAssign({}, state, {
- [layerGroupStr]: removeLayer(action.id, state[layerGroupStr]) // returns new object
+ return update(state, {
+ [layerGroupStr]: { $splice: [[action.index, 1]] }
+ });
+ case UPDATE_OPACITY:
+ return update(state, {
+ [layerGroupStr]: {
+ [action.index]: { opacity: { $set: action.opacity } }
+ }
});
default:
return state;
diff --git a/web/js/modules/palettes/actions.js b/web/js/modules/palettes/actions.js
index 4b2d7061a..e1264c0ce 100644
--- a/web/js/modules/palettes/actions.js
+++ b/web/js/modules/palettes/actions.js
@@ -40,11 +40,11 @@ export function setRange(layerId, min, max, squash, index, groupName) {
palettes.rendered,
config
);
- return {
+ dispatch({
type: SET_RANGE_AND_SQUASH,
groupName: groupName,
palettes: newActivePalettesObj
- };
+ });
};
}
export function setCustom(layerId, paletteId, index, groupName) {
@@ -58,11 +58,11 @@ export function setCustom(layerId, paletteId, index, groupName) {
config,
palettes.rendered
);
- return {
+ dispatch({
type: SET_CUSTOM,
groupName: groupName,
palettes: newActivePalettesObj
- };
+ });
};
}
export function clearCustom(layerId, index, groupName) {
@@ -74,11 +74,11 @@ export function clearCustom(layerId, index, groupName) {
palettes[groupName]
);
- return {
+ dispatch({
type: CLEAR_CUSTOM,
groupName: groupName,
palettes: newActivePalettesObj
- };
+ });
};
}
export function loadedCustomPalettes(customs) {