Interactive interface for browsing global, full-resolution satellite imagery
refactor event request flow
Jason Kent1f61943b9424
diff --git a/web/js/containers/sidebar/sidebar.js b/web/js/containers/sidebar/sidebar.jsindex 4cd9e36cc..661cf964a 100644--- a/web/js/containers/sidebar/sidebar.js+++ b/web/js/containers/sidebar/sidebar.js@@ -38,7 +38,6 @@ import {} from '../../modules/sidebar/actions';import history from '../../main';import safeLocalStorage from '../../util/local-storage';-import { getEventsRequestURL } from '../../modules/natural-events/util';const { SIDEBAR_COLLAPSED } = safeLocalStorage.keys;@@ -67,14 +66,18 @@ class Sidebar extends React.Component {componentDidMount() {const {- activeTab, config, isEmbedModeActive, loadedCustomPalettes,+ activeTab,+ config,+ isEmbedModeActive,+ loadedCustomPalettes,+ requestSources,} = this.props;const customPalettePromise = loadCustomPalette(config);customPalettePromise.then((customs) => {loadedCustomPalettes(customs);});+ requestSources();this.updateDimensions();- this.loadEvents();// prevent events tab if embed init layers tabif (isEmbedModeActive && activeTab === 'layers') {this.setState({ isEventsTabDisabledEmbed: true });@@ -92,58 +95,15 @@ class Sidebar extends React.Component {}componentDidUpdate(prevProps) {- const { activeTab, proj } = this.props;- if (activeTab === 'events') {- const projChange = !lodashEqual(proj, prevProps.proj);- this.loadEvents(projChange);- }- this.updateDimensions();- }-- loadEvents(shouldUpdate) {const {- isLoadingEvents,- hasEventRequestError,- eventsData,- config,- proj,- bbox,- requestEvents,- requestSources,- selectedDates,- selectedCategories,+ activeTab, requestEvents, selectedMap, mapIsRendered,} = 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, selectedDates, selectedCategories, proj, bbox);- let sourcesURL = `${baseUrl}/sources`;-- if (mockEvents) {- // eslint-disable-next-line no-console- console.warn(`Using mock events data: ${mockEvents}`);- eventsURL = mockEvents === 'true'- ? 'mock/events_data.json'- : `mock/events_data.json-${mockEvents}`;- }- if (mockSources) {- // eslint-disable-next-line no-console- console.warn(`Using mock sources data: ${mockSources}`);- sourcesURL = `mock/sources_data.json-${mockSources}`;- }-- if (initialLoad) {- requestEvents(eventsURL);- requestSources(sourcesURL);- } else if (shouldUpdate) {- requestEvents(eventsURL);+ const mapChange = mapIsRendered && !lodashEqual(selectedMap, prevProps.selectedMap);+ const mapRenderedChange = mapIsRendered && mapIsRendered !== prevProps.mapIsRendered;+ if (activeTab === 'events' && (mapRenderedChange || mapChange)) {+ requestEvents();}+ this.updateDimensions();}updateDimensions() {@@ -381,15 +341,13 @@ const mapStateToProps = (state) => {events,measure,modal,+ map,requestedEvents,requestedEventSources,sidebar,ui,- proj,- map,} = state;- const { selectedCategories, selectedDates } = events;const isLoadingEvents = requestedEvents.isLoading|| requestedEventSources.isLoading;const hasEventRequestError = !!(requestedEvents.error@@ -412,12 +370,11 @@ const mapStateToProps = (state) => {const isMobile = browser.lessThan.medium;// Collapse when Image download / GIF / is open or measure tool activeconst shouldBeCollapsed = snapshotModalOpen || measure.isActive || animation.gifActive;- const bbox = !events.showAll ? map.extent : [];+ const selectedMap = map && map.ui && map.ui.selected;return {activeTab,activeString,- bbox,config,eventsData,eventsSources,@@ -430,9 +387,8 @@ const mapStateToProps = (state) => {isEmbedModeActive,isLoadingEvents,isMobile,- proj,- selectedDates,- selectedCategories,+ selectedMap,+ mapIsRendered: selectedMap && selectedMap.isRendered(),screenHeight,selectedDate: getSelectedDate(state),tabTypes,@@ -459,11 +415,11 @@ const mapDispatchToProps = (dispatch) => ({loadedCustomPalettes: (customs) => {dispatch(loadedCustomPalettesAction(customs));},- requestEvents: (url) => {- dispatch(requestEventsActionCreator(url));+ requestEvents: () => {+ dispatch(requestEventsActionCreator());},- requestSources: (url) => {- dispatch(requestSourcesActionCreator(url));+ requestSources: () => {+ dispatch(requestSourcesActionCreator());},});@@ -476,7 +432,6 @@ export default connect(Sidebar.propTypes = {activeString: PropTypes.string,activeTab: PropTypes.string,- bbox: PropTypes.array,changeTab: PropTypes.func,collapseExpandToggle: PropTypes.func,config: PropTypes.object,@@ -491,12 +446,11 @@ Sidebar.propTypes = {isLoadingEvents: PropTypes.bool,isMobile: PropTypes.bool,loadedCustomPalettes: PropTypes.func,+ mapIsRendered: PropTypes.bool,numberOfLayers: PropTypes.number,onTabClick: PropTypes.func,- proj: PropTypes.object,screenHeight: PropTypes.number,- selectedDates: PropTypes.object,- selectedCategories: PropTypes.array,+ selectedMap: PropTypes.object,tabTypes: PropTypes.object,requestEvents: PropTypes.func,requestSources: PropTypes.func,diff --git a/web/js/modules/natural-events/actions.js b/web/js/modules/natural-events/actions.jsindex 52256376b..dc84e024b 100644--- a/web/js/modules/natural-events/actions.js+++ b/web/js/modules/natural-events/actions.js@@ -16,23 +16,38 @@ import {} from './constants';import { requestAction } from '../core/actions';--export function requestEvents(location) {- return (dispatch) => requestAction(- dispatch,- REQUEST_EVENTS,- location,- 'application/json',- );+export function requestEvents() {+ return (dispatch, getState) => {+ const state = getState();+ const requestUrl = getEventsRequestURL(state);+ requestAction(+ dispatch,+ REQUEST_EVENTS,+ requestUrl,+ 'application/json',+ );+ };}-export function requestSources(location) {- return (dispatch) => requestAction(- dispatch,- REQUEST_SOURCES,- location,- 'application/json',- );+export function requestSources() {+ return (dispatch, getState) => {+ const { config } = getState();+ const baseUrl = lodashGet(config, 'features.naturalEvents.host');+ const mockSources = lodashGet(config, 'parameters.mockSources');+ let sourcesURL = `${baseUrl}/sources`;++ if (mockSources) {+ // eslint-disable-next-line no-console+ console.warn(`Using mock sources data: ${mockSources}`);+ sourcesURL = `mock/sources_data.json-${mockSources}`;+ }+ requestAction(+ dispatch,+ REQUEST_SOURCES,+ sourcesURL,+ 'application/json',+ );+ };}export function selectEvent(id, eventDate) {@@ -51,24 +66,14 @@ export function deselectEvent(id, date) {export function setEventsFilter(categories, start, end, showAll) {return (dispatch, getState) => {- 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, { start, end }, categories, proj, bbox);+ } = getState().events;+ const requestUrl = getEventsRequestURL(getState());const sameCategories = lodashEqual(selectedCategories, categories);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,@@ -76,6 +81,10 @@ export function setEventsFilter(categories, start, end, showAll) {end,showAll,});+ // Only make request if something has changed+ if (!showAll || (prevShowAll !== showAll) || !sameCategories || !sameDates) {+ dispatch(requestEvents(requestUrl));+ }};}diff --git a/web/js/modules/natural-events/util.js b/web/js/modules/natural-events/util.jsindex b48d0b87b..ca945ea00 100644--- a/web/js/modules/natural-events/util.js+++ b/web/js/modules/natural-events/util.js@@ -1,4 +1,4 @@-import { get } from 'lodash';+import { get as lodashGet } from 'lodash';import update from 'immutability-helper';import moment from 'moment';import util from '../../util/util';@@ -22,8 +22,8 @@ export function parseEvent(eventString) {};}export function serializeEvent(currentItemState) {- const eventId = get(currentItemState, 'selected.id');- const eventDate = get(currentItemState, 'selected.date');+ const eventId = lodashGet(currentItemState, 'selected.id');+ const eventDate = lodashGet(currentItemState, 'selected.date');const eventsTabActive = currentItemState.active;return eventsTabActive && eventDate && eventId? [eventId, eventDate].join(',')@@ -43,8 +43,8 @@ export function parseEventFilterDates(eventFilterDatesString) {}export function serializeEventFilterDates(currentItemState, state) {- const selectedStartDate = get(currentItemState, 'start');- const selectedEndDate = get(currentItemState, 'end');+ const selectedStartDate = lodashGet(currentItemState, 'start');+ const selectedEndDate = lodashGet(currentItemState, 'end');const eventsTabActive = state.events.active;return eventsTabActive && selectedStartDate && selectedEndDate? `${selectedStartDate},${selectedEndDate}`@@ -101,20 +101,42 @@ export function mapLocationToEventFilterState(parameters, stateFromLocation, sta});}-export function getEventsRequestURL (baseUrl, selectedDates, categories = [], proj, bbox) {+export function getEventsRequestURL (state) {+ const {+ config, proj, events, map,+ } = state;+ const {+ selectedCategories,+ selectedDates,+ showAll,+ } = events;++ const baseUrl = lodashGet(config, 'features.naturalEvents.host');+ const mockEvents = lodashGet(config, 'parameters.mockEvents');++ if (mockEvents) {+ // eslint-disable-next-line no-console+ console.warn(`Using mock events data: ${mockEvents}`);+ return mockEvents === 'true'+ ? 'mock/events_data.json'+ : `mock/events_data.json-${mockEvents}`;+ }+const { crs } = proj.selected;+ const selectedMap = map && map.ui.selected;+ const bbox = !showAll && selectedMap && selectedMap.getView().calculateExtent();+ const { start, end } = selectedDates;const params = {status: 'all',limit: LIMIT_EVENT_REQUEST_COUNT,};- 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(',');+ if (selectedCategories.length) {+ params.category = selectedCategories.map(({ id }) => id).join(',');}let [minLon, maxLat, maxLon, minLat] = [-180, 90, 180, -90];