|
| 1 | +import React, { Component } from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import { canUseDOM } from 'exenv'; |
| 4 | +import { connect } from 'react-redux'; |
| 5 | +import HeaderSystemMessagesDropdown from '../../components/widgets/HeaderSystemMessagesDropdown'; |
| 6 | +import { readyActiveSystemMessagesSelector, fetchManyUserStatus } from '../../redux/selectors/systemMessages'; |
| 7 | +import FetchManyResourceRenderer from '../../components/helpers/FetchManyResourceRenderer'; |
| 8 | +import { fetchAllUserMessages } from '../../redux/modules/systemMessages'; |
| 9 | + |
| 10 | +class HeaderSystemMessagesContainer extends Component { |
| 11 | + state = { isOpen: false }; |
| 12 | + |
| 13 | + static loadAsync = (params, dispatch) => Promise.all([dispatch(fetchAllUserMessages)]); |
| 14 | + |
| 15 | + // |
| 16 | + // Monitor clicking and hide the notifications panel when the user clicks sideways |
| 17 | + |
| 18 | + componentWillMount = () => { |
| 19 | + this.props.loadAsync(); |
| 20 | + this.lastClick = 0; |
| 21 | + if (canUseDOM) { |
| 22 | + window.addEventListener('click', () => this.clickAnywhere()); |
| 23 | + } |
| 24 | + }; |
| 25 | + |
| 26 | + componentWillUnMount = () => { |
| 27 | + if (canUseDOM) { |
| 28 | + window.removeEventListener(() => this.clickAnywhere()); |
| 29 | + } |
| 30 | + }; |
| 31 | + |
| 32 | + clickAnywhere = () => { |
| 33 | + if (this.state.isOpen === true && this.isClickingSomewhereElse()) { |
| 34 | + this.close(); |
| 35 | + } |
| 36 | + }; |
| 37 | + |
| 38 | + markClick = () => { |
| 39 | + this.lastClick = Date.now(); |
| 40 | + }; |
| 41 | + |
| 42 | + /** |
| 43 | + * Determines, whether this click is on the container or not - a 50ms tolerance |
| 44 | + * between now and the time of last click on the container is defined. |
| 45 | + */ |
| 46 | + isClickingSomewhereElse = () => Date.now() - this.lastClick > 50; |
| 47 | + |
| 48 | + toggleOpen = e => { |
| 49 | + e.preventDefault(); |
| 50 | + this.state.isOpen ? this.close() : this.open(); |
| 51 | + this.markClick(); |
| 52 | + }; |
| 53 | + |
| 54 | + close = () => { |
| 55 | + this.setState({ isOpen: false }); |
| 56 | + }; |
| 57 | + |
| 58 | + open = () => this.setState({ isOpen: true }); |
| 59 | + |
| 60 | + render() { |
| 61 | + const { systemMessages, fetchStatus } = this.props; |
| 62 | + const { isOpen } = this.state; |
| 63 | + |
| 64 | + return ( |
| 65 | + <FetchManyResourceRenderer fetchManyStatus={fetchStatus} loading={<span />}> |
| 66 | + {() => ( |
| 67 | + <HeaderSystemMessagesDropdown |
| 68 | + isOpen={isOpen} |
| 69 | + toggleOpen={this.toggleOpen} |
| 70 | + markClick={this.markClick} |
| 71 | + systemMessages={systemMessages} |
| 72 | + /> |
| 73 | + )} |
| 74 | + </FetchManyResourceRenderer> |
| 75 | + ); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +HeaderSystemMessagesContainer.propTypes = { |
| 80 | + systemMessages: PropTypes.array.isRequired, |
| 81 | + fetchStatus: PropTypes.string, |
| 82 | + loadAsync: PropTypes.func.isRequired, |
| 83 | +}; |
| 84 | + |
| 85 | +export default connect( |
| 86 | + state => ({ |
| 87 | + fetchStatus: fetchManyUserStatus(state), |
| 88 | + systemMessages: readyActiveSystemMessagesSelector(state), |
| 89 | + }), |
| 90 | + dispatch => ({ |
| 91 | + loadAsync: () => HeaderSystemMessagesContainer.loadAsync({}, dispatch), |
| 92 | + }) |
| 93 | +)(HeaderSystemMessagesContainer); |
0 commit comments