UNPKG

redux-signal

Version:

A scalable solution for modals using React and Redux

192 lines (151 loc) 7.16 kB
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; }; 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; }; }(); 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; } import React from 'react'; import PropTypes from 'prop-types'; import { connect } from 'react-redux'; import { Map, List } from 'immutable'; import { getModal, getSignal } from './selectors'; import { getSignalModalId } from './utils'; import { destroySignal, signalEvent, setModalState, feedbackQueueShift } from './actions'; import * as ModalEvents from './constants/SignalEvents'; import * as ModalStates from './constants/ModalStates'; var createContainer = function createContainer(Modal) { var SignalContainer = function (_React$Component) { _inherits(SignalContainer, _React$Component); function SignalContainer() { _classCallCheck(this, SignalContainer); return _possibleConstructorReturn(this, (SignalContainer.__proto__ || Object.getPrototypeOf(SignalContainer)).apply(this, arguments)); } _createClass(SignalContainer, [{ key: 'componentWillMount', value: function componentWillMount() { this.props.updateModals(); } }, { key: 'componentWillReceiveProps', value: function componentWillReceiveProps(nextProps) { nextProps.updateModals(); nextProps.handleEventFeedback(); } }, { key: 'render', value: function render() { var _props = this.props, modals = _props.modals, rawModal = _props.rawModal, onModalExited = _props.onModalExited, onModalClose = _props.onModalClose, onModalEvent = _props.onModalEvent; return modals.map(function (modal) { var id = modal.get('id'); var currentRawModal = rawModal.get(id); return React.createElement(Modal, { key: 'signal-' + id, event: onModalEvent, destroy: function destroy() { return onModalExited(id); }, close: function close() { return onModalClose(id); }, modal: _extends({}, modal.toJS(), currentRawModal) }); }); } }]); return SignalContainer; }(React.Component); SignalContainer.propTypes = { updateModals: PropTypes.func.isRequired, handleEventFeedback: PropTypes.func.isRequired, onModalExited: PropTypes.func.isRequired, onModalClose: PropTypes.func.isRequired, onModalEvent: PropTypes.func.isRequired, modals: PropTypes.instanceOf(List), rawModal: PropTypes.instanceOf(Map) }; var mapStateToProps = function mapStateToProps(state) { var modals = getSignal(state); var rawModal = Map(modals.map(function (modal) { return [modal.get('id'), getModal(getSignalModalId(modal.get('id')))(state)]; })); var eventFeedback = Map(modals.map(function (modal) { var feedback = null; var eventQueueId = modal.get('eventHandlerId'); if (eventQueueId) { var events = state.signal.getIn(['signal', 'feedbackQueue', eventQueueId]); if (events && events.size > 0) { feedback = events.first(); } } return [modal.get('id'), feedback]; })); return { eventFeedback: eventFeedback, modals: modals, rawModal: rawModal }; }; var mapDispatchToProps = function mapDispatchToProps(dispatch) { return { dispatch: dispatch, onModalExited: function onModalExited(modalId) { dispatch(destroySignal(modalId)); }, onModalClose: function onModalClose(modalId) { dispatch(signalEvent(modalId, ModalEvents.CLOSE)); dispatch(setModalState(modalId, ModalStates.DESTROYED)); } }; }; var mergeProps = function mergeProps(stateProps, dispatchProps) { var eventFeedback = stateProps.eventFeedback, modals = stateProps.modals, rawModal = stateProps.rawModal; var dispatch = dispatchProps.dispatch; return _extends({}, stateProps, dispatchProps, { handleEventFeedback: function handleEventFeedback() { modals.forEach(function (modal) { var event = eventFeedback.get(modal.get('id')); if (!event) { return; } var modalId = modal.get('id'); dispatch(feedbackQueueShift(modal.get('eventHandlerId'))); // By default, BTN_* events trigger modal close if (event.type.startsWith('BTN_')) { dispatch(signalEvent(modalId, ModalEvents.CLOSE)); dispatch(setModalState(modalId, ModalStates.DESTROYED)); } }); }, onModalEvent: function onModalEvent(modal, eventType) { var modalId = modal.id; if (modal.eventHandlerId) { dispatch(signalEvent(modalId, eventType)); } else { // If we do not have an event handler, just close the modal on any button if (eventType.startsWith('BTN_')) { dispatch(setModalState(modalId, ModalStates.DESTROYED)); } } }, updateModals: function updateModals() { modals.forEach(function (modal) { var modalId = modal.get('id'); var state = rawModal.getIn([modalId]).state; switch (state) { case ModalStates.CREATED: dispatch(setModalState(modalId, ModalStates.VISIBLE)); break; } }); } }); }; return connect(mapStateToProps, mapDispatchToProps, mergeProps)(SignalContainer); }; export default createContainer;