Interactive interface for browsing global, full-resolution satellite imagery
basic recent layers list capability
Jason Kentf8eb92be7071
diff --git a/web/js/components/layer/product-picker/browse/browse-layers.js b/web/js/components/layer/product-picker/browse/browse-layers.jsindex e4273523c..b58f80152 100644--- a/web/js/components/layer/product-picker/browse/browse-layers.js+++ b/web/js/components/layer/product-picker/browse/browse-layers.js@@ -12,7 +12,9 @@ import MeasurementMetadataDetail from './measurement-metadata-detail';import {selectCategory as selectCategoryAction,toggleFeatureTab as toggleFeatureTabAction,+ toggleRecentLayersTab as toggleRecentLayersTabAction,} from '../../../../modules/product-picker/actions';+import RecentLayersList from './recent-layers';/** A scrollable list of layers@@ -32,9 +34,15 @@ class BrowseLayers extends React.Component {* @param {String} key | categoryType identifier*/selectCategoryType(key) {- const { selectCategory, toggleFeatureTab } = this.props;+ const {+ selectCategory,+ toggleFeatureTab,+ toggleRecentLayersTab,+ } = this.props;if (key === 'featured') {toggleFeatureTab();+ } else if (key === 'recent') {+ toggleRecentLayersTab();} else {selectCategory(key);}@@ -64,13 +72,19 @@ class BrowseLayers extends React.Component {width,categoryType,mode,+ layerConfig,} = this.props;- const isCategoryDisplay = mode === 'category' && selectedProjection === 'geographic';- const showCategoryTabs = isCategoryDisplay || categoryType === 'featured';+ const isCategoryDisplay = mode === 'category'+ && selectedProjection === 'geographic'+ && categoryType !== 'recent';+ const showCategoryTabs = isCategoryDisplay+ || categoryType === 'featured'+ || categoryType === 'recent';const categoryKeys = ['hazards and disasters','scientific','featured',+ 'recent',];return (@@ -90,11 +104,19 @@ class BrowseLayers extends React.Component {</NavItem>))}</Nav>- {isCategoryDisplay ? (+++ {// eslint-disable-next-line no-nested-ternary+ isCategoryDisplay ? (<div className="product-outter-list-case"><CategoryGrid width={width} /></div>- ) : this.renderLayerList()}+ ) : categoryType === 'recent'+ ? (+ <RecentLayersList proj={selectedProjection} layerConfig={layerConfig} />+ )+ : this.renderLayerList()+ }</>): this.renderLayerList()@@ -105,10 +127,12 @@ class BrowseLayers extends React.Component {BrowseLayers.propTypes = {browser: PropTypes.object,categoryType: PropTypes.string,+ layerConfig: PropTypes.object,mode: PropTypes.string,selectCategory: PropTypes.func,selectedProjection: PropTypes.string,toggleFeatureTab: PropTypes.func,+ toggleRecentLayersTab: PropTypes.func,width: PropTypes.number,};@@ -119,6 +143,9 @@ const mapDispatchToProps = (dispatch) => ({toggleFeatureTab: () => {dispatch(toggleFeatureTabAction());},+ toggleRecentLayersTab: () => {+ dispatch(toggleRecentLayersTabAction());+ },});function mapStateToProps(state, ownProps) {@@ -127,6 +154,7 @@ function mapStateToProps(state, ownProps) {browser,proj,productPicker,+ layers,} = state;const {mode,@@ -142,6 +170,7 @@ function mapStateToProps(state, ownProps) {categoryType,categoryConfig: config.categories,measurementConfig: config.measurements,+ layerConfig: layers.layerConfig,listScrollTop,selectedProjection: proj.id,selectedMeasurement,diff --git a/web/js/components/layer/product-picker/browse/recent-layers.js b/web/js/components/layer/product-picker/browse/recent-layers.jsnew file mode 100644index 000000000..a3c4e0b6c--- /dev/null+++ b/web/js/components/layer/product-picker/browse/recent-layers.js@@ -0,0 +1,36 @@+import React from 'react';+import PropTypes from 'prop-types';+import safeLocalStorage from '../../../../util/local-storage';+import SearchLayerList from '../search/search-layers-list';++const RecentLayersList = ({ proj, layerConfig }) => {+ const { RECENT_LAYERS } = safeLocalStorage.keys;+ const recentLayers = JSON.parse(+ safeLocalStorage.getItem(RECENT_LAYERS),+ );+ const sortFn = (a, b) => {+ if (a.count > b.count) return -1;+ if (a.count < b.count) return 1;+ if (a.dateAdded > b.dateAdded) return 1;+ if (a.dateAdded < b.dateAdded) return -1;+ };++ const results = recentLayers[proj]+ .sort(sortFn)+ .map(({ id }) => layerConfig[id]);++ console.table(recentLayers[proj].sort(sortFn));++ return (+ <div>+ <SearchLayerList results={results} />+ </div>+ );+};++RecentLayersList.propTypes = {+ proj: PropTypes.string,+ layerConfig: PropTypes.object,+};++export default RecentLayersList;diff --git a/web/js/modules/layers/actions.js b/web/js/modules/layers/actions.jsindex 91dbceee1..d994cfd43 100644--- a/web/js/modules/layers/actions.js+++ b/web/js/modules/layers/actions.js@@ -7,7 +7,6 @@ import {getLayers as getLayersSelector,activateLayersForEventCategory as activateLayersForEventCategorySelector,} from './selectors';-import updateRecentLayers from './util';import {RESET_LAYERS,ADD_LAYER,@@ -20,6 +19,7 @@ import {ADD_LAYERS_FOR_EVENT,} from './constants';import { selectProduct } from '../data/actions';+import { updateRecentLayers } from './util';export function resetLayers(activeString) {return (dispatch, getState) => {@@ -36,6 +36,7 @@ export function resetLayers(activeString) {});};}+export function activateLayersForEventCategory(activeLayers) {return (dispatch, getState) => {const state = getState();@@ -51,18 +52,18 @@ export function activateLayersForEventCategory(activeLayers) {});};}-export function addLayer(id, spec) {- spec = spec || {};++export function addLayer(id, spec = {}) {googleTagManager.pushEvent({event: 'layer_added',layers: { id },});- updateRecentLayers(id);return (dispatch, getState) => {const state = getState();const { layers, compare, proj } = state;const { activeString } = compare;- const layerObj = getLayersSelector(+ const layerObj = layers.layerConfig[id];+ const activeLayers = getLayersSelector(layers[activeString],{ group: 'all' },state,@@ -72,10 +73,10 @@ export function addLayer(id, spec) {spec,layers[activeString],layers.layerConfig,- layerObj.overlays.length || 0,+ activeLayers.overlays.length || 0,proj.id,);-+ updateRecentLayers(layerObj);dispatch({type: ADD_LAYER,id,@@ -84,21 +85,25 @@ export function addLayer(id, spec) {});};}+export function clearGraticule() {return (dispatch) => {dispatch(toggleVisibility('Graticule', false));};}+export function refreshGraticule() {return (dispatch) => {dispatch(toggleVisibility('Graticule', true));};}+export function initSecondLayerGroup() {return {type: INIT_SECOND_LAYER_GROUP,};}+export function reorderLayers(layerArray) {return {type: REORDER_LAYER_GROUP,@@ -113,6 +118,7 @@ export function layerHover(id, isMouseOver) {active: isMouseOver,};}+export function toggleVisibility(id, visible) {return (dispatch, getState) => {const { layers, compare } = getState();@@ -130,6 +136,7 @@ export function toggleVisibility(id, visible) {});};}+export function removeLayer(id) {return (dispatch, getState) => {const {@@ -157,6 +164,7 @@ export function removeLayer(id) {});};}+export function setOpacity(id, opacity) {return (dispatch, getState) => {const { layers, compare } = getState();diff --git a/web/js/modules/layers/util.js b/web/js/modules/layers/util.jsindex 6e4d80406..8b2e96297 100644--- a/web/js/modules/layers/util.js+++ b/web/js/modules/layers/util.js@@ -1251,15 +1251,44 @@ export const hasNonClickableVectorLayer = (activeLayers, mapRes) => {return isNonClickableVectorLayer;};-// safeLocalStorage.removeItem(lsKey);-export const updateRecentLayers = (id) => {- const { recentLayers: key } = safeLocalStorage.keys;- const recentLayersJson = safeLocalStorage.getItem(key);- let recentLayers = JSON.parse(recentLayersJson) || [];- if (recentLayers.length >= 10) {- recentLayers = recentLayers.slice(1, 10);- }- recentLayers.push(id);- console.table(recentLayers);- safeLocalStorage.setItem(key, JSON.stringify(recentLayers));-};+export function updateRecentLayers({ id: layerId, projections }) {+ const MAX_RECENT_LAYERS = 10;+ const { RECENT_LAYERS } = safeLocalStorage.keys;+ const defaultObj = {+ geographic: [],+ arctic: [],+ antarctic: [],+ };+ const recentLayersJson = safeLocalStorage.getItem(RECENT_LAYERS);+ const recentLayers = JSON.parse(recentLayersJson) || defaultObj;++ Object.keys(projections).forEach((proj) => {+ const layers = recentLayers[proj];+ const existingEntry = layers.find(({ id }) => id === layerId);++ if (existingEntry) {+ existingEntry.count += 1;+ existingEntry.dateAdded = new Date().valueOf();+ } else {+ if (layers.length === MAX_RECENT_LAYERS) {+ // - Determine lowest count+ // - Collect all layers that match lowest count+ // - Remove oldest layer from that group+ const [lowestCountLayer] = layers.sort((a, b) => a.count - b.count);+ const filteredByCount = layers.filter(+ ({ count }) => count === lowestCountLayer.count,+ );+ const [oldestLowest] = filteredByCount.sort(+ (a, b) => a.dateAdded.valueOf() - b.dateAdded.valueOf(),+ );+ recentLayers[proj] = layers.filter(({ id }) => id !== oldestLowest.id);+ }+ recentLayers[proj].push({+ id: layerId,+ count: 1,+ dateAdded: new Date().valueOf(),+ });+ }+ });+ safeLocalStorage.setItem(RECENT_LAYERS, JSON.stringify(recentLayers));+}diff --git a/web/js/modules/product-picker/actions.js b/web/js/modules/product-picker/actions.jsindex 238768940..3d7286262 100644--- a/web/js/modules/product-picker/actions.js+++ b/web/js/modules/product-picker/actions.js@@ -9,6 +9,7 @@ import {SELECT_LAYER,SHOW_MEASUREMENTS,TOGGLE_FEATURED_TAB,+ TOGGLE_RECENT_LAYERS,TOGGLE_SEARCH_MODE,TOGGLE_CATEGORY_MODE,TOGGLE_MOBILE_FACETS,@@ -74,6 +75,11 @@ export function toggleFeatureTab() {});};}+export function toggleRecentLayersTab() {+ return {+ type: TOGGLE_RECENT_LAYERS,+ };+}export function toggleSearchMode() {return {type: TOGGLE_SEARCH_MODE,diff --git a/web/js/modules/product-picker/constants.js b/web/js/modules/product-picker/constants.jsindex aed66a991..2e55af307 100644--- a/web/js/modules/product-picker/constants.js+++ b/web/js/modules/product-picker/constants.js@@ -9,5 +9,6 @@ export const SHOW_MEASUREMENTS = 'PRODUCT_PICKER/SHOW_MEASUREMENTS';export const TOGGLE_SEARCH_MODE = 'PRODUCT_PICKER/TOGGLE_SEARCH_MODE';export const TOGGLE_CATEGORY_MODE = 'PRODUCT_PICKER/TOGGLE_CATEGORY_MODE';export const TOGGLE_FEATURED_TAB = 'PRODUCT_PICKER/TOGGLE_FEATURED_TAB';+export const TOGGLE_RECENT_LAYERS = 'PRODUCT_PICKER/TOGGLE_RECENT_LAYERS';export const TOGGLE_MOBILE_FACETS = 'PRODUCT_PICKE/TOGGLE_MOBILE_FACETS';export const RESET_STATE = 'PRODUCT_PICKER/RESET_STATE';diff --git a/web/js/modules/product-picker/reducers.js b/web/js/modules/product-picker/reducers.jsindex 6cb0b7182..30e0e20ee 100644--- a/web/js/modules/product-picker/reducers.js+++ b/web/js/modules/product-picker/reducers.js@@ -13,6 +13,7 @@ import {SELECT_LAYER,SHOW_MEASUREMENTS,TOGGLE_FEATURED_TAB,+ TOGGLE_RECENT_LAYERS,TOGGLE_SEARCH_MODE,TOGGLE_CATEGORY_MODE,TOGGLE_MOBILE_FACETS,@@ -148,6 +149,13 @@ export function productPickerReducer(state = productPickerState, action) {};}+ case TOGGLE_RECENT_LAYERS: {+ return {+ ...state,+ categoryType: 'recent',+ };+ }+case TOGGLE_CATEGORY_MODE: {return {...state,diff --git a/web/js/util/local-storage.js b/web/js/util/local-storage.jsindex aa59cbc12..de6340bfd 100644--- a/web/js/util/local-storage.js+++ b/web/js/util/local-storage.js@@ -18,7 +18,7 @@ const enabled = (function() {export default {enabled,keys: {- RECENT_LAYERS: 'recentlyAddedLayers',+ RECENT_LAYERS: 'recentLayers',DISMISSED_EVENT_VIS_ALERT: 'dismissedEventVisibilityAlert',DISMISSED_COMPARE_ALERT: 'dismissedCompareAlert',HIDE_TOUR: 'hideTour',@@ -29,9 +29,7 @@ export default {NOTIFICATION_MSG: 'message',},getItem(key) {- if (enabled) {- return localStorage.getItem(key);- }+ return enabled && localStorage.getItem(key);},setItem(key, value) {if (enabled) {