Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

implement basic data-download sidebar template in redux WIP #1623

Benjamin King84b4e08edc86
diff --git a/web/js/components/sidebar/data/data.js b/web/js/components/sidebar/data/data.js
index 794324270..914589c75 100644
--- a/web/js/components/sidebar/data/data.js
+++ b/web/js/components/sidebar/data/data.js
@@ -2,22 +2,29 @@ import React from 'react';
import PropTypes from 'prop-types';
import Product from './product';
import Scrollbars from '../../util/scrollbar';
+import { connect } from 'react-redux';
+import {
+ getSelectionCounts,
+ getDataProductsFromActiveLayers,
+ findAvailableProduct
+} from '../../../modules/data/selectors';
+import { selectProduct } from '../../../modules/data/actions';
class Data extends React.Component {
render() {
const {
height,
- data,
- getCounts,
- selected,
+ findProductToSelect,
+ counts,
+ selectedProduct,
selectProduct,
+ products,
showUnavailableReason,
tabTypes
} = this.props;
if (!tabTypes.download) return null;
- const dataArray = Object.entries(data);
- const counts = getCounts();
-
+ const dataArray = Object.entries(products);
+ if (dataArray.length > 0 && !selectedProduct) findProductToSelect(products);
return (
<Scrollbars style={{ maxHeight: height + 'px' }}>
<div id="wv-data">
@@ -30,7 +37,7 @@ class Data extends React.Component {
id={product[0]}
productObject={product[1]}
countsObject={counts}
- isSelected={selected === product[0]}
+ isSelected={selectedProduct === product[0]}
selectProduct={selectProduct}
showUnavailableReason={showUnavailableReason}
/>
@@ -52,5 +59,30 @@ Data.propTypes = {
selectProduct: PropTypes.func,
tabTypes: PropTypes.object
};
-
-export default Data;
+const mapDispatchToProps = dispatch => ({
+ findProductToSelect: products => {
+ const newSelection = findAvailableProduct(products);
+ if (newSelection) dispatch(selectProduct(newSelection));
+ },
+ selectProduct: id => {
+ dispatch(selectProduct(id));
+ }
+});
+function mapStateToProps(state, ownProps) {
+ const { tabTypes } = ownProps;
+ const { layers, proj, data, config } = state;
+ const { selectedProduct, selectedGranules } = data;
+ const { activeString } = layers;
+ const activeLayers = layers['layers' + activeString];
+ const counts = getSelectionCounts(activeLayers, selectedGranules);
+ const products = getDataProductsFromActiveLayers(
+ activeLayers,
+ config,
+ proj.id
+ );
+ return { counts, selectedProduct, products, tabTypes };
+}
+export default connect(
+ mapStateToProps,
+ mapDispatchToProps
+)(Data);
diff --git a/web/js/components/sidebar/footer-content.js b/web/js/components/sidebar/footer-content.js
index 2ef371c17..1f1d83a0a 100644
--- a/web/js/components/sidebar/footer-content.js
+++ b/web/js/components/sidebar/footer-content.js
@@ -6,6 +6,10 @@ import googleTagManager from 'googleTagManager';
import { get as lodashGet } from 'lodash';
import { changeMode } from '../../modules/compare/actions';
import { connect } from 'react-redux';
+import {
+ getSelectionCounts,
+ getDataSelectionSize
+} from '../../modules/data/selectors';
class FooterContent extends React.Component {
render() {
@@ -21,8 +25,8 @@ class FooterContent extends React.Component {
changeCompareMode,
addLayers,
toggleMode,
- // getDataSelectionCounts,
- // getDataSelectionSize,
+ counts,
+ dataSelectionSize,
compareFeature
} = this.props;
if (activeTab === 'layers') {
@@ -89,39 +93,39 @@ class FooterContent extends React.Component {
</div>
);
} else {
- // var counts = getDataSelectionCounts();
- // var validSize = getDataSelectionSize();
- // var countArray = Object.values(counts);
- // var noDataSelected =
- // countArray.length === 0
- // ? true
- // : countArray.reduce((a, b) => a + b, 0) === 0;
- // return (
- // <div className="data-download-footer-case">
- // <Button
- // onClick={e => {
- // e.stopPropagation();
- // onGetData();
- // googleTagManager.pushEvent({
- // event: 'data_download_button'
- // });
- // }}
- // className={
- // noDataSelected
- // ? 'wv-data-download-button black no-pointer-events'
- // : 'wv-data-download-button red'
- // }
- // id="compare-toggle-button"
- // text={
- // validSize
- // ? 'Download Data (' + Math.round(validSize * 100) / 100 + ' MB)'
- // : noDataSelected
- // ? 'No Data Selected'
- // : 'Download Selected Data'
- // }
- // />
- // </div>
- // );
+ var countArray = Object.values(counts);
+ var noDataSelected =
+ countArray.length === 0
+ ? true
+ : countArray.reduce((a, b) => a + b, 0) === 0;
+ return (
+ <div className="data-download-footer-case">
+ <Button
+ onClick={e => {
+ e.stopPropagation();
+ onGetData();
+ googleTagManager.pushEvent({
+ event: 'data_download_button'
+ });
+ }}
+ className={
+ noDataSelected
+ ? 'wv-data-download-button black no-pointer-events'
+ : 'wv-data-download-button red'
+ }
+ id="compare-toggle-button"
+ text={
+ dataSelectionSize
+ ? 'Download Data (' +
+ Math.round(dataSelectionSize * 100) / 100 +
+ ' MB)'
+ : noDataSelected
+ ? 'No Data Selected'
+ : 'Download Selected Data'
+ }
+ />
+ </div>
+ );
}
}
}
@@ -132,14 +136,22 @@ const mapDispatchToProps = dispatch => ({
});
function mapStateToProps(state, ownProps) {
const { activeTab } = ownProps;
- const { requestedEvents, config } = state;
+ const { requestedEvents, config, layers, data } = state;
const { showAll } = state.events;
const showListAllButton = !showAll;
const events = lodashGet(requestedEvents, 'response');
+ const { activeString } = layers;
+ const { selectedGranules } = data;
+ const activeLayers = layers['layers' + activeString];
+ const counts = getSelectionCounts(activeLayers, selectedGranules);
+ const dataSelectionSize = getDataSelectionSize(selectedGranules);
+
return {
showListAllButton,
activeTab,
events,
+ counts,
+ dataSelectionSize,
compareFeature: config.features.compare
};
}
@@ -160,7 +172,7 @@ FooterContent.propTypes = {
changeCompareMode: PropTypes.func,
addLayers: PropTypes.func,
toggleMode: PropTypes.func,
- getDataSelectionCounts: PropTypes.func,
- getDataSelectionSize: PropTypes.func,
+ counts: PropTypes.obj,
+ dataSelectionSize: PropTypes.num,
compareFeature: PropTypes.bool
};
diff --git a/web/js/containers/sidebar.js b/web/js/containers/sidebar.js
index 67fd6016c..7890bd774 100644
--- a/web/js/containers/sidebar.js
+++ b/web/js/containers/sidebar.js
@@ -176,12 +176,13 @@ class Sidebar extends React.Component {
height={subComponentHeight}
/>
</TabPane>
- {/* <TabPane tabId="download">
+ <TabPane tabId="download">
<Data
isActive={activeTab === 'download'}
height={subComponentHeight}
+ tabTypes={tabTypes}
/>
- </TabPane> */}
+ </TabPane>
<footer ref={footerElement => (this.footerElement = footerElement)}>
<FooterContent tabTypes={tabTypes} activeTab={activeTab} />
</footer>
@@ -198,7 +199,7 @@ function mapStateToProps(state) {
const { activeString } = layers;
const numberOfLayers = getLayers(layers[activeString], {}, state).length;
const tabTypes = getActiveTabs(config);
- console.log(browser);
+
return {
activeTab,
isMobile: browser.is.small,
diff --git a/web/js/modules/combine-reducers.js b/web/js/modules/combine-reducers.js
index b0ed1a3db..1c58c461d 100644
--- a/web/js/modules/combine-reducers.js
+++ b/web/js/modules/combine-reducers.js
@@ -23,6 +23,7 @@ import {
layerReducer,
getInitialState as getLayersInitialState
} from './layers/reducers';
+import dataDownloadReducer from './data/reducers';
import { get as lodashGet } from 'lodash';
const responsiveStateReducer = createResponsiveStateReducer(
@@ -72,6 +73,7 @@ const reducers = {
compare: compareReducer,
layers: layerReducer,
events: eventsReducer,
+ data: dataDownloadReducer,
requestedEvents,
requestedEventSources,
requestedEventCategories,
diff --git a/web/js/modules/data/actions.js b/web/js/modules/data/actions.js
new file mode 100644
index 000000000..438042402
--- /dev/null
+++ b/web/js/modules/data/actions.js
@@ -0,0 +1,7 @@
+import { SELECT_PRODUCT } from './constants';
+export function selectProduct(id) {
+ return {
+ type: SELECT_PRODUCT,
+ id: id
+ };
+}
diff --git a/web/js/modules/data/constants.js b/web/js/modules/data/constants.js
new file mode 100644
index 000000000..e69c5a6c4
--- /dev/null
+++ b/web/js/modules/data/constants.js
@@ -0,0 +1 @@
+export const SELECT_PRODUCT = 'DATA/SELECT_PRODUCT';
diff --git a/web/js/modules/data/reducers.js b/web/js/modules/data/reducers.js
new file mode 100644
index 000000000..d6aea6761
--- /dev/null
+++ b/web/js/modules/data/reducers.js
@@ -0,0 +1,16 @@
+import { SELECT_PRODUCT } from './constants';
+import { assign as lodashAssign } from 'lodash';
+const initDataState = {
+ selectedProduct: '',
+ selectedGranules: {}
+};
+export default function dataDownloadReducer(state = initDataState, action) {
+ switch (action.type) {
+ case SELECT_PRODUCT:
+ return lodashAssign({}, state, {
+ selectedProduct: action.id
+ });
+ default:
+ return state;
+ }
+}
diff --git a/web/js/modules/data/selectors.js b/web/js/modules/data/selectors.js
new file mode 100644
index 000000000..44ddb79b7
--- /dev/null
+++ b/web/js/modules/data/selectors.js
@@ -0,0 +1,118 @@
+import util from '../../util/util';
+import { each as lodashEach } from 'lodash';
+import { getTitles } from '../layers/selectors';
+const NO_PRODUCT_ID = '__NO_PRODUCT';
+const NO_PRODUCT = {
+ name: "Not available for download &nbsp;&nbsp;<span class='link'>(?)</span>",
+ notSelectable: true
+};
+
+// For each product displayed in the sidebar, gets the count of
+// granules that have been selected by the user. Returns an object
+// in the form of product_id = count.
+export function getSelectionCounts(layers, selectedGranules) {
+ var counts = {};
+ for (let layer of layers) {
+ if (layer.product) {
+ let products = util.toArray(layer.product);
+ for (let product of products) {
+ counts[product] = 0;
+ }
+ }
+ }
+ for (let granule of Object.values(selectedGranules)) {
+ counts[granule.product]++;
+ }
+ return counts;
+}
+export function getDataSelectionSize(selectedGranules) {
+ var totalSize = 0;
+ var sizeValid = true;
+ lodashEach(selectedGranules, function(index, granule) {
+ if (sizeValid && granule.granule_size) {
+ totalSize += parseFloat(granule.granule_size);
+ } else {
+ sizeValid = false;
+ }
+ });
+ if (sizeValid) {
+ return totalSize;
+ }
+}
+export function groupByProducts(config, dataProducts) {
+ let products = {};
+ for (let layer of dataProducts) {
+ // If products is a list, use it. If a single item, convert to a list.
+ // If no products are defined, add it to the no product group.
+ let productIds = util.toArray(layer.product);
+ if (productIds.length === 0) {
+ productIds = [NO_PRODUCT_ID];
+ }
+ for (let productId of productIds) {
+ var product = config.products[productId] || NO_PRODUCT;
+ if (!products[productId]) {
+ products[productId] = {
+ title: product.name,
+ items: [],
+ notSelectable: product.notSelectable
+ };
+ }
+ products[productId].items.push({
+ label: layer.name,
+ sublabel: layer.description,
+ value: layer.id,
+ categories: {
+ All: 1
+ }
+ });
+ }
+ }
+
+ // FIXME: This is a hack to force the not availables to the bottom
+ // especially for IE9. This whole function needs clean up.
+ var results = {};
+ var none = products.__NO_PRODUCT;
+ lodashEach(products, function(product, key) {
+ if (key !== NO_PRODUCT_ID) {
+ results[key] = product;
+ }
+ });
+ if (none) {
+ results[NO_PRODUCT_ID] = none;
+ }
+ return results;
+}
+export function getDataProductsFromActiveLayers(layers, config, projId) {
+ let dataProducts = [];
+ lodashEach(layers, function(layer) {
+ var id = layer.id;
+ var names = getTitles(config, layer.id, projId);
+ var layerName = names.title;
+ var description = names.subtitle;
+ var productName = layer.product;
+ dataProducts.push({
+ id: id,
+ name: layerName,
+ description: description,
+ product: productName
+ });
+ });
+ var products = groupByProducts(config, dataProducts);
+ return products;
+}
+
+export function findAvailableProduct(layers) {
+ var foundProduct = null;
+
+ // Find the top most layer that has a product entry in CMR
+ for (var i = layers.length - 1; i >= 0; i--) {
+ if (layers[i].product) {
+ foundProduct = layers[i].product;
+ // If the layer has more than one product, select the first.
+ if (Array.isArray(foundProduct)) {
+ foundProduct = foundProduct[0];
+ }
+ }
+ }
+ return foundProduct;
+}
diff --git a/web/js/modules/layers/selectors.js b/web/js/modules/layers/selectors.js
index 9f271434f..d9bd06f37 100644
--- a/web/js/modules/layers/selectors.js
+++ b/web/js/modules/layers/selectors.js
@@ -62,11 +62,13 @@ export function resetLayers(config) {
}
return layers;
}
-export function getTitles(config, layerId, proj, state) {
+export function getTitles(config, layerId, projId) {
try {
- proj = proj || state.proj.id;
var title, subtitle, tags;
- const forProj = lodashGet(config, `layers.${layerId}.projections.${proj}`);
+ const forProj = lodashGet(
+ config,
+ `layers.${layerId}.projections.${projId}`
+ );
if (forProj) {
title = forProj.title;
subtitle = forProj.subtitle;