Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

server side extent filtering

Jason Kent0797b1af04cc
diff --git a/web/js/containers/sidebar/sidebar.js b/web/js/containers/sidebar/sidebar.js
index fc61e7c41..4fc2729ef 100644
--- a/web/js/containers/sidebar/sidebar.js
+++ b/web/js/containers/sidebar/sidebar.js
@@ -1,7 +1,10 @@
import React from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
-import { get as lodashGet } from 'lodash';
+import {
+ get as lodashGet,
+ isEqual as lodashEqual,
+} from 'lodash';
import { TabContent, TabPane } from 'reactstrap';
import googleTagManager from 'googleTagManager';
import LayersContainer from './layers-container';
@@ -88,30 +91,35 @@ class Sidebar extends React.Component {
componentDidUpdate(prevProps) {
const { activeTab, proj } = this.props;
if (activeTab === 'events') {
- const projChange = proj !== prevProps.proj;
+ const projChange = !lodashEqual(proj, prevProps.proj);
this.loadEvents(projChange);
}
this.updateDimensions();
}
- loadEvents(projChanged) {
+ loadEvents(shouldUpdate) {
const {
isLoadingEvents,
hasEventRequestError,
eventsData,
config,
proj,
+ bbox,
requestEvents,
requestSources,
- selectedStartDate,
- selectedEndDate,
+ selectedDates,
selectedCategories,
} = this.props;
+ const initialLoad = !isLoadingEvents && !hasEventRequestError && !eventsData;
+ if (!initialLoad && !shouldUpdate) {
+ return;
+ }
+
const baseUrl = lodashGet(config, 'features.naturalEvents.host');
const mockEvents = lodashGet(config, 'parameters.mockEvents');
const mockSources = lodashGet(config, 'parameters.mockSources');
- let eventsURL = getEventsRequestURL(baseUrl, selectedStartDate, selectedEndDate, selectedCategories, proj);
+ let eventsURL = getEventsRequestURL(baseUrl, selectedDates, selectedCategories, proj, bbox);
let sourcesURL = `${baseUrl}/sources`;
if (mockEvents) {
@@ -125,11 +133,11 @@ class Sidebar extends React.Component {
sourcesURL = `mock/categories_data.json-${mockSources}`;
}
- const initialLoad = !isLoadingEvents && !hasEventRequestError && !eventsData;
+
if (initialLoad) {
requestEvents(eventsURL);
requestSources(sourcesURL);
- } else if (projChanged) {
+ } else if (shouldUpdate) {
requestEvents(eventsURL);
}
}
@@ -374,8 +382,10 @@ const mapStateToProps = (state) => {
sidebar,
ui,
proj,
+ map,
} = state;
+ const { selectedCategories, selectedDates } = events;
const isLoadingEvents = requestedEvents.isLoading
|| requestedEventSources.isLoading;
const hasEventRequestError = !!(requestedEvents.error
@@ -398,10 +408,12 @@ const mapStateToProps = (state) => {
const isMobile = browser.lessThan.medium;
// Collapse when Image download / GIF / is open or measure tool active
const shouldBeCollapsed = snapshotModalOpen || measure.isActive || animation.gifActive;
+ const bbox = !events.showAll ? map.extent : [];
return {
activeTab,
activeString,
+ bbox,
config,
eventsData,
eventsSources,
@@ -415,9 +427,8 @@ const mapStateToProps = (state) => {
isLoadingEvents,
isMobile,
proj,
- selectedStartDate: events.selectedDates.start,
- selectedEndDate: events.selectedDates.end,
- selectedCategories: events.selectedCategories,
+ selectedDates,
+ selectedCategories,
screenHeight,
selectedDate: getSelectedDate(state),
tabTypes,
@@ -461,6 +472,7 @@ export default connect(
Sidebar.propTypes = {
activeString: PropTypes.string,
activeTab: PropTypes.string,
+ bbox: PropTypes.array,
changeTab: PropTypes.func,
collapseExpandToggle: PropTypes.func,
config: PropTypes.object,
@@ -479,8 +491,7 @@ Sidebar.propTypes = {
onTabClick: PropTypes.func,
proj: PropTypes.object,
screenHeight: PropTypes.number,
- selectedStartDate: PropTypes.string,
- selectedEndDate: PropTypes.string,
+ selectedDates: PropTypes.object,
selectedCategories: PropTypes.array,
tabTypes: PropTypes.object,
requestEvents: PropTypes.func,
diff --git a/web/js/location.js b/web/js/location.js
index 06549d1e9..b01127529 100644
--- a/web/js/location.js
+++ b/web/js/location.js
@@ -34,7 +34,7 @@ import {
mapLocationToLayerState,
} from './modules/layers/util';
import { resetLayers, hasSubDaily, getActiveLayers } from './modules/layers/selectors';
-import { eventsReducerState } from './modules/natural-events/reducers';
+import { getInitialEventsState } from './modules/natural-events/reducers';
import { mapLocationToPaletteState } from './modules/palettes/util';
import { mapLocationToEmbedState } from './modules/embed/util';
import { mapLocationToAnimationState } from './modules/animation/util';
@@ -146,6 +146,7 @@ const getParameters = function(config, parameters) {
const nowMinusSevenDays = util.dateAdd(config.pageLoadTime, 'day', -7);
const { initialDate } = config;
const startingLayers = resetLayers(config.defaults.startingLayers, config.layers);
+ const eventsReducerState = getInitialEventsState(config);
return {
p: {
stateKey: 'proj.id',
@@ -314,13 +315,23 @@ const getParameters = function(config, parameters) {
serialize: serializeEvent,
},
},
+ efs: {
+ stateKey: 'events.showAll',
+ initialState: true,
+ type: 'bool',
+ options: {
+ serializeNeedsGlobalState: true,
+ serialize: (showAll, state) => {
+ const eventsActive = get(state, 'events.active');
+ return eventsActive ? showAll : undefined;
+ },
+ setAsEmptyItem: true,
+ },
+ },
efd: {
stateKey: 'events.selectedDates',
type: 'object',
- initialState: {
- start: undefined,
- end: undefined,
- },
+ initialState: eventsReducerState.selectedDates,
options: {
parse: parseEventFilterDates,
serialize: serializeEventFilterDates,
diff --git a/web/js/map/natural-events/util.js b/web/js/map/natural-events/util.js
index 2cb14c55f..8162c92b1 100644
--- a/web/js/map/natural-events/util.js
+++ b/web/js/map/natural-events/util.js
@@ -7,19 +7,19 @@ import moment from 'moment';
import util from '../../util/util';
import { LIMIT_EVENT_REQUEST_COUNT } from '../../modules/natural-events/constants';
-export function getEventsRequestURL (baseUrl, startDate, endDate, categories = [], proj) {
+export function getEventsRequestURL (baseUrl, selectedDates, categories = [], proj, bbox) {
const params = {
status: 'all',
limit: LIMIT_EVENT_REQUEST_COUNT,
};
- if (startDate && endDate) {
- params.start = moment.utc(startDate).format('YYYY-MM-DD');
- params.end = moment.utc(endDate).format('YYYY-MM-DD');
+ const { start, end } = selectedDates;
+ if (start && end) {
+ params.start = moment.utc(start).format('YYYY-MM-DD');
+ params.end = moment.utc(end).format('YYYY-MM-DD');
}
if (categories.length) {
params.category = categories.map(({ id }) => id).join(',');
}
-
let [minLon, maxLat, maxLon, minLat] = [-180, 90, 180, -90];
const { crs } = proj.selected;
if (crs === 'EPSG:3413') {
@@ -28,6 +28,9 @@ export function getEventsRequestURL (baseUrl, startDate, endDate, categories = [
if (crs === 'EPSG:3031') {
[minLon, maxLat, maxLon, minLat] = [-180, -90, 180, -50];
}
+ if (bbox && bbox.length) {
+ [minLon, maxLat, maxLon, minLat] = bbox;
+ }
params.bbox = [minLon, maxLat, maxLon, minLat];
return `${baseUrl}/events${util.toQueryString(params)}`;
diff --git a/web/js/modules/natural-events/actions.js b/web/js/modules/natural-events/actions.js
index 791091609..c707fcd82 100644
--- a/web/js/modules/natural-events/actions.js
+++ b/web/js/modules/natural-events/actions.js
@@ -46,23 +46,31 @@ export function deselectEvent(id, date) {
};
}
-export function setEventsFilter(categories, startDate, endDate, showAll) {
+export function setEventsFilter(categories, start, end, showAll) {
return (dispatch, getState) => {
- const { config, proj, events } = getState();
- const { selectedCategories, selectedDates } = events;
+ const {
+ config, proj, events, map,
+ } = getState();
+ const {
+ selectedCategories,
+ selectedDates,
+ showAll: prevShowAll,
+ } = events;
+ const bbox = !showAll && map.ui.selected.getView().calculateExtent();
const baseUrl = lodashGet(config, 'features.naturalEvents.host');
- const requestUrl = getEventsRequestURL(baseUrl, startDate, endDate, categories, proj);
+ const requestUrl = getEventsRequestURL(baseUrl, { start, end }, categories, proj, bbox);
const sameCategories = lodashEqual(selectedCategories, categories);
- const sameDates = lodashEqual(selectedDates, { start: startDate, end: endDate });
- if (!sameCategories || !sameDates) {
+ const sameDates = lodashEqual(selectedDates, { start, end });
+
+ // Only make request if something has changed
+ if (!showAll || (prevShowAll !== showAll) || !sameCategories || !sameDates) {
dispatch(requestEvents(requestUrl));
}
-
dispatch({
type: SET_EVENTS_FILTER,
categories,
- startDate,
- endDate,
+ start,
+ end,
showAll,
});
};
diff --git a/web/js/modules/natural-events/reducers.js b/web/js/modules/natural-events/reducers.js
index e442032d3..64cde3f43 100644
--- a/web/js/modules/natural-events/reducers.js
+++ b/web/js/modules/natural-events/reducers.js
@@ -92,8 +92,8 @@ export function eventsReducer(state = eventsReducerState, action) {
showAll: action.showAll,
selectedCategories: action.categories,
selectedDates: {
- start: action.startDate,
- end: action.endDate,
+ start: action.start,
+ end: action.end,
},
};
case CHANGE_SIDEBAR_TAB: {
diff --git a/web/js/modules/natural-events/util.js b/web/js/modules/natural-events/util.js
index 046f74b8d..89a87e191 100644
--- a/web/js/modules/natural-events/util.js
+++ b/web/js/modules/natural-events/util.js
@@ -71,6 +71,12 @@ export function mapLocationToEventFilterState(parameters, stateFromLocation, sta
selectedStartDate = selected.date;
selectedEndDate = selected.date;
}
+ let showAll;
+ if (parameters.efs === 'true') {
+ showAll = true;
+ } else if (parameters.efs === 'false') {
+ showAll = false;
+ }
return update(stateFromLocation, {
events: {
@@ -81,6 +87,9 @@ export function mapLocationToEventFilterState(parameters, stateFromLocation, sta
start: { $set: selectedStartDate },
end: { $set: selectedEndDate },
},
+ showAll: {
+ $set: showAll,
+ },
},
});
}