Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

Reduxify notification module #1576

Benjamin Kingdc9be68225ff
diff --git a/web/css/alert.css b/web/css/notifications.css
similarity index 71%
rename from web/css/alert.css
rename to web/css/notifications.css
index ff32e801e..5f9b92fe8 100644
--- a/web/css/alert.css
+++ b/web/css/notifications.css
@@ -4,8 +4,8 @@
position: relative;
}
-#wv-info-button label::before {
- content: attr(data-content) '';
+#wv-info-button::before {
+ content: attr(datacontent) '';
display: block;
width: 20px;
height: 20px;
@@ -19,19 +19,19 @@
line-height: 1.45;
}
-#wv-info-button.wv-status-outage label::before {
+#wv-info-button.wv-status-outage::before {
background: #ff1a1e;
}
-#wv-info-button.wv-status-alert label::before {
+#wv-info-button.wv-status-alert::before {
background: #e5c937;
}
-#wv-info-button.wv-status-message label::before {
+#wv-info-button.wv-status-message::before {
background: #40a9db;
}
-#wv-info-button.wv-status-hide label::before {
+#wv-info-button.wv-status-hide::before {
display: none;
}
@@ -54,16 +54,19 @@
top: 4px;
z-index: 33;
}
-
-.ui-menu-item.exclamation-circle a::before {
+.list-group-item .badge {
+ position: absolute;
+ right: 5px;
+}
+.outage-notification .badge {
background: #ff1a1e;
}
-.ui-menu-item.bolt a::before {
+.alert-notification .badge {
background: #e5c937;
}
-.ui-menu-item.gift a::before {
+.message-notification .badge {
background: #40a9db;
}
@@ -80,15 +83,15 @@
padding: 0.75em 10px;
}
-.wv-notify-modal .outage h2 {
+.wv-notify-modal .outage-notification-item h2 {
background: rgba(179, 41, 43, 0.3);
}
-.wv-notify-modal .alert h2 {
+.wv-notify-modal .alert-notification-item h2 {
background: rgba(229, 201, 55, 0.3);
}
-.wv-notify-modal .message h2 {
+.wv-notify-modal .message-notification-item h2 {
background: rgba(64, 169, 219, 0.3);
}
@@ -106,17 +109,17 @@
margin: 10px;
}
-.wv-dialog .wv-notify-modal ul {
+.wv-notify-modal ul {
margin: 0;
padding: 0;
}
-.wv-dialog .wv-notify-modal ul li {
+.wv-notify-modal ul li {
list-style-type: none;
border-bottom: 1px solid #404040;
margin: 0;
}
-.wv-dialog .wv-notify-modal ul li div {
+.wv-notify-modal ul li div {
padding: 0;
}
diff --git a/web/js/app.js b/web/js/app.js
index a1979fd99..db0a37bcd 100644
--- a/web/js/app.js
+++ b/web/js/app.js
@@ -78,6 +78,16 @@ import loadingIndicator from './ui/indicator';
// Toolbar
import Toolbar from './containers/toolbar';
+
+// Notifications
+import {
+ STATUS_REQUEST_URL,
+ REQUEST_NOTIFICATIONS
+} from './modules/notifications/constants';
+import {
+ requestNotifications,
+ setNotifications
+} from './modules/notifications/actions';
// Link
import { linkModel } from './link/model';
@@ -124,7 +134,7 @@ import '../css/jquery-ui-override.css';
import '../css/rc-slider-overrides.css';
import '../css/util.css';
import '../css/toolbar.css';
-import '../css/alert.css';
+import '../css/notifications.css';
import '../css/sidebar-panel.css';
import '../css/button.css';
import '../css/checkbox.css';
@@ -486,7 +496,19 @@ class App extends React.Component {
}
// ui.link = linkUi(models, config);
if (config.features.alert) {
- // ui.alert = notificationsUi(ui, config);
+ let notificationURL = config.features.alert
+ ? config.features.alert.url
+ : STATUS_REQUEST_URL;
+ if (config.parameters.mockAlerts) {
+ notificationURL =
+ 'mock/notify_' + config.parameters.mockAlerts + '.json';
+ }
+ if (config.parameters.notificationURL) {
+ notificationURL =
+ 'https://status.earthdata.nasa.gov/api/v1/notifications?domain=' +
+ config.parameters.notificationURL;
+ }
+ self.props.requestNotifications(notificationURL);
}
if (config.features.compare) {
ui.compare = compareUi(models, ui, config);
@@ -606,6 +628,17 @@ const mapDispatchToProps = dispatch => ({
},
updatePermalink: permalink => {
dispatch(updatePermalink(permalink));
+ },
+ requestNotifications: location => {
+ const promise = dispatch(
+ requestNotifications(location, REQUEST_NOTIFICATIONS, 'json')
+ );
+ promise.then(data => {
+ const obj = JSON.parse(data);
+ if (obj.notifications) {
+ dispatch(setNotifications(obj.notifications));
+ }
+ });
}
});
diff --git a/web/js/components/notifications/notification-block.js b/web/js/components/notifications/notification-block.js
new file mode 100644
index 000000000..ecbdd3207
--- /dev/null
+++ b/web/js/components/notifications/notification-block.js
@@ -0,0 +1,47 @@
+import React from 'react';
+import PropTypes from 'prop-types';
+import util from '../../util/util';
+
+const CLASS_MATCHING_OBJ = {
+ alert: 'bolt',
+ message: 'gift',
+ outage: 'exclamation-circle'
+};
+
+class NotificationBlock extends React.Component {
+ render() {
+ const { arr, type, numberNotSeen } = this.props;
+ return (
+ <ul>
+ {arr.map((notification, i) => {
+ const dateObject = new Date(notification.created_at);
+ const date =
+ dateObject.getDate() +
+ ' ' +
+ util.giveMonth(dateObject) +
+ ' ' +
+ dateObject.getFullYear();
+ const activeClass =
+ numberNotSeen > i ? type + '-notification-item' : '';
+ return (
+ <li key={type + i} className={activeClass}>
+ <h2>
+ <i className={'fa fa-' + CLASS_MATCHING_OBJ[type]} />
+ <span>{'Posted ' + date}</span>
+ </h2>
+ <p dangerouslySetInnerHTML={{ __html: notification.message }} />
+ </li>
+ );
+ })}
+ </ul>
+ );
+ }
+}
+
+export default NotificationBlock;
+
+NotificationBlock.propTypes = {
+ arr: PropTypes.array.isRequired,
+ type: PropTypes.string.isRequired,
+ numberNotSeen: PropTypes.number.isRequired
+};
diff --git a/web/js/components/util/image-crop.js b/web/js/components/util/image-crop.js
index f8e5f3e93..cc2c9bcf9 100644
--- a/web/js/components/util/image-crop.js
+++ b/web/js/components/util/image-crop.js
@@ -101,5 +101,11 @@ Crop.propTypes = {
width: PropTypes.number,
height: PropTypes.number,
onChange: PropTypes.func.isRequired,
- onClose: PropTypes.func.isRequired
+ onClose: PropTypes.func.isRequired,
+ coordinates: PropTypes.object,
+ topRightStyle: PropTypes.object,
+ bottomLeftStyle: PropTypes.object,
+ maxWidth: PropTypes.number,
+ maxHeight: PropTypes.number,
+ showCoordinates: PropTypes.bool
};
diff --git a/web/js/components/util/list.js b/web/js/components/util/list.js
index 0d0cbba88..509682f9e 100644
--- a/web/js/components/util/list.js
+++ b/web/js/components/util/list.js
@@ -33,7 +33,7 @@ export default class List extends React.Component {
? item.onClick
: onClick
? () => {
- onClick(item.key);
+ onClick(item.key || item.id);
}
: null
}
@@ -53,5 +53,10 @@ List.defaultProps = {
size: 'medium'
};
List.propTypes = {
- list: PropTypes.array
+ list: PropTypes.array,
+ listClass: PropTypes.string,
+ active: PropTypes.string,
+ disabled: PropTypes.bool,
+ onClick: PropTypes.function,
+ size: PropTypes.string
};
diff --git a/web/js/containers/info.js b/web/js/containers/info.js
index 8ccdcff54..d600ebdad 100644
--- a/web/js/containers/info.js
+++ b/web/js/containers/info.js
@@ -1,16 +1,49 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
-import { requestLoadedPage, renderTemplate } from '../modules/modal/actions';
+import {
+ requestLoadedPage,
+ renderTemplate,
+ openCustomContent
+} from '../modules/modal/actions';
+import { ABOUT_PAGE_REQUEST } from '../modules/modal/constants';
import IconList from '../components/util/list';
import { onClickFeedback } from '../modules/feedback/util';
+import { addToLocalStorage } from '../modules/notifications/util';
+
import { initFeedback } from '../modules/feedback/actions';
+import { notificationsSeen } from '../modules/notifications/actions';
import util from '../util/util';
class InfoList extends Component {
- render() {
- const { sendFeedback, feedbackIsInitiated, aboutClick } = this.props;
- const infoArray = [
+ getNotificationListItem(obj) {
+ const { number, type, object } = this.props.notifications;
+ const baseIconclass = 'ui-icon fa fa-fw fa-';
+
+ return {
+ text: 'Notifications',
+ iconClass:
+ type === 'message'
+ ? baseIconclass + 'gift'
+ : type === 'outage'
+ ? baseIconclass + 'exclamation-circle'
+ : baseIconclass + 'bolt',
+ id: 'notifications_info_item',
+ badge: number,
+ className: type ? type + '-notification' : '',
+ onClick: () => {
+ this.props.notificationClick(object, number);
+ }
+ };
+ }
+ getListArray() {
+ const {
+ sendFeedback,
+ feedbackIsInitiated,
+ aboutClick,
+ notifications
+ } = this.props;
+ let arr = [
{
text: 'Send feedback',
iconClass: 'ui-icon fa fa-envelope fa-fw',
@@ -45,14 +78,24 @@ class InfoList extends Component {
}
}
];
+ if (notifications.isActive) {
+ let obj = this.getNotificationListItem();
+ arr.splice(4, 0, obj);
+ }
+ return arr;
+ }
+ render() {
+ const infoArray = this.getListArray();
return <IconList list={infoArray} size="small" />;
}
}
+
function mapStateToProps(state) {
const { isInitiated } = state.feedback;
return {
- feedbackIsInitiated: isInitiated
+ feedbackIsInitiated: isInitiated,
+ notifications: state.notifications
};
}
const mapDispatchToProps = dispatch => ({
@@ -63,13 +106,21 @@ const mapDispatchToProps = dispatch => ({
}
},
startTour: () => {},
+ notificationClick: (obj, num) => {
+ if (num > 0) {
+ dispatch(notificationsSeen());
+ addToLocalStorage(obj);
+ }
+
+ dispatch(openCustomContent('NOTIFICATION_LIST_MODAL'));
+ },
aboutClick: () => {
if (util.browser.small) {
window.open('pages/about.html?v=@BUILD_NONCE@', '_blank');
} else {
dispatch(
requestLoadedPage(
- 'MODAL_ABOUT_PAGE_REQUEST',
+ ABOUT_PAGE_REQUEST,
'pages/about.html?v=@BUILD_NONCE@',
'html'
)
@@ -85,5 +136,11 @@ export default connect(
)(InfoList);
InfoList.propTypes = {
- openModal: PropTypes.func
+ openModal: PropTypes.func,
+ notificationsRequest: PropTypes.object,
+ sendFeedback: PropTypes.func,
+ feedbackIsInitiated: PropTypes.bool,
+ aboutClick: PropTypes.func,
+ notificationClick: PropTypes.func,
+ notifications: PropTypes.object
};
diff --git a/web/js/containers/modal.js b/web/js/containers/modal.js
index 0976c9d16..53d22f710 100644
--- a/web/js/containers/modal.js
+++ b/web/js/containers/modal.js
@@ -3,7 +3,6 @@ import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import update from 'immutability-helper';
import { Modal, ModalBody, ModalHeader } from 'reactstrap';
-
import { customProps } from '../modules/modal/customs';
import { onToggle } from '../modules/modal/actions';
import DetectOuterClick from '../components/util/detect-outer-click';
@@ -124,9 +123,8 @@ export default connect(
ModalContainer.propTypes = {
isCustom: PropTypes.bool,
- id: PropTypes.string
-};
-ModalContainer.defualtProps = {
- type: 'default',
- backdrop: true
+ id: PropTypes.string,
+ bodyTemplate: PropTypes.node,
+ isOpen: PropTypes.bool,
+ isTemplateModal: PropTypes.bool
};
diff --git a/web/js/containers/notifications.js b/web/js/containers/notifications.js
new file mode 100644
index 000000000..fecd5389f
--- /dev/null
+++ b/web/js/containers/notifications.js
@@ -0,0 +1,46 @@
+import React, { Component } from 'react';
+import PropTypes from 'prop-types';
+import { connect } from 'react-redux';
+import NotificationBlock from '../components/notifications/notification-block';
+import { getNumberOfTypeNotSeen } from '../modules/notifications/util';
+
+class Notifications extends Component {
+ render() {
+ const { object } = this.props;
+ return (
+ <div className="wv-notify-modal">
+ <NotificationBlock
+ arr={object.outages}
+ type="outage"
+ numberNotSeen={getNumberOfTypeNotSeen('outage', object.outages)}
+ />
+ <NotificationBlock
+ arr={object.alerts}
+ type="alert"
+ numberNotSeen={getNumberOfTypeNotSeen('alert', object.alerts)}
+ />
+ <NotificationBlock
+ arr={object.messages}
+ type="message"
+ numberNotSeen={getNumberOfTypeNotSeen('message', object.messages)}
+ />
+ </div>
+ );
+ }
+}
+function mapStateToProps(state) {
+ const { object } = state.notifications;
+
+ return {
+ object
+ };
+}
+
+export default connect(
+ mapStateToProps,
+ null
+)(Notifications);
+
+Notifications.propTypes = {
+ object: PropTypes.object
+};
diff --git a/web/js/containers/projection.js b/web/js/containers/projection.js
index 125bceb4d..1aec60404 100644
--- a/web/js/containers/projection.js
+++ b/web/js/containers/projection.js
@@ -61,5 +61,8 @@ export default connect(
)(ProjectionList);
ProjectionList.propTypes = {
- openModal: PropTypes.func
+ openModal: PropTypes.func,
+ updateProjection: PropTypes.func,
+ models: PropTypes.object,
+ projection: PropTypes.string
};
diff --git a/web/js/containers/share.js b/web/js/containers/share.js
index 43fa16f2f..100ff7370 100644
--- a/web/js/containers/share.js
+++ b/web/js/containers/share.js
@@ -208,5 +208,8 @@ export default connect(
)(ShareLinkContainer);
ShareLinkContainer.propTypes = {
- projection: PropTypes.string
+ projection: PropTypes.string,
+ queryString: PropTypes.string,
+ requestShortLink: PropTypes.function,
+ mock: PropTypes.function
};
diff --git a/web/js/containers/toolbar.js b/web/js/containers/toolbar.js
index 33ddb9605..39cd96157 100644
--- a/web/js/containers/toolbar.js
+++ b/web/js/containers/toolbar.js
@@ -1,14 +1,21 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
-import { openCustomContent } from '../modules/modal/actions';
import { ButtonToolbar, Button } from 'reactstrap';
+import { openCustomContent } from '../modules/modal/actions';
class LinksContainer extends Component {
render() {
- const { openModal } = this.props;
+ const {
+ openModal,
+ notificationType,
+ notificationContentNumber
+ } = this.props;
+ const notificationClass = notificationType
+ ? ' wv-status-' + notificationType
+ : ' wv-status-hide';
return (
- <ButtonToolbar id="wv-toolbar" className="wv-toolbar">
+ <ButtonToolbar id="wv-toolbar" className={'wv-toolbar'}>
<Button
id="wv-link-button"
className="wv-toolbar-button"
@@ -36,8 +43,9 @@ class LinksContainer extends Component {
<Button
id="wv-info-button"
title="Information"
- className="wv-toolbar-button wv-status-hide"
+ className={'wv-toolbar-button' + notificationClass}
onClick={() => openModal('TOOLBAR_INFO')}
+ datacontent={notificationContentNumber}
>
<i className="fa fa-info-circle fa-2x" />{' '}
</Button>
@@ -45,7 +53,14 @@ class LinksContainer extends Component {
);
}
}
+function mapStateToProps(state) {
+ const { number, type } = state.notifications;
+ return {
+ notificationType: type,
+ notificationContentNumber: number
+ };
+}
const mapDispatchToProps = dispatch => ({
openModal: text => {
dispatch(openCustomContent(text));
@@ -53,7 +68,7 @@ const mapDispatchToProps = dispatch => ({
});
export default connect(
- null,
+ mapStateToProps,
mapDispatchToProps
)(LinksContainer);
diff --git a/web/js/main.js b/web/js/main.js
index 53766dee4..943dcc9ab 100644
--- a/web/js/main.js
+++ b/web/js/main.js
@@ -9,6 +9,10 @@ import legacyReducer from './modules/migration/reducers';
import feedbackReducer from './modules/feedback/reducers';
import projectionReducer from './modules/projection/reducer';
import { shortLink, linkReducer } from './modules/link/reducers';
+import {
+ notificationsRequest,
+ notificationsReducer
+} from './modules/notifications/reducers';
import thunk from 'redux-thunk';
const reducers = {
@@ -17,8 +21,10 @@ const reducers = {
legacy: legacyReducer,
feedback: feedbackReducer,
link: linkReducer,
+ notifications: notificationsReducer,
modalAboutPage,
- shortLink
+ shortLink,
+ notificationsRequest
};
const store = createStore(combineReducers(reducers), applyMiddleware(thunk));
diff --git a/web/js/modules/core/reducers.js b/web/js/modules/core/reducers.js
index cfadcd0d4..c2169ce6a 100644
--- a/web/js/modules/core/reducers.js
+++ b/web/js/modules/core/reducers.js
@@ -13,7 +13,7 @@ export function requestResponse(props = {}) {
);
}
-export function requestReducer(actionName, state, action) {
+export function requestReducer(actionName, state, action, callback) {
const START = `${actionName}_START`;
const SUCCESS = `${actionName}_SUCCESS`;
const FAILURE = `${actionName}_FAILURE`;
diff --git a/web/js/modules/modal/actions.js b/web/js/modules/modal/actions.js
index 949c49802..751a885a5 100644
--- a/web/js/modules/modal/actions.js
+++ b/web/js/modules/modal/actions.js
@@ -24,9 +24,9 @@ export function renderTemplate(headerText, template) {
headerText: headerText
};
}
-export function requestLoadedPage(pageName, location, type) {
+export function requestLoadedPage(pageActionName, location, type) {
return dispatch => {
- return requestAction(dispatch, pageName, location);
+ return requestAction(dispatch, pageActionName, location);
};
}
export function onToggle() {
diff --git a/web/js/modules/modal/constants.js b/web/js/modules/modal/constants.js
index d5bea3618..856b15632 100644
--- a/web/js/modules/modal/constants.js
+++ b/web/js/modules/modal/constants.js
@@ -3,6 +3,4 @@ export const OPEN_CUSTOM = 'OPEN_MODAL_WITH_CUSTOM_CONTENT';
export const OPEN_BASIC = 'OPEN_MODAL_WITH_BASIC_CONTENT';
export const RENDER_TEMPLATE = 'OPEN_MODAL_CONTAINING_LOADED_PAGE';
-export const ABOUT_PAGE_START = 'MODAL_ABOUT_PAGE_REQUEST_START';
-export const ABOUT_PAGE_SUCCESS = 'MODAL_ABOUT_PAGE_REQUEST_SUCCESS';
-export const ABOUT_PAGE_FAILURE = 'MODAL_ABOUT_PAGE_REQUEST_FAILURE';
+export const ABOUT_PAGE_REQUEST = 'MODAL_ABOUT_PAGE_REQUEST';
diff --git a/web/js/modules/modal/customs.js b/web/js/modules/modal/customs.js
index 7498ec5fa..4e23fd467 100644
--- a/web/js/modules/modal/customs.js
+++ b/web/js/modules/modal/customs.js
@@ -2,13 +2,14 @@ import ImageDownload from '../../containers/image-download';
import Projection from '../../containers/projection';
import InfoList from '../../containers/info';
import ShareLinks from '../../containers/share';
+import Notifications from '../../containers/notifications';
export const customProps = {
TOOLBAR_PROJECTION: {
headerText: null,
type: 'toolbar',
modalClassName: 'toolbar-list-modal toolbar-modal',
- backdrop: false,
+ backdrop: true,
bodyComponent: Projection,
offsetRight: '40px'
},
@@ -27,7 +28,6 @@ export const customProps = {
backdrop: false,
type: 'toolbar',
modalClassName: 'toolbar-list-modal toolbar-modal',
-
offsetRight: '10px',
bodyComponent: InfoList
},
@@ -39,5 +39,9 @@ export const customProps = {
offsetRight: '70px',
modalClassName: 'toolbar-snapshot-modal toolbar-modal toolbar-medium-modal',
bodyComponent: ImageDownload
+ },
+ NOTIFICATION_LIST_MODAL: {
+ headerText: 'Notifications',
+ bodyComponent: Notifications
}
};
diff --git a/web/js/modules/modal/reducers.js b/web/js/modules/modal/reducers.js
index f2fa4400c..0e674f151 100644
--- a/web/js/modules/modal/reducers.js
+++ b/web/js/modules/modal/reducers.js
@@ -1,4 +1,10 @@
-import { TOGGLE, OPEN_CUSTOM, OPEN_BASIC, RENDER_TEMPLATE } from './constants';
+import {
+ TOGGLE,
+ OPEN_CUSTOM,
+ OPEN_BASIC,
+ RENDER_TEMPLATE,
+ ABOUT_PAGE_REQUEST
+} from './constants';
import { requestReducer } from '../core/reducers';
import { assign as lodashAssign } from 'lodash';
import update from 'immutability-helper';
@@ -16,7 +22,7 @@ const modalState = {
bodyHTML: null
};
export function modalAboutPage(state = {}, action) {
- return requestReducer('MODAL_ABOUT_PAGE_REQUEST', state, action);
+ return requestReducer(ABOUT_PAGE_REQUEST, state, action);
}
export function modalReducer(state = modalState, action) {
switch (action.type) {
diff --git a/web/js/modules/notifications/actions.js b/web/js/modules/notifications/actions.js
new file mode 100644
index 000000000..cc64b4c83
--- /dev/null
+++ b/web/js/modules/notifications/actions.js
@@ -0,0 +1,24 @@
+import { requestAction } from '../core/actions';
+
+import {
+ REQUEST_NOTIFICATIONS,
+ SET_NOTIFICATIONS,
+ NOTIFICATIONS_SEEN
+} from './constants';
+
+export function requestNotifications(location, type) {
+ return dispatch => {
+ return requestAction(dispatch, REQUEST_NOTIFICATIONS, location);
+ };
+}
+export function setNotifications(array) {
+ return {
+ type: SET_NOTIFICATIONS,
+ array: array
+ };
+}
+export function notificationsSeen() {
+ return {
+ type: NOTIFICATIONS_SEEN
+ };
+}
diff --git a/web/js/modules/notifications/constants.js b/web/js/modules/notifications/constants.js
new file mode 100644
index 000000000..9ce8d0efa
--- /dev/null
+++ b/web/js/modules/notifications/constants.js
@@ -0,0 +1,5 @@
+export const REQUEST_NOTIFICATIONS = 'REQUEST_APP_NOTIFICATIONS';
+export const SET_NOTIFICATIONS = 'SET_NOTIFICATION_RESPONSE';
+export const NOTIFICATIONS_SEEN = 'NOTIFICATIONS_HAVE_BEEN_SEEN';
+export const STATUS_REQUEST_URL =
+ 'https://status.earthdata.nasa.gov/api/v1/notifications?domain=https%3A%2F%2Fworldview.earthdata.nasa.gov';
diff --git a/web/js/modules/notifications/reducers.js b/web/js/modules/notifications/reducers.js
new file mode 100644
index 000000000..728cba30b
--- /dev/null
+++ b/web/js/modules/notifications/reducers.js
@@ -0,0 +1,42 @@
+import { requestReducer } from '../core/reducers';
+import { getCount, separateByType, getPriority } from './util';
+import {
+ REQUEST_NOTIFICATIONS,
+ SET_NOTIFICATIONS,
+ NOTIFICATIONS_SEEN
+} from './constants';
+import { assign } from 'lodash';
+
+const notificationState = {
+ number: null,
+ type: '',
+ isActive: false,
+ object: {}
+};
+
+export function notificationsRequest(state = {}, action) {
+ return requestReducer(REQUEST_NOTIFICATIONS, state, action);
+}
+
+export function notificationsReducer(state = notificationState, action) {
+ switch (action.type) {
+ case SET_NOTIFICATIONS:
+ if (action.array.length > 0) {
+ const sortedNotificationObject = separateByType(action.array);
+ return assign({}, state, {
+ number: getCount(sortedNotificationObject),
+ type: getPriority(sortedNotificationObject),
+ isActive: true,
+ object: sortedNotificationObject
+ });
+ } else return state;
+ case NOTIFICATIONS_SEEN:
+ return assign({}, state, {
+ number: null,
+ type: null,
+ isActive: true
+ });
+ default:
+ return state;
+ }
+}
diff --git a/web/js/modules/notifications/util.js b/web/js/modules/notifications/util.js
new file mode 100644
index 000000000..545f7f008
--- /dev/null
+++ b/web/js/modules/notifications/util.js
@@ -0,0 +1,173 @@
+/**
+ * Categorizes the returned array
+ * @function separateByType
+ * @private
+ * @param {object} obj - array from API
+ * @returns {void}
+ */
+export function separateByType(array) {
+ var type;
+ var subObj;
+ var messages = [];
+ var alerts = [];
+ var outages = [];
+
+ for (var i = 0, len = array.length; i < len; i++) {
+ subObj = array[i];
+ type = subObj.notification_type;
+
+ if (type === 'message') {
+ messages.push(subObj);
+ } else if (type === 'alert') {
+ alerts.push(subObj);
+ } else {
+ outages.push(subObj);
+ }
+ }
+
+ return {
+ messages: orderByDate(messages),
+ alerts: orderByDate(alerts),
+ outages: orderByDate(outages)
+ };
+}
+/**
+ * Organizes array by date created
+ * @function orderByDate
+ * @private
+ * @param {object} obj - array
+ * @returns {void}
+ */
+var orderByDate = function(obj) {
+ obj.sort(function(a, b) {
+ return new Date(b.created_at) - new Date(a.created_at);
+ });
+
+ return obj;
+};
+/**
+ * Checks to see if id or smaller #id is in localstorage
+ * @function localStorageValueMatches
+ * @private
+ * @param {String} property - name of category type
+ * @param {string} value - id of notification
+ * @returns {Boolean}
+ */
+export function localStorageValueMatches(property, value) {
+ var oldValue = localStorage.getItem(property);
+ return new Date(value) <= new Date(oldValue);
+}
+
+/**
+ * Sets active state global values
+ * @function getPriority
+ * @private
+ * @param {object} sortedNotifications - object that contains categorized
+ * notifications
+ * @returns {void}
+ */
+export function getPriority(sortedNotifications) {
+ var priority, message, outage, alert;
+
+ priority = '';
+ message = sortedNotifications.messages[0];
+ outage = sortedNotifications.outages[0];
+ alert = sortedNotifications.alerts[0];
+
+ if (message && !objectAlreadySeen(message)) {
+ priority = 'message';
+ }
+
+ if (alert && !objectAlreadySeen(alert)) {
+ priority = 'alert';
+ }
+
+ if (outage && !objectAlreadySeen(outage)) {
+ priority = 'outage';
+ }
+
+ return priority;
+}
+/**
+ * Gets a total count of the unseen notifications
+ * @function getCounts
+ * @private
+ * @returns {Number}
+ */
+export function getCount(sortedNotifications) {
+ var outageCount, messageCount, alertCount;
+
+ messageCount = getNumberOfTypeNotSeen(
+ 'message',
+ sortedNotifications.messages
+ ); // Number of messages not yet seen
+ alertCount = getNumberOfTypeNotSeen('alert', sortedNotifications.alerts); // Number of alerts not yet seen
+ outageCount = getNumberOfTypeNotSeen('outage', sortedNotifications.outages); // Number of outages not yet seen
+
+ return messageCount + outageCount + alertCount;
+}
+export function addToLocalStorage(obj) {
+ const message = obj.messages[0];
+ const outage = obj.outages[0];
+ const alert = obj.alerts[0];
+
+ if (outage) {
+ localStorage.setItem('outage', outage.created_at);
+ }
+ if (alert) {
+ localStorage.setItem('alert', alert.created_at);
+ }
+ if (message) {
+ localStorage.setItem('message', message.created_at);
+ }
+}
+/**
+ * Determines the number of status of this type that the user is yet to see
+ * @function getNumberOfTypeNotSeen
+ * @private
+ * @param {string} type - Status type
+ * @param {object} arra - array of status of one type
+ * @returns {Number} count - number of unseen messages in LocalStorage
+ */
+export function getNumberOfTypeNotSeen(type, arra) {
+ var storageItem = localStorage.getItem(type);
+ var count, len;
+
+ len = arra.length;
+ count = 0;
+
+ if (!storageItem) {
+ return len;
+ }
+
+ for (var i = 0; i < len; i++) {
+ if (new Date(storageItem) < new Date(arra[i].created_at)) {
+ count++;
+ } else {
+ return count;
+ }
+ }
+
+ return count;
+}
+/**
+ * Determines if most recent notification has already been seen
+ * @function objectAlreadySeen
+ * @private
+ * @param {object} obj - object from API array
+ * @returns {void}
+ */
+var objectAlreadySeen = function(obj) {
+ var fieldExists, fieldValueMatches, type, idString;
+
+ type = obj.notification_type;
+ idString = obj.created_at.toString();
+ fieldValueMatches = false;
+ fieldExists = !!localStorage.getItem(type);
+
+ if (fieldExists) {
+ fieldValueMatches = localStorageValueMatches(type, idString);
+ }
+
+ return fieldValueMatches;
+};