Skip to content

Commit 0191f97

Browse files
authored
Merge pull request #3867 from benjiwheeler/alerts-revise
Revise alerts code to clarify and reorganize alert type handling
2 parents 74d036d + 0af13c4 commit 0191f97

File tree

3 files changed

+40
-22
lines changed

3 files changed

+40
-22
lines changed

src/containers/alerts.jsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
33
import {connect} from 'react-redux';
4-
import {AlertTypes} from '../lib/alerts/index.jsx';
54

65
import {
7-
closeAlert
6+
closeAlert,
7+
filterPopupAlerts
88
} from '../reducers/alerts';
99

1010
import AlertsComponent from '../components/alerts/alerts.jsx';
@@ -16,10 +16,7 @@ const Alerts = ({
1616
}) => (
1717
<AlertsComponent
1818
// only display standard and extension alerts here
19-
alertsList={alertsList.filter(curAlert => (
20-
curAlert.alertType === AlertTypes.STANDARD ||
21-
curAlert.alertType === AlertTypes.EXTENSION
22-
))}
19+
alertsList={filterPopupAlerts(alertsList)}
2320
className={className}
2421
onCloseAlert={onCloseAlert}
2522
/>

src/containers/inline-messages.jsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
33
import {connect} from 'react-redux';
4-
import {AlertTypes} from '../lib/alerts/index.jsx';
4+
5+
import {
6+
filterInlineAlerts
7+
} from '../reducers/alerts';
58

69
import InlineMessageComponent from '../components/alerts/inline-message.jsx';
710

@@ -13,9 +16,7 @@ const InlineMessages = ({
1316
return null;
1417
}
1518
// only display inline alerts here
16-
const inlineAlerts = alertsList.filter(curAlert => (
17-
curAlert.alertType === AlertTypes.INLINE
18-
));
19+
const inlineAlerts = filterInlineAlerts(alertsList);
1920
if (!inlineAlerts || !inlineAlerts.length) {
2021
return null;
2122
}

src/reducers/alerts.js

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,47 @@ import alertsData from '../lib/alerts/index.jsx';
22
import {AlertTypes, AlertLevels} from '../lib/alerts/index.jsx';
33
import extensionData from '../lib/libraries/extensions/index.jsx';
44

5-
const SHOW_STANDARD_ALERT = 'scratch-gui/alerts/SHOW_STANDARD_ALERT';
5+
const SHOW_ALERT = 'scratch-gui/alerts/SHOW_ALERT';
66
const SHOW_EXTENSION_ALERT = 'scratch-gui/alerts/SHOW_EXTENSION_ALERT';
77
const CLOSE_ALERT = 'scratch-gui/alerts/CLOSE_ALERT';
88
const CLOSE_ALERTS_WITH_ID = 'scratch-gui/alerts/CLOSE_ALERTS_WITH_ID';
99

10+
/**
11+
* Initial state of alerts reducer
12+
*
13+
* {bool} visible - whether the alerts are visible
14+
* {array} alertsList - list of alerts, each with properties:
15+
* * alertType (required): one of AlertTypes
16+
* * closeButton (optional): bool indicating that we should show close button
17+
* * content (optional): react element (a <FormattedMessage />)
18+
* * extentionId (optional): id string that identifies the extension
19+
* * iconURL (optional): string
20+
* * level (required): string, one of AlertLevels
21+
* * message (optional): string
22+
* * showReconnect (optional): bool
23+
*/
1024
const initialState = {
1125
visible: true,
12-
// list of alerts, each with properties:
13-
// * alert type (required): one of AlertTypes
14-
// * closeButton (optional): bool indicating that we should show close button
15-
// * content (optional): react element (a <FormattedMessage />)
16-
// * extentionId (optional): id string that identifies the extension
17-
// * iconURL (optional): string
18-
// * level (required): string, one of AlertLevels
19-
// * message (optional): string
20-
// * showReconnect (optional): bool
2126
alertsList: []
2227
};
2328

29+
const filterPopupAlerts = alertsList => (
30+
alertsList.filter(curAlert => (
31+
curAlert.alertType === AlertTypes.STANDARD ||
32+
curAlert.alertType === AlertTypes.EXTENSION
33+
))
34+
);
35+
36+
const filterInlineAlerts = alertsList => (
37+
alertsList.filter(curAlert => (
38+
curAlert.alertType === AlertTypes.INLINE
39+
))
40+
);
41+
2442
const reducer = function (state, action) {
2543
if (typeof state === 'undefined') state = initialState;
2644
switch (action.type) {
27-
case SHOW_STANDARD_ALERT: { // also will show inline alerts
45+
case SHOW_ALERT: { // intended to show standard and inline alerts, but not extensions
2846
const alertId = action.alertId;
2947
if (alertId) {
3048
const newAlert = {
@@ -134,7 +152,7 @@ const closeAlertsWithId = function (alertId) {
134152
*/
135153
const showStandardAlert = function (alertId) {
136154
return {
137-
type: SHOW_STANDARD_ALERT,
155+
type: SHOW_ALERT,
138156
alertId
139157
};
140158
};
@@ -177,6 +195,8 @@ export {
177195
reducer as default,
178196
initialState as alertsInitialState,
179197
closeAlert,
198+
filterInlineAlerts,
199+
filterPopupAlerts,
180200
showAlertWithTimeout,
181201
showExtensionAlert,
182202
showStandardAlert

0 commit comments

Comments
 (0)