Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

better separation of selectors and searchConfig

Jason Kent01ed6b3988ce
diff --git a/web/js/components/layer/product-picker/product-picker.js b/web/js/components/layer/product-picker/product-picker.js
index 2a6ec1c8c..b4c9fa5ad 100644
--- a/web/js/components/layer/product-picker/product-picker.js
+++ b/web/js/components/layer/product-picker/product-picker.js
@@ -12,17 +12,13 @@ import {
} from '@elastic/react-search-ui';
import ProductPickerHeader from './header';
import FilterUnavailable from './filterUnavailable';
-import {
- getLayersForProjection,
-} from '../../../modules/layers/selectors';
import { onToggle } from '../../../modules/modal/actions';
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/selectors';
/*
* A scrollable list of layers
@@ -289,7 +285,7 @@ ProductPicker.propTypes = {
allLayers: PropTypes.array,
category: PropTypes.object,
categoryType: PropTypes.string,
- config: PropTypes.object,
+ searchConfig: PropTypes.object,
filterByAvailable: PropTypes.bool,
inputValue: PropTypes.string,
isMobile: PropTypes.bool,
@@ -332,7 +328,6 @@ function mapStateToProps(state, ownProps) {
return {
...productPicker,
- config,
selectedDate: date.selected,
isMobile,
screenHeight,
diff --git a/web/js/modules/product-picker/searchConfig.js b/web/js/modules/product-picker/searchConfig.js
index 112744b46..9ca2986b9 100644
--- a/web/js/modules/product-picker/searchConfig.js
+++ b/web/js/modules/product-picker/searchConfig.js
@@ -4,30 +4,15 @@ 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 = {};
let initialLayersArray;
let configRef;
let projectionRef;
-const initialState = {
- filters: [
- // {
- // field: "facetPeriod",
- // values: ["Multi-Day"],
- // type: "any"
- // }
- ],
- resultsPerPage: Infinity,
-};
const hideZeroCountFields = [
'measurements',
@@ -168,70 +153,35 @@ function filterSearch (layer, val, terms) {
return filtered;
}
-function getOnSearch(config, projection) {
- configRef = config;
- projectionRef = projection;
- return async (requestState, queryConfig) => {
- const { filters, searchTerm } = requestState;
- const results = layersMatchSearchAndFilters(filters, searchTerm);
- updateAllFacetCounts(filters, searchTerm);
-
- return {
- facets: formatFacets(facets),
- results,
- totalResults: results.length,
- };
+/**
+ *
+ * @param {Object} requestState
+ * - https://github.com/elastic/search-ui/blob/master/ADVANCED.md#request-state
+ * @returns {Object} responseState
+ * - https://github.com/elastic/search-ui/blob/master/ADVANCED.md#response-state
+ */
+async function onSearch (requestState) {
+ const { filters, searchTerm } = requestState;
+ const results = layersMatchSearchAndFilters(filters, searchTerm);
+ updateAllFacetCounts(filters, searchTerm);
+
+ return {
+ facets: formatFacets(facets),
+ results,
+ totalResults: results.length,
};
}
-function decodeHtml(html) {
- const txt = document.createElement('textarea');
- txt.innerHTML = html;
- return txt.value;
-}
+export default function initSearch(layers, config, projection) {
+ configRef = config;
+ projectionRef = projection;
+ initialLayersArray = layers;
-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);
- });
+ 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 onSearch;
+}
diff --git a/web/js/modules/product-picker/selectors.js b/web/js/modules/product-picker/selectors.js
index 79334409a..eebb82966 100644
--- a/web/js/modules/product-picker/selectors.js
+++ b/web/js/modules/product-picker/selectors.js
@@ -4,6 +4,7 @@ import {
} from 'lodash';
import { createSelector } from 'reselect';
import buildLayerFacetProps from './formatConfig';
+import initSearch from './searchConfig';
const decodeHtml = (html) => {
const txt = document.createElement('textarea');
@@ -13,12 +14,11 @@ const decodeHtml = (html) => {
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
+ [getConfig, getProjection],
+ (config, projection) => {
+ const filteredRows = buildLayerFacetProps(config)
// Only use the layers for the active projection
.filter((layer) => layer.projections[projection])
.map((layer) => {
@@ -27,31 +27,28 @@ const getLayersForProjection = createSelector(
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);
+ if (layer.subtitle.includes('&')) {
+ layer.subtitle = decodeHtml(layer.subtitle);
+ }
return layer;
});
return lodashSortBy(filteredRows, (layer) => lodashIndexOf(config.layerOrder, layer.id));
},
);
+/**
+ * Returns a SearchProvider configuration object.
+ * https://github.com/elastic/search-ui/blob/master/ADVANCED.md#advanced-configuration
+ */
// 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: {},
- };
- },
+ (layers, config, projection) => ({
+ // debug: true,
+ alwaysSearchOnInitialLoad: true,
+ trackUrlState: false,
+ initialState: {},
+ onSearch: initSearch(layers, config, projection),
+ searchQuery: {},
+ }),
);