Skip to content

Commit a2afb24

Browse files
committed
fixed bugs
1 parent 4ef5ad9 commit a2afb24

File tree

11 files changed

+223
-119
lines changed

11 files changed

+223
-119
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
node_modules/
2-
lib/

lib/actions.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.destroyAllAlerts = exports.dismissAllAlerts = exports.destroyAlert = exports.dismissAlert = exports.createAlert = exports.initializeAlert = undefined;
7+
8+
var _constants = require('./constants');
9+
10+
var alertConstants = _interopRequireWildcard(_constants);
11+
12+
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
13+
14+
var initializeAlert = exports.initializeAlert = function initializeAlert(alertName) {
15+
return { type: alertConstants.INITIALIZE_ALERT, alertName: alertName };
16+
};
17+
18+
var createAlert = exports.createAlert = function createAlert(alertName) {
19+
return { type: alertConstants.CREATE_ALERT, alertName: alertName };
20+
};
21+
22+
var dismissAlert = exports.dismissAlert = function dismissAlert(alertName) {
23+
return { type: alertConstants.DISMISS_ALERT, alertName: alertName };
24+
};
25+
26+
var destroyAlert = exports.destroyAlert = function destroyAlert(alertName) {
27+
return { type: alertConstants.DESTROY_ALERT, alertName: alertName };
28+
};
29+
30+
var dismissAllAlerts = exports.dismissAllAlerts = function dismissAllAlerts() {
31+
return { type: alertConstants.DISMISS_ALL_ALERTS };
32+
};
33+
34+
var destroyAllAlerts = exports.destroyAllAlerts = function destroyAllAlerts() {
35+
return { type: alertConstants.DESTROY_ALL_ALERTS };
36+
};

lib/alertContainer.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
7+
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
8+
9+
var _react = require('react');
10+
11+
var _react2 = _interopRequireDefault(_react);
12+
13+
var _reactRedux = require('react-redux');
14+
15+
var _redux = require('redux');
16+
17+
var _actions = require('./actions');
18+
19+
var alertActions = _interopRequireWildcard(_actions);
20+
21+
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
22+
23+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
24+
25+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
26+
27+
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
28+
29+
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
30+
31+
var createAlert = function createAlert(config) {
32+
return function (WrappedComponent) {
33+
var alertName = config.alertName;
34+
35+
var AlertContainer = function (_Component) {
36+
_inherits(AlertContainer, _Component);
37+
38+
function AlertContainer() {
39+
_classCallCheck(this, AlertContainer);
40+
41+
return _possibleConstructorReturn(this, Object.getPrototypeOf(AlertContainer).apply(this, arguments));
42+
}
43+
44+
_createClass(AlertContainer, [{
45+
key: 'componentDidMount',
46+
value: function componentDidMount() {
47+
this.props.actions.initializeAlert(alertName);
48+
}
49+
}, {
50+
key: 'componentWillUnmount',
51+
value: function componentWillUnmount() {
52+
this.props.actions.destoryAlert(alertName);
53+
}
54+
}, {
55+
key: 'render',
56+
value: function render() {
57+
if (!this.props.isVisible) return false;
58+
return _react2.default.createElement(WrappedComponent, null);
59+
}
60+
}]);
61+
62+
return AlertContainer;
63+
}(_react.Component);
64+
65+
function mapStateToProps(state) {
66+
return { isVisible: state.alerts ? state.alerts[alertName] : false };
67+
};
68+
69+
function mapDispatchToProps(dispatch) {
70+
return { actions: (0, _redux.bindActionCreators)(alertActions, dispatch) };
71+
};
72+
return (0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps)(AlertContainer);
73+
};
74+
};
75+
76+
exports.default = createAlert;

lib/constants.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
var INITIALIZE_ALERT = exports.INITIALIZE_ALERT = 'redux-alerts/INITIALIZE_ALERT';
7+
var CREATE_ALERT = exports.CREATE_ALERT = 'redux-alerts/CREATE_ALERT';
8+
var DISMISS_ALERT = exports.DISMISS_ALERT = 'redux-alerts/DISMISS_ALERT';
9+
var DISMISS_ALL_ALERTS = exports.DISMISS_ALL_ALERTS = 'redux-alerts/DISMISS_ALL_ALERTS';
10+
var DESTROY_ALL_ALERTS = exports.DESTROY_ALL_ALERTS = 'redux-alerts/DESTROY_ALL_ALERTS';
11+
var DESTROY_ALERT = exports.DESTROY_ALERT = 'redux-alerts/DESTROY_ALERT';

