UNPKG

react-toastify-redux

Version:
166 lines (141 loc) 8.04 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.ToastContainer = undefined; var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; 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; }; }(); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _reactToastify = require('react-toastify'); var _reactRedux = require('react-redux'); var _compare = require('./utils/compare'); var _compare2 = _interopRequireDefault(_compare); var _actions = require('./actions'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 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; } 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; } var __rest = undefined && undefined.__rest || function (s, e) { var t = {}; for (var p in s) { if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; }if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0) t[p[i]] = s[p[i]]; }return t; }; var ToastContainer = exports.ToastContainer = function (_React$Component) { _inherits(ToastContainer, _React$Component); function ToastContainer() { _classCallCheck(this, ToastContainer); var _this = _possibleConstructorReturn(this, (ToastContainer.__proto__ || Object.getPrototypeOf(ToastContainer)).apply(this, arguments)); _this._toastIds = {}; _this.getCustomComponentProps = function (toastItem) { var id = toastItem.id, message = toastItem.message, _toastItem$title = toastItem.title, title = _toastItem$title === undefined ? '' : _toastItem$title; return { id: id, message: message, title: title }; }; _this.getToastOptions = function (toastItem) { var id = toastItem.id, message = toastItem.message, title = toastItem.title, renderDefaultComponent = toastItem.renderDefaultComponent, options = __rest(toastItem, ["id", "message", "title", "renderDefaultComponent"]); return Object.assign({ onClose: function onClose() { return _this.onCloseHandler(id); } }, options); }; _this.renderToasts = function (nextProps) { nextProps.toastList.forEach(function (toastItem) { var _toastItem$renderDefa = toastItem.renderDefaultComponent, renderDefaultComponent = _toastItem$renderDefa === undefined ? false : _toastItem$renderDefa; // new toast if (!(toastItem.id in _this._toastIds)) { _this._toastIds[toastItem.id] = nextProps.toastComponent && !renderDefaultComponent ? (0, _reactToastify.toast)(_react2.default.createElement(nextProps.toastComponent, _this.getCustomComponentProps(toastItem)), _this.getToastOptions(toastItem)) : (0, _reactToastify.toast)(toastItem.message, _this.getToastOptions(toastItem)); } // update toast var foundToast = _this.props.toastList.find(function (toast) { return toast.id === toastItem.id; }); if (foundToast && (!(0, _compare2.default)(toastItem, foundToast) || nextProps.toastComponent !== _this.props.toastComponent)) { _reactToastify.toast.update(_this._toastIds[toastItem.id], Object.assign({}, _this.getToastOptions(toastItem), { render: nextProps.toastComponent && !renderDefaultComponent ? _react2.default.createElement(nextProps.toastComponent, _this.getCustomComponentProps(toastItem)) : toastItem.message })); } }); // delete toast _this.props.toastList.filter(function (toastItem) { var foundItem = nextProps.toastList.find(function (nextToastItem) { return nextToastItem.id === toastItem.id; }); return !foundItem && toastItem.id in _this._toastIds; }).forEach(function (doomedToast) { return _this.closeToast(doomedToast.id); }); }; _this.closeToast = function (storageToastId) { /* istanbul ignore next */ var _a = _this._toastIds, _b = storageToastId, _toastId = _a[_b], toastIds = __rest(_a, [(typeof _b === 'undefined' ? 'undefined' : _typeof(_b)) === "symbol" ? _b : _b + ""]); _this._toastIds = toastIds; _reactToastify.toast.dismiss(_toastId); }; _this.onCloseHandler = function (storageToastId) { _this.closeToast(storageToastId); _this.props.dismiss(storageToastId); }; return _this; } _createClass(ToastContainer, [{ key: 'componentDidMount', value: function componentDidMount() { this.renderToasts(this.props); } }, { key: 'componentWillUnmount', value: function componentWillUnmount() { this.props.dismiss(); this._toastIds = {}; } }, { key: 'componentWillReceiveProps', value: function componentWillReceiveProps(nextProps) { this.renderToasts(nextProps); } }, { key: 'shouldComponentUpdate', value: function shouldComponentUpdate(nextProps) { return this.props !== nextProps; } }, { key: 'render', value: function render() { var _a = this.props, dismiss = _a.dismiss, toastList = _a.toastList, toastComponent = _a.toastComponent, rest = __rest(_a, ["dismiss", "toastList", "toastComponent"]); return _react2.default.createElement(_reactToastify.ToastContainer, rest); } }]); return ToastContainer; }(_react2.default.Component); var mapStateToProps = function mapStateToProps(state) { return { toastList: state.toasts }; }; var mapDispatchToProps = function mapDispatchToProps(dispatch) { return { dismiss: function dismiss(id) { return dispatch((0, _actions.dismiss)(id)); } }; }; exports.default = (0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps)(ToastContainer);