Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

re-work selectors

Jason Kenta056f0345692
diff --git a/web/js/components/layer/product-picker/product-picker.js b/web/js/components/layer/product-picker/product-picker.js
index 1f0826f73..2a6ec1c8c 100644
--- a/web/js/components/layer/product-picker/product-picker.js
+++ b/web/js/components/layer/product-picker/product-picker.js
@@ -16,13 +16,12 @@ import {
getLayersForProjection,
} from '../../../modules/layers/selectors';
import { onToggle } from '../../../modules/modal/actions';
-import { availableAtDate } from '../../../modules/layers/util';
import {
updateProductPicker,
} from '../../../modules/product-picker/actions';
import BrowseLayers from './browse-layers';
import SearchLayers from './search-layers';
-import getSearchConfig from '../../../modules/product-picker/searchConfig';
+import { getSearchConfig } from '../../../modules/product-picker/searchConfig';
/*
@@ -37,7 +36,6 @@ class ProductPicker extends React.Component {
this.state = {
modalElement: undefined,
headerElement: undefined,
- searchConfig: undefined,
};
this.runSearch = lodashDebounce(this.runSearch.bind(this), 300);
@@ -50,11 +48,10 @@ class ProductPicker extends React.Component {
const modalElement = document.getElementById('layer_picker_component');
const headerElement = modalElement.querySelector('.modal-header');
modalElement.classList.add('category-width');
- const { allLayers, config, selectedProjection } = this.props;
+
this.setState({
modalElement,
headerElement,
- searchConfig: getSearchConfig(allLayers, config, selectedProjection),
});
}
@@ -237,8 +234,8 @@ class ProductPicker extends React.Component {
listType,
categoryType,
category,
+ searchConfig,
} = this.props;
- const { searchConfig } = this.state;
const { listHeight, listMinHeight, detailHeight } = this.getComponentHeights();
return !searchConfig ? null : (
@@ -330,8 +327,8 @@ function mapStateToProps(state, ownProps) {
const isMobile = browser.lessThan.medium;
const activeString = compare.isCompareA ? 'active' : 'activeB';
const width = getModalWidth(screenWidth);
- const allLayers = getLayersForProjection(state);
const activeLayers = layers[activeString];
+ const searchConfig = getSearchConfig(state);
return {
...productPicker,
@@ -340,9 +337,9 @@ function mapStateToProps(state, ownProps) {
isMobile,
screenHeight,
width,
- allLayers,
activeLayers,
selectedProjection: proj.id,
+ searchConfig,
};
}
export default connect(
diff --git a/web/js/modules/layers/reducers.js b/web/js/modules/layers/reducers.js
index fd56a4b3d..4b342e672 100644
--- a/web/js/modules/layers/reducers.js
+++ b/web/js/modules/layers/reducers.js
@@ -27,7 +27,6 @@ import {
SET_FILTER_RANGE,
} from '../vector-styles/constants';
import { resetLayers } from './selectors';
-import buildLayerFacetProps from './formatConfig';
export const initialState = {
active: [],
@@ -41,7 +40,6 @@ export function getInitialState(config) {
return lodashAssign({}, initialState, {
active: resetLayers(config.defaults.startingLayers, config.layers),
layerConfig: config.layers,
- facetArray: buildLayerFacetProps(config),
startingLayers: config.defaults.startingLayers,
});
}
diff --git a/web/js/modules/layers/selectors.js b/web/js/modules/layers/selectors.js
index a189e109c..ba018d69a 100644
--- a/web/js/modules/layers/selectors.js
+++ b/web/js/modules/layers/selectors.js
@@ -6,37 +6,11 @@ import {
cloneDeep as lodashCloneDeep,
isUndefined as lodashIsUndefined,
values as lodashValues,
- sortBy as lodashSortBy,
- indexOf as lodashIndexOf,
findIndex as lodashFindIndex,
} from 'lodash';
import update from 'immutability-helper';
-import { createSelector } from 'reselect';
import util from '../../util/util';
-const getConfig = (state) => state.config;
-const getProjection = (state) => state.proj && state.proj.id;
-const getFacetLayers = (state) => state.layers.facetArray;
-
-export const getLayersForProjection = createSelector(
- [getConfig, getProjection, getFacetLayers],
- (config, projection, layers) => {
- const filteredRows = layers
- // Only use the layers for the active projection
- .filter((layer) => layer.projections[projection])
- .map((layer) => {
- // If there is metadata for the current projection, use that
- const projectionMeta = layer.projections[projection];
- if (projectionMeta.title) layer.title = projectionMeta.title;
- if (projectionMeta.subtitle) layer.subtitle = projectionMeta.subtitle;
- // Decode HTML entities in the subtitle
- if (layer.subtitle) layer.subtitle = decodeHtml(layer.subtitle);
- return layer;
- });
- return lodashSortBy(filteredRows, (layer) => lodashIndexOf(config.layerOrder, layer.id));
- },
-);
-
export function hasMeasurementSource(current, config, projId) {
let hasSource;
lodashValues(current.sources).forEach((source) => {
@@ -80,12 +54,6 @@ export function hasMeasurementSetting(current, source, config, projId) {
return hasSetting;
}
-function decodeHtml(html) {
- const txt = document.createElement('textarea');
- txt.innerHTML = html;
- return txt.value;
-}
-
/**
* See if an array of layers has a subdaily
* product in it
diff --git a/web/js/modules/layers/formatConfig.js b/web/js/modules/product-picker/formatConfig.js
similarity index 100%
rename from web/js/modules/layers/formatConfig.js
rename to web/js/modules/product-picker/formatConfig.js
diff --git a/web/js/modules/product-picker/searchConfig.js b/web/js/modules/product-picker/searchConfig.js
index f81ceb70a..112744b46 100644
--- a/web/js/modules/product-picker/searchConfig.js
+++ b/web/js/modules/product-picker/searchConfig.js
@@ -4,10 +4,14 @@ import {
forEach as lodashForEach,
toLower as lodashToLower,
includes as lodashIncludes,
+ sortBy as lodashSortBy,
+ indexOf as lodashIndexOf,
} from 'lodash';
+import { createSelector } from 'reselect';
import {
getTitles,
} from '../layers/selectors';
+import buildLayerFacetProps from './formatConfig';
import { availableAtDate } from '../layers/util';
const facets = {};
@@ -180,20 +184,54 @@ function getOnSearch(config, projection) {
};
}
-export default function getSearchConfig(layers, config, projection) {
- initialLayersArray = layers;
- layers.forEach((layer) => {
- facetFields.forEach((facetField) => {
- updateFacetCounts(facetField, layer);
+function decodeHtml(html) {
+ const txt = document.createElement('textarea');
+ txt.innerHTML = html;
+ return txt.value;
+}
+
+const getConfig = (state) => state.config;
+const getProjection = (state) => state.proj && state.proj.id;
+const getFacetLayers = (state) => buildLayerFacetProps(state.config);
+
+const getLayersForProjection = createSelector(
+ [getConfig, getProjection, getFacetLayers],
+ (config, projection, layers) => {
+ const filteredRows = layers
+ // Only use the layers for the active projection
+ .filter((layer) => layer.projections[projection])
+ .map((layer) => {
+ // If there is metadata for the current projection, use that
+ const projectionMeta = layer.projections[projection];
+ if (projectionMeta.title) layer.title = projectionMeta.title;
+ if (projectionMeta.subtitle) layer.subtitle = projectionMeta.subtitle;
+ // Decode HTML entities in the subtitle
+ if (layer.subtitle) layer.subtitle = decodeHtml(layer.subtitle);
+ return layer;
+ });
+ return lodashSortBy(filteredRows, (layer) => lodashIndexOf(config.layerOrder, layer.id));
+ },
+);
+
+// eslint-disable-next-line import/prefer-default-export
+export const getSearchConfig = createSelector(
+ [getLayersForProjection, getConfig, getProjection],
+ (layers, config, projection) => {
+
+ initialLayersArray = layers;
+ layers.forEach((layer) => {
+ facetFields.forEach((facetField) => {
+ updateFacetCounts(facetField, layer);
+ });
});
- });
- return {
- // debug: true, // TODO disable for prod
- alwaysSearchOnInitialLoad: true,
- trackUrlState: false,
- initialState,
- onSearch: getOnSearch(config, projection),
- searchQuery: {},
- };
-}
+ return {
+ // debug: true, // TODO disable for prod
+ alwaysSearchOnInitialLoad: true,
+ trackUrlState: false,
+ initialState,
+ onSearch: getOnSearch(config, projection),
+ searchQuery: {},
+ };
+ },
+);
diff --git a/web/js/modules/product-picker/selectors.js b/web/js/modules/product-picker/selectors.js
new file mode 100644
index 000000000..79334409a
--- /dev/null
+++ b/web/js/modules/product-picker/selectors.js
@@ -0,0 +1,57 @@
+import {
+ sortBy as lodashSortBy,
+ indexOf as lodashIndexOf,
+} from 'lodash';
+import { createSelector } from 'reselect';
+import buildLayerFacetProps from './formatConfig';
+
+const decodeHtml = (html) => {
+ const txt = document.createElement('textarea');
+ txt.innerHTML = html;
+ return txt.value;
+};
+
+const getConfig = (state) => state.config;
+const getProjection = (state) => state.proj && state.proj.id;
+const getFacetLayers = (state) => buildLayerFacetProps(state.config);
+
+const getLayersForProjection = createSelector(
+ [getConfig, getProjection, getFacetLayers],
+ (config, projection, layers) => {
+ const filteredRows = layers
+ // Only use the layers for the active projection
+ .filter((layer) => layer.projections[projection])
+ .map((layer) => {
+ // If there is metadata for the current projection, use that
+ const projectionMeta = layer.projections[projection];
+ if (projectionMeta.title) layer.title = projectionMeta.title;
+ if (projectionMeta.subtitle) layer.subtitle = projectionMeta.subtitle;
+ // Decode HTML entities in the subtitle
+ if (layer.subtitle) layer.subtitle = decodeHtml(layer.subtitle);
+ return layer;
+ });
+ return lodashSortBy(filteredRows, (layer) => lodashIndexOf(config.layerOrder, layer.id));
+ },
+);
+
+// eslint-disable-next-line import/prefer-default-export
+export const getSearchConfig = createSelector(
+ [getLayersForProjection, getConfig, getProjection],
+ (layers, config, projection) => {
+ initialLayersArray = layers;
+ layers.forEach((layer) => {
+ facetFields.forEach((facetField) => {
+ updateFacetCounts(facetField, layer);
+ });
+ });
+
+ return {
+ // debug: true, // TODO disable for prod
+ alwaysSearchOnInitialLoad: true,
+ trackUrlState: false,
+ initialState,
+ onSearch: getOnSearch(config, projection),
+ searchQuery: {},
+ };
+ },
+);