Skip to content
Snippets Groups Projects
Unverified Commit 0191f978 authored by Benjamin Wheeler's avatar Benjamin Wheeler Committed by GitHub
Browse files

Merge pull request #3867 from benjiwheeler/alerts-revise

Revise alerts code to clarify and reorganize alert type handling
parents 74d036d8 0af13c46
No related branches found
No related tags found
No related merge requests found
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import {connect} from 'react-redux'; import {connect} from 'react-redux';
import {AlertTypes} from '../lib/alerts/index.jsx';
import { import {
closeAlert closeAlert,
filterPopupAlerts
} from '../reducers/alerts'; } from '../reducers/alerts';
import AlertsComponent from '../components/alerts/alerts.jsx'; import AlertsComponent from '../components/alerts/alerts.jsx';
...@@ -16,10 +16,7 @@ const Alerts = ({ ...@@ -16,10 +16,7 @@ const Alerts = ({
}) => ( }) => (
<AlertsComponent <AlertsComponent
// only display standard and extension alerts here // only display standard and extension alerts here
alertsList={alertsList.filter(curAlert => ( alertsList={filterPopupAlerts(alertsList)}
curAlert.alertType === AlertTypes.STANDARD ||
curAlert.alertType === AlertTypes.EXTENSION
))}
className={className} className={className}
onCloseAlert={onCloseAlert} onCloseAlert={onCloseAlert}
/> />
......
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import {connect} from 'react-redux'; import {connect} from 'react-redux';
import {AlertTypes} from '../lib/alerts/index.jsx';
import {
filterInlineAlerts
} from '../reducers/alerts';
import InlineMessageComponent from '../components/alerts/inline-message.jsx'; import InlineMessageComponent from '../components/alerts/inline-message.jsx';
...@@ -13,9 +16,7 @@ const InlineMessages = ({ ...@@ -13,9 +16,7 @@ const InlineMessages = ({
return null; return null;
} }
// only display inline alerts here // only display inline alerts here
const inlineAlerts = alertsList.filter(curAlert => ( const inlineAlerts = filterInlineAlerts(alertsList);
curAlert.alertType === AlertTypes.INLINE
));
if (!inlineAlerts || !inlineAlerts.length) { if (!inlineAlerts || !inlineAlerts.length) {
return null; return null;
} }
......
...@@ -2,29 +2,47 @@ import alertsData from '../lib/alerts/index.jsx'; ...@@ -2,29 +2,47 @@ import alertsData from '../lib/alerts/index.jsx';
import {AlertTypes, AlertLevels} from '../lib/alerts/index.jsx'; import {AlertTypes, AlertLevels} from '../lib/alerts/index.jsx';
import extensionData from '../lib/libraries/extensions/index.jsx'; import extensionData from '../lib/libraries/extensions/index.jsx';
const SHOW_STANDARD_ALERT = 'scratch-gui/alerts/SHOW_STANDARD_ALERT'; const SHOW_ALERT = 'scratch-gui/alerts/SHOW_ALERT';
const SHOW_EXTENSION_ALERT = 'scratch-gui/alerts/SHOW_EXTENSION_ALERT'; const SHOW_EXTENSION_ALERT = 'scratch-gui/alerts/SHOW_EXTENSION_ALERT';
const CLOSE_ALERT = 'scratch-gui/alerts/CLOSE_ALERT'; const CLOSE_ALERT = 'scratch-gui/alerts/CLOSE_ALERT';
const CLOSE_ALERTS_WITH_ID = 'scratch-gui/alerts/CLOSE_ALERTS_WITH_ID'; const CLOSE_ALERTS_WITH_ID = 'scratch-gui/alerts/CLOSE_ALERTS_WITH_ID';
/**
* Initial state of alerts reducer
*
* {bool} visible - whether the alerts are visible
* {array} alertsList - list of alerts, each with properties:
* * alertType (required): one of AlertTypes
* * closeButton (optional): bool indicating that we should show close button
* * content (optional): react element (a <FormattedMessage />)
* * extentionId (optional): id string that identifies the extension
* * iconURL (optional): string
* * level (required): string, one of AlertLevels
* * message (optional): string
* * showReconnect (optional): bool
*/
const initialState = { const initialState = {
visible: true, visible: true,
// list of alerts, each with properties:
// * alert type (required): one of AlertTypes
// * closeButton (optional): bool indicating that we should show close button
// * content (optional): react element (a <FormattedMessage />)
// * extentionId (optional): id string that identifies the extension
// * iconURL (optional): string
// * level (required): string, one of AlertLevels
// * message (optional): string
// * showReconnect (optional): bool
alertsList: [] alertsList: []
}; };
const filterPopupAlerts = alertsList => (
alertsList.filter(curAlert => (
curAlert.alertType === AlertTypes.STANDARD ||
curAlert.alertType === AlertTypes.EXTENSION
))
);
const filterInlineAlerts = alertsList => (
alertsList.filter(curAlert => (
curAlert.alertType === AlertTypes.INLINE
))
);
const reducer = function (state, action) { const reducer = function (state, action) {
if (typeof state === 'undefined') state = initialState; if (typeof state === 'undefined') state = initialState;
switch (action.type) { switch (action.type) {
case SHOW_STANDARD_ALERT: { // also will show inline alerts case SHOW_ALERT: { // intended to show standard and inline alerts, but not extensions
const alertId = action.alertId; const alertId = action.alertId;
if (alertId) { if (alertId) {
const newAlert = { const newAlert = {
...@@ -134,7 +152,7 @@ const closeAlertsWithId = function (alertId) { ...@@ -134,7 +152,7 @@ const closeAlertsWithId = function (alertId) {
*/ */
const showStandardAlert = function (alertId) { const showStandardAlert = function (alertId) {
return { return {
type: SHOW_STANDARD_ALERT, type: SHOW_ALERT,
alertId alertId
}; };
}; };
...@@ -177,6 +195,8 @@ export { ...@@ -177,6 +195,8 @@ export {
reducer as default, reducer as default,
initialState as alertsInitialState, initialState as alertsInitialState,
closeAlert, closeAlert,
filterInlineAlerts,
filterPopupAlerts,
showAlertWithTimeout, showAlertWithTimeout,
showExtensionAlert, showExtensionAlert,
showStandardAlert showStandardAlert
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment