Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

WV-4038 Modernize Alert Components (#6803)

vicgonzalez25879b2e2e913c
diff --git a/web/js/components/feature-alert/alert.js b/web/js/components/feature-alert/alert.js
deleted file mode 100644
index a74aca3a7..000000000
--- a/web/js/components/feature-alert/alert.js
+++ /dev/null
@@ -1,51 +0,0 @@
-import React from 'react';
-import { connect } from 'react-redux';
-import { openCustomContent } from '../../modules/modal/actions';
-// import safeLocalStorage from '../../util/local-storage';
-
-class FeaturedAlert extends React.Component {
- constructor(props) {
- super(props);
- const alertHasBeenShown = true;
- this.state = {
- showAlert: !alertHasBeenShown,
- };
- }
-
- render() {
- // const { showAlert } = this.state;
- return '';
- // return (
- // <AlertUtil
- // id={'geostationary-alert'}
- // isOpen={showAlert}
- // iconClassName='faLayerGroup'
- // onClick={this.props.showModal.bind(this)}
- // onDismiss={this.dismissAlert.bind(this)}
- // message="Check out our new geostationary layers!"
- // noPortal={true}
- // />
- // );
- }
-}
-
-const mapDispatchToProps = (dispatch) => ({
- showModal: () => {
- dispatch(
- openCustomContent('', {
- headerText: '',
- backdrop: false,
- size: 'lg',
- wrapClassName: '',
- clickableBehindModal: true,
- bodyComponent: undefined,
- scrollable: true,
- }),
- );
- },
-});
-
-export default connect(
- null,
- mapDispatchToProps,
-)(FeaturedAlert);
diff --git a/web/js/components/feature-alert/alert.test.js b/web/js/components/feature-alert/alert.test.js
deleted file mode 100644
index e11cc8c54..000000000
--- a/web/js/components/feature-alert/alert.test.js
+++ /dev/null
@@ -1,32 +0,0 @@
-import React from 'react';
-import { render } from '@testing-library/react';
-import FeaturedAlert from './alert';
-
-jest.mock('../../modules/modal/actions', () => ({
- openCustomContent: jest.fn(),
-}));
-
-describe('FeaturedAlert component', () => {
- it('constructs with showAlert false when alertHasBeenShown is true', () => {
- const Wrapped = FeaturedAlert.WrappedComponent || FeaturedAlert;
- const instance = new Wrapped({});
- expect(instance.state).toBeDefined();
- expect(instance.state.showAlert).toBe(false);
- });
-
- it('renders empty string', () => {
- const { container } = render(
- React.createElement(FeaturedAlert.WrappedComponent || FeaturedAlert),
- );
- expect(container.innerHTML).toBe('');
- });
-
- it('calls provided showModal prop when invoked externally', () => {
- const Wrapped = FeaturedAlert.WrappedComponent || FeaturedAlert;
- const mockShow = jest.fn();
- const instance = new Wrapped({ showModal: mockShow });
- // simulate an external call to the prop
- instance.props.showModal();
- expect(mockShow).toHaveBeenCalled();
- });
-});
diff --git a/web/js/components/util/alert.js b/web/js/components/util/alert.js
index df91db243..f652a795c 100644
--- a/web/js/components/util/alert.js
+++ b/web/js/components/util/alert.js
@@ -1,105 +1,86 @@
-import React from 'react';
+import { useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import { Alert } from 'reactstrap';
import { createPortal } from 'react-dom';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
-/*
- * A react component, Builds a rather specific
- * interactive widget
- *
- * @class AlertComponent
- * @extends React.Component
- */
-export default class AlertUtil extends React.Component {
- constructor(props) {
- super(props);
- if (props.timeout && props.onDismiss) {
- this.timeout = setTimeout(() => {
- props.onDismiss();
- }, props.timeout);
- }
- this.state = {
- isOpen: props.isOpen,
- };
- }
+function AlertUtil({
+ id,
+ isOpen: isOpenProp,
+ title,
+ message,
+ messageTitle,
+ icon,
+ onDismiss,
+ onClick,
+ timeout,
+ noPortal,
+}) {
+ const [isOpen, setIsOpen] = useState(isOpenProp);
- componentWillUnmount() {
- if (this.timeout) {
- clearTimeout(this.timeout);
+ // Replaces constructor timeout + componentWillUnmount cleanup
+ useEffect(() => {
+ if (timeout && onDismiss) {
+ const timer = setTimeout(() => {
+ onDismiss();
+ }, timeout);
+ return () => clearTimeout(timer);
}
- }
+ }, []);
- closeAlert() {
- const { onDismiss } = this.props;
- this.setState({ isOpen: false });
+ const closeAlert = () => {
+ setIsOpen(false);
if (onDismiss) {
onDismiss();
}
- }
-
- renderAlert() {
- const {
- id,
- title,
- message,
- messageTitle,
- icon,
- onDismiss,
- onClick,
- } = this.props;
- const { isOpen } = this.state;
+ };
- return (
- <Alert
- id={id}
- className="wv-alert"
- isOpen={isOpen}
+ const alertContent = (
+ <Alert
+ id={id}
+ className="wv-alert"
+ isOpen={isOpen}
+ >
+ <div
+ role="alertdialog"
+ className="alert-content"
+ title={title}
+ onClick={onClick}
+ style={{ paddingRight: !onDismiss ? 8 : 5 }}
>
- <div
- role="alertdialog"
- className="alert-content"
- title={title}
- onClick={onClick}
- style={{ paddingRight: !onDismiss ? 8 : 5 }}
- >
- <FontAwesomeIcon
- icon={icon || 'exclamation-triangle'}
- className="wv-alert-icon"
- size="1x"
- widthAuto
- />
- <div className="alert-text">
- <p className="wv-alert-title">
- {messageTitle}
- </p>
- <em className="wv-alert-message">
- <b>{message}</b>
- </em>
- </div>
+ <FontAwesomeIcon
+ icon={icon || 'exclamation-triangle'}
+ className="wv-alert-icon"
+ size="1x"
+ widthAuto
+ />
+ <div className="alert-text">
+ <p className="wv-alert-title">
+ {messageTitle}
+ </p>
+ <em className="wv-alert-message">
+ <b>{message}</b>
+ </em>
</div>
- {onDismiss && (
- <button
- type="button"
- id={`${id}-close`}
- className="close-alert"
- onClick={() => this.closeAlert()}
- >
- <FontAwesomeIcon icon="times" className="exit" size="1x" widthAuto />
- </button>
- )}
- </Alert>
- );
- }
+ </div>
+ {onDismiss && (
+ <button
+ type="button"
+ id={`${id}-close`}
+ className="close-alert"
+ onClick={() => closeAlert()}
+ >
+ <FontAwesomeIcon icon="times" className="exit" size="1x" widthAuto />
+ </button>
+ )}
+ </Alert>
+ );
- render() {
- const { noPortal } = this.props;
- const alertContainer = document.getElementById('wv-alert-container');
- if (!noPortal && alertContainer) {
- return createPortal(this.renderAlert(), document.getElementById('wv-alert-container'));
- }
- return this.renderAlert();
+ const alertContainer = document.getElementById('wv-alert-container');
+ if (!noPortal && alertContainer) {
+ return createPortal(alertContent, alertContainer);
}
+ return alertContent;
}
AlertUtil.defaultProps = {
@@ -118,3 +99,5 @@ AlertUtil.propTypes = {
timeout: PropTypes.number,
title: PropTypes.string,
};
+
+export default AlertUtil;
diff --git a/web/js/containers/alertDropdown.js b/web/js/containers/alertDropdown.js
index 75cc077c7..b2dec45f9 100644
--- a/web/js/containers/alertDropdown.js
+++ b/web/js/containers/alertDropdown.js
@@ -1,7 +1,6 @@
import { useState, useRef } from 'react';
import { useSelector } from 'react-redux';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
-import FeatureAlert from '../components/feature-alert/alert';
import Alerts from './alerts';
export default function AlertDropdown(isTourActive) {
@@ -26,7 +25,6 @@ export default function AlertDropdown(isTourActive) {
{dropdownOpen ? <FontAwesomeIcon icon="fa-solid fa-caret-down" widthAuto /> : <FontAwesomeIcon icon="fa-solid fa-caret-up" widthAuto />}
</button>
<div ref={containerRef} hidden={!(dropdownOpen || notifications === 1)} id="wv-alert-container" className="wv-alert-container">
- <FeatureAlert />
<Alerts />
</div>
<div className="wv-alert-footer" hidden={!dropdownOpen || notifications <= 1}>
diff --git a/web/js/containers/alertDropdown.test.js b/web/js/containers/alertDropdown.test.js
index a929c9da5..81d55c4ed 100644
--- a/web/js/containers/alertDropdown.test.js
+++ b/web/js/containers/alertDropdown.test.js
@@ -12,8 +12,12 @@ jest.mock('@fortawesome/react-fontawesome', () => ({
FontAwesomeIcon: (props) => <i data-testid="fa-icon" data-icon={String(props.icon)} />,
}));
-jest.mock('../components/feature-alert/alert', () => () => <div data-testid="feature-alert" />);
-jest.mock('./alerts', () => () => <div data-testid="alerts" />);
+jest.mock('./alerts', () => () => (
+ <>
+ <div data-testid="alerts" />
+ <div data-testid="alerts-2" />
+ </>
+));
const AlertDropdown = require('./alertDropdown').default;
@@ -52,9 +56,8 @@ describe('AlertDropdown', () => {
expect(container.querySelector('.wv-alert-dropdown')).toHaveAttribute('hidden');
});
- it('renders FeatureAlert and Alerts inside the alert container', () => {
+ it('renders Alerts inside the alert container', () => {
const { getByTestId } = render(<AlertDropdown isTourActive={false} />);
- expect(getByTestId('feature-alert')).toBeInTheDocument();
expect(getByTestId('alerts')).toBeInTheDocument();
});
diff --git a/web/js/containers/alerts.js b/web/js/containers/alerts.js
index fd1ee493d..18f2157f7 100644
--- a/web/js/containers/alerts.js
+++ b/web/js/containers/alerts.js
@@ -1,4 +1,4 @@
-import React from 'react';
+import { useState, useEffect, useRef } from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import AlertUtil from '../components/util/alert';
@@ -23,184 +23,177 @@ const {
DISSMISSED_DDV_LOCATION_ALERT,
} = safeLocalStorage.keys;
-class DismissableAlerts extends React.Component {
- constructor(props) {
- super(props);
+function DismissableAlerts(props) {
+ const {
+ dismissVectorZoomAlert,
+ dismissVectorExceededAlert,
+ hasSubdailyLayers,
+ isCompareActive,
+ isDistractionFreeModeActive,
+ isEmbedModeActive,
+ isEventsActive,
+ isSmall,
+ isMobile,
+ isAnimationActive,
+ isVectorZoomAlertPresent,
+ isVectorExceededAlertPresent,
+ openAlertModal,
+ isDDVZoomAlertPresent,
+ isDDVLocationAlertPresent,
+ openGranuleAlertModal,
+ openZoomAlertModal,
+ ddvZoomAlerts,
+ ddvLocationAlerts,
+ } = props;
- this.state = {
- hasDismissedEvents: !!safeLocalStorage.getItem(DISMISSED_EVENT_VIS_ALERT),
- hasDismissedCompare: !!safeLocalStorage.getItem(DISMISSED_COMPARE_ALERT),
- hasDismissedDistractionFree: !!safeLocalStorage.getItem(DISMISSED_DISTRACTION_FREE_ALERT),
- hasDismissedDDVZoom: !!safeLocalStorage.getItem(DISSMISSED_DDV_ZOOM_ALERT),
- hasDismissedDDVLocation: !!safeLocalStorage.getItem(DISSMISSED_DDV_LOCATION_ALERT),
- distractionFreeModeInitLoad: false,
- };
- }
+ const [hasDismissedEvents, setHasDismissedEvents] = useState(
+ () => !!safeLocalStorage.getItem(DISMISSED_EVENT_VIS_ALERT),
+ );
+ const [hasDismissedCompare, setHasDismissedCompare] = useState(
+ () => !!safeLocalStorage.getItem(DISMISSED_COMPARE_ALERT),
+ );
+ const [hasDismissedDistractionFree, setHasDismissedDistractionFree] = useState(
+ () => !!safeLocalStorage.getItem(DISMISSED_DISTRACTION_FREE_ALERT),
+ );
+ const [hasDismissedDDVZoom, setHasDismissedDDVZoom] = useState(
+ () => !!safeLocalStorage.getItem(DISSMISSED_DDV_ZOOM_ALERT),
+ );
+ const [hasDismissedDDVLocation, setHasDismissedDDVLocation] = useState(
+ () => !!safeLocalStorage.getItem(DISSMISSED_DDV_LOCATION_ALERT),
+ );
+ const [distractionFreeModeInitLoad, setDistractionFreeModeInitLoad] = useState(false);
+
+ const prevDistractionFreeRef = useRef(isDistractionFreeModeActive);
- componentDidMount() {
- const { isDistractionFreeModeActive } = this.props;
+ // componentDidMount: set init load flag if distraction free mode is already active
+ useEffect(() => {
if (isDistractionFreeModeActive) {
- this.toggleDistractionFreeModeInitLoad(true);
+ setDistractionFreeModeInitLoad(true);
}
- }
+ }, []);
- componentDidUpdate(prevProps) {
- const { isDistractionFreeModeActive } = this.props;
- const { distractionFreeModeInitLoad } = this.state;
- const isDistractionFreeModeActiveChanged = prevProps.isDistractionFreeModeActive &&
- !isDistractionFreeModeActive;
- if (distractionFreeModeInitLoad && isDistractionFreeModeActiveChanged) {
- this.toggleDistractionFreeModeInitLoad(false);
+ // componentDidUpdate: clear init load flag when distraction free mode is deactivated
+ useEffect(() => {
+ const prev = prevDistractionFreeRef.current;
+ prevDistractionFreeRef.current = isDistractionFreeModeActive;
+ if (distractionFreeModeInitLoad && prev && !isDistractionFreeModeActive) {
+ setDistractionFreeModeInitLoad(false);
}
- }
-
- toggleDistractionFreeModeInitLoad(isActive) {
- this.setState({ distractionFreeModeInitLoad: isActive });
- }
+ }, [isDistractionFreeModeActive, distractionFreeModeInitLoad]);
- /**
- * Update state and local storage when
- * alert is dismissed
- *
- * @param {String} storageKey
- * @param {String} stateKey
- */
- dismissAlert(storageKey, stateKey) {
+ const dismissAlert = (storageKey, stateKey) => {
safeLocalStorage.setItem(storageKey, true);
- this.setState({ [stateKey]: true });
- }
+ const setters = {
+ hasDismissedEvents: setHasDismissedEvents,
+ hasDismissedCompare: setHasDismissedCompare,
+ hasDismissedDistractionFree: setHasDismissedDistractionFree,
+ hasDismissedDDVZoom: setHasDismissedDDVZoom,
+ hasDismissedDDVLocation: setHasDismissedDDVLocation,
+ };
+ if (setters[stateKey]) setters[stateKey](true);
+ };
- render() {
- const {
- dismissVectorZoomAlert,
- dismissVectorExceededAlert,
- hasSubdailyLayers,
- isCompareActive,
- isDistractionFreeModeActive,
- isEmbedModeActive,
- isEventsActive,
- isSmall,
- isMobile,
- isAnimationActive,
- isVectorZoomAlertPresent,
- isVectorExceededAlertPresent,
- openAlertModal,
- isDDVZoomAlertPresent,
- isDDVLocationAlertPresent,
- openGranuleAlertModal,
- openZoomAlertModal,
- ddvZoomAlerts,
- ddvLocationAlerts,
- } = this.props;
- const {
- hasDismissedEvents,
- hasDismissedCompare,
- hasDismissedDistractionFree,
- distractionFreeModeInitLoad,
- hasDismissedDDVZoom,
- hasDismissedDDVLocation,
- } = this.state;
- const { eventModalProps, compareModalProps, vectorModalProps } = MODAL_PROPERTIES;
- const hasFailCondition = !HAS_LOCAL_STORAGE ||
+ const { eventModalProps, compareModalProps, vectorModalProps } = MODAL_PROPERTIES;
+ const hasFailCondition = !HAS_LOCAL_STORAGE ||
isEmbedModeActive ||
distractionFreeModeInitLoad;
- if (hasFailCondition) return null;
+ if (hasFailCondition) return null;
- const showEventsAlert = !isSmall && !hasDismissedEvents && isEventsActive;
- const showCompareAlert = !isSmall && !hasDismissedCompare && isCompareActive;
- const showAnimationAlert = isMobile && isAnimationActive && hasSubdailyLayers;
- const showDDVZoomAlert = isDDVZoomAlertPresent && !hasDismissedDDVZoom;
- const showDDVLocationAlert = isDDVLocationAlertPresent && !hasDismissedDDVLocation;
+ const showEventsAlert = !isSmall && !hasDismissedEvents && isEventsActive;
+ const showCompareAlert = !isSmall && !hasDismissedCompare && isCompareActive;
+ const showAnimationAlert = isMobile && isAnimationActive && hasSubdailyLayers;
+ const showDDVZoomAlert = isDDVZoomAlertPresent && !hasDismissedDDVZoom;
+ const showDDVLocationAlert = isDDVLocationAlertPresent && !hasDismissedDDVLocation;
- return isDistractionFreeModeActive
- ? !hasDismissedDistractionFree && (
- <AlertUtil
- id="distraction-free-mode-active-alert"
- isOpen
- noPortal
- onDismiss={() => this.dismissAlert(DISMISSED_DISTRACTION_FREE_ALERT, 'hasDismissedDistractionFree')}
- message="You are now in distraction free mode. Click the eye button to exit."
- />
- )
- : (
- <>
- {showEventsAlert && (
+ return isDistractionFreeModeActive
+ ? !hasDismissedDistractionFree && (
+ <AlertUtil
+ id="distraction-free-mode-active-alert"
+ isOpen
+ noPortal
+ onDismiss={() => dismissAlert(DISMISSED_DISTRACTION_FREE_ALERT, 'hasDismissedDistractionFree')}
+ message="You are now in distraction free mode. Click the eye button to exit."
+ />
+ )
+ : (
+ <>
+ {showEventsAlert && (
+ <AlertUtil
+ id="event-alert"
+ isOpen
+ noPortal
+ onClick={() => openAlertModal(eventModalProps)}
+ onDismiss={() => dismissAlert(DISMISSED_EVENT_VIS_ALERT, 'hasDismissedEvents')}
+ message="Events may not be visible at all times."
+ />
+ )}
+ {showCompareAlert && (
+ <AlertUtil
+ isOpen
+ noPortal
+ onClick={() => openAlertModal(compareModalProps)}
+ onDismiss={() => dismissAlert(DISMISSED_COMPARE_ALERT, 'hasDismissedCompare')}
+ message="You are now in comparison mode."
+ />
+ )}
+ {isVectorZoomAlertPresent && (
+ <AlertUtil
+ isOpen
+ noPortal
+ onClick={() => openAlertModal(vectorModalProps)}
+ onDismiss={dismissVectorZoomAlert}
+ message="Vector features may not be clickable at all zoom levels."
+ />
+ )}
+ {isVectorExceededAlertPresent && (
+ <AlertUtil
+ isOpen
+ noPortal
+ onDismiss={dismissVectorExceededAlert}
+ message="Too many results at selected point. Zoom in map to see more individual points."
+ />
+ )}
+ {showAnimationAlert && (
+ <AlertUtil
+ isOpen
+ noPortal
+ icon="info-circle"
+ message="Some animations may use a large amount of data (>100MB)"
+ onDismiss={() => {}}
+ />
+ )}
+ {showDDVZoomAlert &&
+ ddvZoomAlerts.map((layer) => (
<AlertUtil
- id="event-alert"
+ key={`ddv-zoom-${layer}`}
+ id={`zoom-alert-${`${layer}`.replace(/[^a-z0-9_-]/gi, '-')}`}
isOpen
noPortal
- onClick={() => openAlertModal(eventModalProps)}
- onDismiss={() => this.dismissAlert(DISMISSED_EVENT_VIS_ALERT, 'hasDismissedEvents')}
- message="Events may not be visible at all times."
+ title="Zoom in to see imagery for this layer"
+ messageTitle={layer}
+ message="Imagery is not available at this zoom level."
+ onDismiss={() => dismissAlert(DISSMISSED_DDV_ZOOM_ALERT, 'hasDismissedDDVZoom')}
+ onClick={openZoomAlertModal}
/>
- )}
- {showCompareAlert && (
+ ))}
+ { showDDVLocationAlert &&
+ ddvLocationAlerts.map((layer) => (
<AlertUtil
+ key={`ddv-location-${layer}`}
+ id={`granule-alert-${`${layer}`.replace(/[^a-z0-9_-]/gi, '-')}`}
isOpen
noPortal
- onClick={() => openAlertModal(compareModalProps)}
- onDismiss={() => this.dismissAlert(DISMISSED_COMPARE_ALERT, 'hasDismissedCompare')}
- message="You are now in comparison mode."
+ title="Try moving the map or select a different date in the layer's settings."
+ messageTitle={layer}
+ message="Imagery is not available at this location or date."
+ onDismiss={() => dismissAlert(DISSMISSED_DDV_LOCATION_ALERT, 'hasDismissedDDVLocation')}
+ onClick={openGranuleAlertModal}
/>
- )}
- {isVectorZoomAlertPresent && (
- <AlertUtil
- isOpen
- noPortal
- onClick={() => openAlertModal(vectorModalProps)}
- onDismiss={dismissVectorZoomAlert}
- message="Vector features may not be clickable at all zoom levels."
- />
- )}
- {isVectorExceededAlertPresent && (
- <AlertUtil
- isOpen
- noPortal
- onDismiss={dismissVectorExceededAlert}
- message="Too many results at selected point. Zoom in map to see more individual points."
- />
- )}
- {showAnimationAlert && (
- <AlertUtil
- isOpen
- noPortal
- icon="info-circle"
- message="Some animations may use a large amount of data (>100MB)"
- onDismiss={() => {}}
- />
- )}
- {showDDVZoomAlert &&
- ddvZoomAlerts.map((layer) => (
- <AlertUtil
- key={`ddv-zoom-${layer}`}
- id={`zoom-alert-${`${layer}`.replace(/[^a-z0-9_-]/gi, '-')}`}
- isOpen
- noPortal
- title="Zoom in to see imagery for this layer"
- messageTitle={layer}
- message="Imagery is not available at this zoom level."
- onDismiss={() => this.dismissAlert(DISSMISSED_DDV_ZOOM_ALERT, 'hasDismissedDDVZoom')}
- onClick={openZoomAlertModal}
- />
- ))}
- { showDDVLocationAlert &&
- ddvLocationAlerts.map((layer) => (
- <AlertUtil
- key={`ddv-location-${layer}`}
- id={`granule-alert-${`${layer}`.replace(/[^a-z0-9_-]/gi, '-')}`}
- isOpen
- noPortal
- title="Try moving the map or select a different date in the layer's settings."
- messageTitle={layer}
- message="Imagery is not available at this location or date."
- onDismiss={() => this.dismissAlert(DISSMISSED_DDV_LOCATION_ALERT, 'hasDismissedDDVLocation')}
- onClick={openGranuleAlertModal}
- />
- ))}
- </>
- );
- }
+ ))}
+ </>
+ );
}
+
const mapDispatchToProps = (dispatch) => ({
openAlertModal: ({ id, props }) => {
dispatch(openCustomContent(id, props));