lib/index.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
7+
var _actions = require('./actions');
8+
9+
var _actions2 = _interopRequireDefault(_actions);
10+
11+
var _reducer = require('./reducer');
12+
13+
var _reducer2 = _interopRequireDefault(_reducer);
14+
15+
var _constants = require('./constants');
16+
17+
var _constants2 = _interopRequireDefault(_constants);
18+
19+
var _alertContainer = require('./alertContainer');
20+
21+
var _alertContainer2 = _interopRequireDefault(_alertContainer);
22+
23+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
24+
25+
exports.default = {
26+
actions: _actions2.default,
27+
alertReducer: _reducer2.default,
28+
constants: _constants2.default,
29+
createAlert: _alertContainer2.default
30+
};

lib/reducer.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
7+
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
8+
9+
var _behaviors;
10+
11+
var _constants = require('./constants');
12+
13+
var alertTypes = _interopRequireWildcard(_constants);
14+
15+
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
16+
17+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
18+
19+
var behaviors = (_behaviors = {}, _defineProperty(_behaviors, alertTypes.CREATE_ALERT, function (state, action) {
20+
return true;
21+
}), _defineProperty(_behaviors, alertTypes.DISMISS_ALERT, function (state, action) {
22+
return false;
23+
}), _behaviors);
24+
25+
var initialAlertState = false;
26+
27+
var reducer = function reducer(state, action) {
28+
var behavior = behaviors[action.type];
29+
return behavior ? behavior(state, action) : state;
30+
};
31+
32+
var alertReducer = function alertReducer() {
33+
var state = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
34+
var action = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
35+
var type = action.type;
36+
var alertName = action.alertName;
37+
38+
if (state === undefined) return state;
39+
if (type === alertTypes.INITIALIZE_ALERT) {
40+
return _extends({}, state, _defineProperty({}, alertName, false));
41+
}
42+
if (type === alertTypes.DISMISS_ALL_ALERTS) {
43+
return Object.keys(state).reduce(function (acc, alert) {
44+
var prev = Object.assign({}, acc, _defineProperty({}, alert, initialAlertState));
45+
return prev;
46+
}, {});
47+
}
48+
49+
if (state[alertName] === undefined) return state;
50+
if (type === alertTypes.DESTROY_ALERT) {
51+
return Object.keys(state).reduce(function (acc, alert) {
52+
var prev = alert === alertName ? acc : _extends({}, acc, _defineProperty({}, alert, state[alert]));
53+
return prev;
54+
}, {});
55+
}
56+
return Object.assign({}, state, _defineProperty({}, alertName, reducer(state[alertName], action)));
57+
};
58+
exports.default = alertReducer;

npm-debug.log

Lines changed: 0 additions & 114 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-redux-alerts",
3-
"version": "1.3.1",
3+
"version": "1.3.11",
44
"description": "A lightweight library for creating keyed alerts in a Redux app",
55
"main": "./lib/index.js",
66
"scripts": {

src/alertContainer.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,15 @@ const createAlert = config => WrappedComponent => {
2222
}
2323
}
2424

25-
const mapStateToProps = state => { isVisible: state.alerts[alertName] };
26-
const mapDispatchToProps = dispatch => { actions: bindActionCreators(alertActions, dispatch) };
25+
function mapStateToProps (state) {
26+
return { isVisible: state.alerts ? state.alerts[alertName] : false };
27+
};
28+
29+
function mapDispatchToProps (dispatch) {
30+
return { actions: bindActionCreators(alertActions, dispatch) };
31+
};
2732
return connect(mapStateToProps, mapDispatchToProps)(AlertContainer);
33+
2834
};
2935

3036
export default createAlert;

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import actions from './actions';
22
import alertReducer from './reducer';
33
import constants from './constants';
4-
import createAlert from '/alertContainer';
4+
import createAlert from './alertContainer';
55

66
export default {
77
actions,

src/reducer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const reducer = (state, action) => {
1919

2020
const alertReducer = (state = {}, action = {}) => {
2121
const {type, alertName } = action;
22+
if (state === undefined) return state;
2223
if (type === alertTypes.INITIALIZE_ALERT) {
2324
return {
2425
...state,
@@ -32,6 +33,7 @@ const alertReducer = (state = {}, action = {}) => {
3233
return prev;
3334
}, {});
3435
}
36+
3537
if (state[alertName] === undefined) return state;
3638
if (type === alertTypes.DESTROY_ALERT) {
3739
return Object.keys(state).reduce((acc, alert) => {

0 commit comments

Comments
 (0)