Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

historical events base functionality

Jason Kent826a38005d52
diff --git a/web/js/components/sidebar/events-filter.js b/web/js/components/sidebar/events-filter.js
new file mode 100644
index 000000000..edaa5c35a
--- /dev/null
+++ b/web/js/components/sidebar/events-filter.js
@@ -0,0 +1,126 @@
+import React, { useState, useEffect } from 'react';
+import PropTypes from 'prop-types';
+import { connect } from 'react-redux';
+import { Button } from 'reactstrap';
+import Switch from '../util/switch';
+import Arrow from '../util/arrow';
+import {
+ setEventsFilter as setEventsFilterAction,
+} from '../../modules/natural-events/actions';
+
+function EventsFilter (props) {
+ const {
+ eventCategories,
+ selectedCategories,
+ selectedYear,
+ setFilter,
+ closeModal,
+ } = props;
+
+ const [year, setYear] = useState(selectedYear);
+ const onYearChange = (e) => {
+ setYear(e.target.value);
+ };
+ const onYearClick = (increment) => {
+ setYear(year + increment);
+ };
+
+ const [categories, setCategories] = useState(selectedCategories);
+ const toggleCategory = (categoryTitle) => {
+ const isActive = selectedCategories.includes(categoryTitle);
+ let newCategories;
+ if (isActive) {
+ newCategories = categories.filter((category) => category !== categoryTitle);
+ } else {
+ newCategories = [categoryTitle].concat(categories);
+ }
+ setCategories(newCategories);
+ };
+
+ const applyFilter = () => {
+ setFilter(categories, year);
+ closeModal();
+ };
+
+ return (
+ <div>
+
+ <div className="category-toggles">
+ {eventCategories.map((category) => {
+ const { id, title, description } = category;
+ const switchId = `${id}-switch`;
+ const isActive = categories.includes(title);
+ return (
+ <Switch
+ id={switchId}
+ key={switchId}
+ // color={}
+ label={title}
+ active={isActive}
+ tooltip={description}
+ toggle={() => toggleCategory(title)}
+ />
+ );
+ })}
+ </div>
+
+ <div className="year-selector">
+ <Arrow
+ direction="up"
+ onClick={() => onYearClick(1)}
+ type="year"
+ />
+ <input
+ type="text"
+ size={4}
+ maxLength={4}
+ // className={inputClassName}
+ id="year-selector-input"
+ value={year}
+ onChange={onYearChange}
+ // style={fontSizeStyle}
+ onBlur={onYearChange}
+ // onFocus={this.handleFocus}
+ />
+ <Arrow
+ direction="down"
+ onClick={() => onYearClick(-1)}
+ type="year"
+ />
+ </div>
+
+ <Button onClick={applyFilter}>
+ Apply
+ </Button>
+ <Button onClick={closeModal}>
+ Cancel
+ </Button>
+ </div>
+ );
+}
+
+EventsFilter.propTypes = {
+ closeModal: PropTypes.func,
+ eventCategories: PropTypes.array,
+ selectedCategories: PropTypes.array,
+ selectedYear: PropTypes.number,
+ setFilter: PropTypes.func,
+};
+
+const mapStateToProps = (state) => {
+ const { events, requestedEventCategories } = state;
+ const { selectedCategories, selectedYear } = events;
+ return {
+ eventCategories: requestedEventCategories.response || [],
+ selectedCategories,
+ selectedYear,
+ };
+};
+
+const mapDispatchToProps = (dispatch) => ({
+ setFilter: (categories, year) => {
+ dispatch(setEventsFilterAction(categories, year));
+ },
+});
+
+export default connect(mapStateToProps, mapDispatchToProps)(EventsFilter);
diff --git a/web/js/components/util/switch.js b/web/js/components/util/switch.js
index 989b66180..5937d17b0 100644
--- a/web/js/components/util/switch.js
+++ b/web/js/components/util/switch.js
@@ -59,11 +59,11 @@ const Switch = (props) => {
{tooltip
&& (
<>
- <FontAwesomeIcon icon="info-circle" id="switch-tooltip" />
+ <FontAwesomeIcon icon="info-circle" id={`${id}-switch-tooltip`} />
<Tooltip
placement="right"
isOpen={tooltipOpen}
- target="switch-tooltip"
+ target={`${id}-switch-tooltip`}
toggle={() => { toggleTooltip(!tooltipOpen); }}
>
{tooltip}
diff --git a/web/js/containers/sidebar/events.js b/web/js/containers/sidebar/events.js
index 355f40219..209970103 100644
--- a/web/js/containers/sidebar/events.js
+++ b/web/js/containers/sidebar/events.js
@@ -1,37 +1,35 @@
-import React, { useState } from 'react';
+import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { get as lodashGet } from 'lodash';
import {
- Dropdown, DropdownToggle, DropdownMenu, DropdownItem,
+ Button,
} from 'reactstrap';
+
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import Event from '../../components/sidebar/event';
+import EventFilter from '../../components/sidebar/events-filter';
import Scrollbars from '../../components/util/scrollbar';
import {
selectEvent as selectEventActionCreator,
deselectEvent as deselectEventActionCreator,
- selectCategory as selectCategoryActionCreator,
} from '../../modules/natural-events/actions';
-import { ALL_CATEGORY } from '../../modules/natural-events/constants';
import { getEventsWithinExtent } from '../../map/natural-events/util';
import { collapseSidebar } from '../../modules/sidebar/actions';
import { selectDate } from '../../modules/date/actions';
import { getSelectedDate } from '../../modules/date/selectors';
-import { getEventCategories } from '../../modules/natural-events/selectors';
+import { toggleCustomContent } from '../../modules/modal/actions';
import AlertUtil from '../../components/util/alert';
import util from '../../util/util';
function Events(props) {
const {
eventsData,
- eventCategories,
sources,
isLoading,
selectEvent,
selected,
- selectCategory,
- selectedCategory,
+ openFilterModal,
visibleEvents,
height,
deselectEvent,
@@ -42,9 +40,6 @@ function Events(props) {
selectedDate,
} = props;
- const [dropdownOpen, setDropdownOpen] = useState(false);
- const toggle = () => setDropdownOpen((prevState) => !prevState);
-
const dropdownHeight = 34;
const maxHeight = Math.max(height - dropdownHeight, 166);
const scrollbarMaxHeight = isEmbedModeActive ? '50vh' : `${maxHeight}px`;
@@ -54,48 +49,23 @@ function Events(props) {
let showInactiveEventAlert = missingEventDate || selectedEventNotInData;
const errorOrLoadingText = isLoading
- ? 'Loading...'
+ ? 'Loading ...'
: hasRequestError
? 'There has been an ERROR retrieving events from the EONET events API. Please try again later.'
: '';
- const eventsForSelectedCategory = !isLoading && (eventsData || []).filter((event) => {
- if (selectedCategory === ALL_CATEGORY) return event;
- if (event.categories.find((category) => category.title === selectedCategory)) {
- return event;
- }
- return false;
- });
-
return (
<div className="event-container">
- {!isEmbedModeActive && (
- <Dropdown id="event-category-dropdown" isOpen={dropdownOpen} toggle={toggle}>
- <DropdownToggle caret>
- {selectedCategory}
- </DropdownToggle>
- <DropdownMenu id="event-category-menu">
- <DropdownItem
- id="event-category-item-all"
- active={selectedCategory === ALL_CATEGORY}
- onClick={() => selectCategory(ALL_CATEGORY)}
- >
- {ALL_CATEGORY}
- </DropdownItem>
- {
- !isLoading && eventCategories.map((category) => (
- <DropdownItem
- id={`event-category-item-${category.toLowerCase()}`}
- key={category}
- onClick={() => selectCategory(category)}
- active={category === selectedCategory}
- >
- {category}
- </DropdownItem>
- ))
- }
- </DropdownMenu>
- </Dropdown>
+ {!isLoading && (
+ <Button
+ id="event-filter-button"
+ className="filter-button"
+ aria-label="Filtered layer search"
+ onClick={openFilterModal}
+ >
+ <FontAwesomeIcon icon="filter" />
+ Filter Events
+ </Button>
)}
<Scrollbars
style={{ maxHeight: `${scrollbarMaxHeight}` }}
@@ -114,10 +84,10 @@ function Events(props) {
</div>
)}
- {eventsForSelectedCategory.length ? (
+ {eventsData && eventsData.length ? (
<div className="wv-eventslist sidebar-panel">
<ul id="wv-eventscontent" className="content map-item-list">
- {sources && eventsForSelectedCategory.filter((event) => (isEmbedModeActive ? selected.id === event.id : true)).map((event) => (
+ {sources && eventsData.map((event) => (
<Event
showAlert={showAlert}
key={event.id}
@@ -133,7 +103,7 @@ function Events(props) {
</ul>
</div>
) : !isLoading && (
- <h3 className="no-events"> No events in this category.</h3>
+ <h3 className="no-events"> No events meet current criteria</h3>
)}
</div>
</Scrollbars>
@@ -166,8 +136,20 @@ const mapDispatchToProps = (dispatch) => ({
deselectEvent: () => {
dispatch(deselectEventActionCreator());
},
- selectCategory: (category) => {
- dispatch(selectCategoryActionCreator(category));
+ openFilterModal: () => {
+ dispatch(toggleCustomContent('events-filter', {
+ headerText: 'Filter Events',
+ backdrop: false,
+ bodyComponent: EventFilter,
+ // Using clickableBehindModal: true here causes an issue where switching sidebar
+ // tabs does not close this modal
+ wrapClassName: 'clickable-behind-modal',
+ modalClassName: ' layer-info-settings-modal layer-settings-modal',
+ timeout: 150,
+ bodyComponentProps: {
+
+ },
+ }));
},
});
@@ -181,7 +163,7 @@ const mapStateToProps = (state, ownProps) => {
} = state;
const { eventsData } = ownProps;
const {
- selected, showAll, category,
+ selected, showAll,
} = events;
let visibleEvents = {};
const mapExtent = lodashGet(state, 'map.extent');
@@ -207,7 +189,7 @@ const mapStateToProps = (state, ownProps) => {
}
return {
- eventCategories: getEventCategories(state),
+ eventsData,
showAll,
selected,
visibleWithinMapExtent,
@@ -217,7 +199,6 @@ const mapStateToProps = (state, ownProps) => {
isEmbedModeActive,
isAnimatingToEvent: events.isAnimatingToEvent,
selectedDate: util.toISOStringDate(getSelectedDate(state)),
- selectedCategory: category,
};
};
export default connect(
@@ -226,7 +207,6 @@ export default connect(
)(Events);
Events.propTypes = {
- eventCategories: PropTypes.array,
deselectEvent: PropTypes.func,
eventsData: PropTypes.array,
hasRequestError: PropTypes.bool,
@@ -234,11 +214,10 @@ Events.propTypes = {
isLoading: PropTypes.bool,
isMobile: PropTypes.bool,
isEmbedModeActive: PropTypes.bool,
+ openFilterModal: PropTypes.func,
selected: PropTypes.object,
selectedDate: PropTypes.string,
selectEvent: PropTypes.func,
- selectCategory: PropTypes.func,
- selectedCategory: PropTypes.string,
showAlert: PropTypes.bool,
sources: PropTypes.array,
visibleEvents: PropTypes.object,
diff --git a/web/js/containers/sidebar/sidebar.js b/web/js/containers/sidebar/sidebar.js
index 9cd62f90a..22ad7ef11 100644
--- a/web/js/containers/sidebar/sidebar.js
+++ b/web/js/containers/sidebar/sidebar.js
@@ -20,6 +20,7 @@ import { getPermalink } from '../../modules/link/util';
import {
requestEvents as requestEventsActionCreator,
requestSources as requestSourcesActionCreator,
+ requestCategories as requestCategoriesActionCreator,
} from '../../modules/natural-events/actions';
import { getAllActiveLayers } from '../../modules/layers/selectors';
import ErrorBoundary from '../error-boundary';
@@ -31,6 +32,7 @@ import {
} from '../../modules/sidebar/actions';
import history from '../../main';
import safeLocalStorage from '../../util/local-storage';
+import { initialEventsLoad } from '../../map/natural-events/util';
const { SIDEBAR_COLLAPSED } = safeLocalStorage.keys;
@@ -84,6 +86,10 @@ class Sidebar extends React.Component {
}
componentDidUpdate() {
+ const { activeTab } = this.props;
+ if (activeTab === 'events') {
+ this.loadEvents();
+ }
this.updateDimensions();
}
@@ -92,31 +98,18 @@ class Sidebar extends React.Component {
isLoadingEvents,
hasEventRequestError,
eventsData,
- eventRequestUrl,
config,
requestEvents,
requestSources,
+ requestCategories,
+ selectedYear,
} = this.props;
if (!isLoadingEvents && !hasEventRequestError && !eventsData) {
- let eventsRequestURL = `${eventRequestUrl}/events`;
- let sourceRequestURL = `${eventRequestUrl}/sources`;
-
- const mockEvents = lodashGet(config, 'parameters.mockEvents');
- const mockSources = lodashGet(config, 'parameters.mockSources');
-
- if (mockEvents) {
- console.warn(`Using mock events data: ${mockEvents}`);
- eventsRequestURL = mockEvents === 'true'
- ? 'mock/events_data.json'
- : `mock/events_data.json-${mockEvents}`;
- }
- if (mockSources) {
- console.warn(`Using mock categories data: ${mockSources}`);
- sourceRequestURL = `mock/categories_data.json-${mockSources}`;
- }
- requestEvents(eventsRequestURL);
- requestSources(sourceRequestURL);
+ const { sourcesURL, eventsURL, categoriesURL } = initialEventsLoad(config, selectedYear);
+ requestEvents(eventsURL);
+ requestSources(sourcesURL);
+ requestCategories(categoriesURL);
}
}
@@ -342,7 +335,8 @@ class Sidebar extends React.Component {
);
}
}
-function mapStateToProps(state) {
+
+const mapStateToProps = (state) => {
const {
animation,
browser,
@@ -355,17 +349,18 @@ function mapStateToProps(state) {
requestedEvents,
requestedEventSources,
sidebar,
+ requestedEventCategories,
ui,
} = state;
- const eventRequestUrl = lodashGet(state, 'config.features.naturalEvents.host');
const isLoadingEvents = requestedEvents.isLoading
- || requestedEventSources.isLoading;
- const hasEventRequestError = requestedEvents.error
- || requestedEventSources.error;
+ || requestedEventSources.isLoading
+ || requestedEventCategories.isLoading;
+ const hasEventRequestError = !!(requestedEvents.error
+ || requestedEventSources.error
+ || requestedEventCategories.error);
const eventsData = lodashGet(requestedEvents, 'response');
const eventsSources = lodashGet(requestedEventSources, 'response');
-
const { screenHeight } = browser;
const { isDistractionFreeModeActive } = ui;
const { isEmbedModeActive } = embed;
@@ -381,11 +376,11 @@ function 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;
+
return {
activeTab,
activeString,
config,
- eventRequestUrl,
eventsData,
eventsSources,
numberOfLayers,
@@ -397,11 +392,13 @@ function mapStateToProps(state) {
isEmbedModeActive,
isLoadingEvents,
isMobile,
+ selectedYear: events.selectedYear,
screenHeight,
selectedDate: getSelectedDate(state),
tabTypes,
};
-}
+};
+
const mapDispatchToProps = (dispatch) => ({
changeTab: (str) => {
dispatch(changeTabAction(str));
@@ -428,6 +425,9 @@ const mapDispatchToProps = (dispatch) => ({
requestSources: (url) => {
dispatch(requestSourcesActionCreator(url));
},
+ requestCategories: (url) => {
+ dispatch(requestCategoriesActionCreator(url));
+ },
});
export default connect(
@@ -442,7 +442,6 @@ Sidebar.propTypes = {
changeTab: PropTypes.func,
collapseExpandToggle: PropTypes.func,
config: PropTypes.object,
- eventRequestUrl: PropTypes.string,
eventsData: PropTypes.array,
eventsSources: PropTypes.array,
hasEventRequestError: PropTypes.bool,
@@ -461,4 +460,6 @@ Sidebar.propTypes = {
screenHeight: PropTypes.number,
selectedDate: PropTypes.object,
tabTypes: PropTypes.object,
+ selectedYear: PropTypes.number,
+ requestCategories: PropTypes.func,
};
diff --git a/web/js/map/natural-events/util.js b/web/js/map/natural-events/util.js
index 641873947..989fad22c 100644
--- a/web/js/map/natural-events/util.js
+++ b/web/js/map/natural-events/util.js
@@ -1,6 +1,48 @@
-import lodashFind from 'lodash/find';
+import {
+ find as lodashFind,
+ get as lodashGet,
+} from 'lodash';
import * as olProj from 'ol/proj';
import { getCenter, boundingExtent, containsCoordinate } from 'ol/extent';
+import moment from 'moment';
+import util from '../../util/util';
+
+export function getEventsRequestURL (baseUrl, year) {
+ const params = {
+ status: 'all',
+ };
+ if (year) {
+ params.start = moment.utc(`01-01-${year}`).format('YYYY-MM-DD');
+ params.end = moment.utc(`12-31-${year}`).format('YYYY-MM-DD');
+ }
+ return `${baseUrl}/events${util.toQueryString(params)}`;
+}
+
+export function initialEventsLoad (config, selectedYear) {
+ const baseUrl = lodashGet(config, 'features.naturalEvents.host');
+ let eventsURL = getEventsRequestURL(baseUrl, selectedYear);
+ let sourcesURL = `${baseUrl}/sources`;
+ const categoriesURL = `${baseUrl}/categories`;
+
+ const mockEvents = lodashGet(config, 'parameters.mockEvents');
+ const mockSources = lodashGet(config, 'parameters.mockSources');
+
+ if (mockEvents) {
+ console.warn(`Using mock events data: ${mockEvents}`);
+ eventsURL = mockEvents === 'true'
+ ? 'mock/events_data.json'
+ : `mock/events_data.json-${mockEvents}`;
+ }
+ if (mockSources) {
+ console.warn(`Using mock categories data: ${mockSources}`);
+ sourcesURL = `mock/categories_data.json-${mockSources}`;
+ }
+ return {
+ sourcesURL,
+ eventsURL,
+ categoriesURL,
+ };
+}
/**
*
diff --git a/web/js/modules/combine-reducers.js b/web/js/modules/combine-reducers.js
index c20461726..837fbd5fc 100644
--- a/web/js/modules/combine-reducers.js
+++ b/web/js/modules/combine-reducers.js
@@ -9,6 +9,7 @@ import { shortLink } from './link/reducers';
import {
requestedEvents,
requestedEventSources,
+ requestedEventCategories,
eventsReducer,
eventRequestResponse,
} from './natural-events/reducers';
@@ -87,6 +88,7 @@ export function getInitialState(models, config, parameters) {
layers: getLayersInitialState(config),
requestedEvents: eventRequestResponse(eventsIgnoreArray),
requestedEventSources: eventRequestResponse(eventsIgnoreArray),
+ requestedEventCategories: eventRequestResponse(eventsIgnoreArray),
palettes: getInitialPaletteState(config),
productPicker: getProductPickerInitialState(config),
vectorStyles: getInitialVectorStyleState(config),
@@ -123,6 +125,7 @@ const reducers = {
imageDownload: imageDownloadReducer,
requestedEvents,
requestedEventSources,
+ requestedEventCategories,
modalAboutPage,
shortLink,
smartHandoffs: smartHandoffReducer,
diff --git a/web/js/modules/core/actions.js b/web/js/modules/core/actions.js
index d5960f145..3ceee502a 100644
--- a/web/js/modules/core/actions.js
+++ b/web/js/modules/core/actions.js
@@ -5,9 +5,7 @@ export function requestAction(
mimeType,
id,
options,
- TIMEOUT_AMOUNT,
) {
- // let didTimeOut = false;
dispatch(startRequest(actionName, id));
return new Promise((resolve, reject) => fetch(url, options)
.then((response) => (mimeType === 'application/json'
@@ -23,6 +21,7 @@ export function requestAction(
reject(error);
}));
}
+
export function startRequest(actionName, id) {
return {
type: `${actionName}_START`,
@@ -31,18 +30,17 @@ export function startRequest(actionName, id) {
}
export function fetchSuccess(actionName, response, id) {
- return {
- type: `${actionName}_SUCCESS`,
- response,
- ...!!id && { id },
+ return (dispatch, getState) => {
+ const state = getState();
+ dispatch({
+ type: `${actionName}_SUCCESS`,
+ response,
+ ...!!id && { id },
+ state,
+ });
};
}
-// export function fetchTimeout(actionName, error, id) {
-// return {
-// type: `${actionName}_TIMEOUT`,
-// id: id
-// };
-// }
+
export function fetchFailure(actionName, error, id) {
return {
type: `${actionName}_FAILURE`,
diff --git a/web/js/modules/natural-events/actions.js b/web/js/modules/natural-events/actions.js
index 6ee2c0a5f..175277e89 100644
--- a/web/js/modules/natural-events/actions.js
+++ b/web/js/modules/natural-events/actions.js
@@ -1,15 +1,20 @@
+import {
+ get as lodashGet,
+} from 'lodash';
import {
REQUEST_EVENTS,
REQUEST_SOURCES,
+ REQUEST_CATEGORIES,
SELECT_EVENT,
DESELECT_EVENT,
SHOW_ALL_EVENTS,
- SELECT_CATEGORY,
+ SET_EVENTS_FILTER,
ONLY_SHOW_VISIBLE,
TOGGLE_SHOW_ALL,
FINISHED_ANIMATING_TO_EVENT,
} from './constants';
import { requestAction } from '../core/actions';
+import { getEventsRequestURL } from '../../map/natural-events/util';
export function requestEvents(location) {
return (dispatch) => requestAction(
@@ -29,6 +34,15 @@ export function requestSources(location) {
);
}
+export function requestCategories(location) {
+ return (dispatch) => requestAction(
+ dispatch,
+ REQUEST_CATEGORIES,
+ location,
+ 'application/json',
+ );
+}
+
export function selectEvent(id, eventDate) {
return {
type: SELECT_EVENT,
@@ -49,10 +63,17 @@ export function showAll() {
};
}
-export function selectCategory(category) {
- return {
- type: SELECT_CATEGORY,
- category,
+export function setEventsFilter(categories, year) {
+ return (dispatch, getState) => {
+ const { config } = getState();
+ const baseUrl = lodashGet(config, 'features.naturalEvents.host');
+ const requestUrl = getEventsRequestURL(baseUrl, year);
+ dispatch(requestEvents(requestUrl));
+ dispatch({
+ type: SET_EVENTS_FILTER,
+ categories,
+ year,
+ });
};
}
diff --git a/web/js/modules/natural-events/constants.js b/web/js/modules/natural-events/constants.js
index dfa3072ed..cc3819808 100644
--- a/web/js/modules/natural-events/constants.js
+++ b/web/js/modules/natural-events/constants.js
@@ -1,17 +1,24 @@
-export const REQUEST_EVENTS = 'EVENTS/REQUEST_EONET_EVENTS';
-export const REQUEST_EVENTS_START = 'EVENTS/REQUEST_EONET_EVENTS_START';
-export const REQUEST_EVENTS_FAILURE = 'EVENTS/REQUEST_EONET_EVENTS_FAILURE';
-export const REQUEST_EVENTS_SUCCESS = 'EVENTS/REQUEST_EONET_EVENTS_SUCCESS';
+export const REQUEST_EVENTS = 'EVENTS/REQUEST_EVENTS';
+export const REQUEST_EVENTS_START = 'EVENTS/REQUEST_EVENTS_START';
+export const REQUEST_EVENTS_FAILURE = 'EVENTS/REQUEST_EVENTS_FAILURE';
+export const REQUEST_EVENTS_SUCCESS = 'EVENTS/REQUEST_EVENTS_SUCCESS';
export const REQUEST_SOURCES = 'EVENTS/REQUEST_SOURCES';
export const REQUEST_SOURCES_START = 'EVENTS/REQUEST_SOURCES_START';
export const REQUEST_SOURCES_FAILURE = 'EVENTS/REQUEST_SOURCES_FAILURE';
export const REQUEST_SOURCES_SUCCESS = 'EVENTS/REQUEST_SOURCES_SUCCESS';
+export const REQUEST_CATEGORIES = 'EVENTS/REQUEST_CATEGORIES';
+export const REQUEST_CATEGORIES_START = 'EVENTS/REQUEST_CATEGORIES_START';
+export const REQUEST_CATEGORIES_FAILURE = 'EVENTS/REQUEST_CATEGORIES_FAILURE';
+export const REQUEST_CATEGORIES_SUCCESS = 'EVENTS/REQUEST_CATEGORIES_SUCCESS';
+
+export const SET_EVENTS_FILTER = 'EVENTS/SET_FILTER';
export const DESELECT_EVENT = 'EVENTS/DESELECT_EVENT';
export const SELECT_EVENT = 'EVENTS/SELECT_EVENT';
export const SHOW_ALL_EVENTS = 'EVENTS/SHOW_ALL_EVENTS';
-export const SELECT_CATEGORY = 'EVENTS/SELECT_CATEGORY';
+export const ADD_CATEGORY = 'EVENTS/ADD_CATEGORY';
+export const REMOVE_CATEGORY = 'EVENTS/REMOVE_CATEGORY';
export const ONLY_SHOW_VISIBLE = 'EVENTS/ONLY_SHOW_VISIBLE';
export const TOGGLE_SHOW_ALL = 'EVENTS/TOGGLE_SHOW_ALL';
export const FINISHED_ANIMATING_TO_EVENT = 'EVENTS/FINISHED_ANIMATING_TO_EVENT';
diff --git a/web/js/modules/natural-events/reducers.js b/web/js/modules/natural-events/reducers.js
index 0147fd945..7b74c6e22 100644
--- a/web/js/modules/natural-events/reducers.js
+++ b/web/js/modules/natural-events/reducers.js
@@ -2,23 +2,32 @@ import {
assign as lodashAssign,
orderBy as lodashOrderBy,
uniqBy as lodashUniqBy,
+ get as lodashGet,
} from 'lodash';
import {
- ALL_CATEGORY,
REQUEST_EVENTS,
REQUEST_SOURCES,
+ REQUEST_CATEGORIES,
SELECT_EVENT,
DESELECT_EVENT,
- SELECT_CATEGORY,
+ SET_EVENTS_FILTER,
SHOW_ALL_EVENTS,
ONLY_SHOW_VISIBLE,
TOGGLE_SHOW_ALL,
FINISHED_ANIMATING_TO_EVENT,
+ REQUEST_CATEGORIES_SUCCESS,
} from './constants';
import { CHANGE_TAB as CHANGE_SIDEBAR_TAB } from '../sidebar/constants';
-const sortEvents = function(events) {
+
+const sortEvents = function(events, categories) {
return events
+ .filter((e) => {
+ if (!categories.length) {
+ return true;
+ }
+ return e.categories.some(({ title }) => categories.includes(title));
+ })
.map((e) => {
e.geometry = lodashOrderBy(e.geometry, 'date', 'desc');
// Discard duplicate geometry dates
@@ -57,7 +66,8 @@ export const eventsReducerState = {
active: false,
showAll: true,
isAnimatingToEvent: false,
- category: ALL_CATEGORY,
+ selectedCategories: [],
+ selectedYear: new Date().getFullYear(),
};
export function eventsReducer(state = eventsReducerState, action) {
@@ -80,10 +90,16 @@ export function eventsReducer(state = eventsReducerState, action) {
...state,
selected: eventsReducerState.selected,
};
- case SELECT_CATEGORY:
+ case REQUEST_CATEGORIES_SUCCESS:
+ return {
+ ...state,
+ selectedCategories: action.response.categories.map(({ title }) => title),
+ };
+ case SET_EVENTS_FILTER:
return {
...state,
- category: action.category,
+ selectedCategories: action.categories,
+ selectedYear: action.year,
};
case SHOW_ALL_EVENTS:
return {
@@ -134,23 +150,31 @@ export function eventsRequestReducer(actionName, state, action) {
const START = `${actionName}_START`;
const SUCCESS = `${actionName}_SUCCESS`;
const FAILURE = `${actionName}_FAILURE`;
+ const selectedCategories = lodashGet(action, 'state.events.selectedCategories') || [];
switch (action.type) {
case START:
return eventRequestResponse({
+ ...state,
isLoading: true,
response: null,
});
+
case SUCCESS: {
- const key = actionName === REQUEST_EVENTS ? 'events' : 'sources';
+ const key = actionName === REQUEST_EVENTS
+ ? 'events'
+ : actionName === REQUEST_CATEGORIES
+ ? 'categories'
+ : 'sources';
const filtered = action.response[key].filter((item) => formatResponse(item, state.ignore));
return eventRequestResponse({
response: actionName === REQUEST_EVENTS
- ? sortEvents(filtered)
+ ? sortEvents(filtered, selectedCategories)
: filtered,
isLoading: false,
});
}
+
case FAILURE:
return eventRequestResponse({
response: null,
@@ -169,3 +193,7 @@ export function requestedEvents(state = {}, action) {
export function requestedEventSources(state = {}, action) {
return eventsRequestReducer(REQUEST_SOURCES, state, action);
}
+
+export function requestedEventCategories(state = {}, action) {
+ return eventsRequestReducer(REQUEST_CATEGORIES, state, action);
+}
diff --git a/web/js/modules/natural-events/selectors.js b/web/js/modules/natural-events/selectors.js
index 550dd3ab7..9daec7688 100644
--- a/web/js/modules/natural-events/selectors.js
+++ b/web/js/modules/natural-events/selectors.js
@@ -1,21 +1,26 @@
-import { createSelector } from 'reselect';
-import {
- get as lodashGet,
-} from 'lodash';
+// import { createSelector } from 'reselect';
+// import {
+// get as lodashGet,
+// } from 'lodash';
const getEvents = ({ requestedEvents }) => (requestedEvents
? requestedEvents.response
: []
);
-// eslint-disable-next-line import/prefer-default-export
-export const getEventCategories = createSelector(
- [getEvents],
- (events) => (events || []).reduce((categories, event) => {
- const categoryTitle = lodashGet(event, 'categories[0].title');
- if (categories.indexOf(categoryTitle) === -1) {
- categories.push(categoryTitle);
- }
- return categories;
- }, []),
+const getCategories = ({ requestedEventCategories }) => (requestedEventCategories
+ ? requestedEventCategories.response
+ : []
);
+
+// eslint-disable-next-line import/prefer-default-export
+// export const getEventCategories = createSelector(
+// [getEvents],
+// (events) => (events || []).reduce((categories, event) => {
+// const categoryTitle = lodashGet(event, 'categories[0].title');
+// if (categories.indexOf(categoryTitle) === -1) {
+// categories.push(categoryTitle);
+// }
+// return categories;
+// }, []),
+// );