2020-02-24 21:56:07 +00:00
|
|
|
import { Fragment } from 'react'
|
|
|
|
import { injectIntl, FormattedMessage, defineMessages } from 'react-intl'
|
|
|
|
import classNames from 'classnames/bind'
|
|
|
|
import { openModal } from '../../actions/modal'
|
|
|
|
import { cancelReplyCompose } from '../../actions/compose'
|
2019-08-07 06:02:36 +01:00
|
|
|
|
|
|
|
const messages = defineMessages({
|
|
|
|
confirm: { id: 'confirmations.delete.confirm', defaultMessage: 'Delete' },
|
2020-02-29 15:42:47 +00:00
|
|
|
delete: { id: 'confirmations.delete.message', defaultMessage: 'Are you sure you want to delete this status?' },
|
2020-02-24 21:56:07 +00:00
|
|
|
})
|
2019-08-07 06:02:36 +01:00
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
2019-08-23 00:05:20 +01:00
|
|
|
composeId: state.getIn(['compose', 'id']),
|
2019-08-07 06:02:36 +01:00
|
|
|
composeText: state.getIn(['compose', 'text']),
|
2020-02-24 21:56:07 +00:00
|
|
|
})
|
2019-08-07 06:02:36 +01:00
|
|
|
|
|
|
|
const mapDispatchToProps = (dispatch) => ({
|
|
|
|
onOpenModal(type, opts) {
|
2020-02-24 21:56:07 +00:00
|
|
|
dispatch(openModal(type, opts))
|
2019-08-07 06:02:36 +01:00
|
|
|
},
|
|
|
|
onCancelReplyCompose() {
|
2020-02-24 21:56:07 +00:00
|
|
|
dispatch(cancelReplyCompose())
|
2019-08-07 06:02:36 +01:00
|
|
|
},
|
2020-02-24 21:56:07 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
const cx = classNames.bind(_s)
|
2019-08-07 06:02:36 +01:00
|
|
|
|
2020-02-25 16:04:44 +00:00
|
|
|
export default
|
|
|
|
@connect(mapStateToProps, mapDispatchToProps)
|
2019-08-07 06:02:36 +01:00
|
|
|
@injectIntl
|
|
|
|
class ModalBase extends PureComponent {
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
children: PropTypes.node,
|
|
|
|
onClose: PropTypes.func.isRequired,
|
|
|
|
onOpenModal: PropTypes.func.isRequired,
|
|
|
|
onCancelReplyCompose: PropTypes.func.isRequired,
|
|
|
|
intl: PropTypes.object.isRequired,
|
2019-08-23 00:05:20 +01:00
|
|
|
composeId: PropTypes.string,
|
2019-08-07 06:02:36 +01:00
|
|
|
composeText: PropTypes.string,
|
|
|
|
type: PropTypes.string,
|
2020-02-24 21:56:07 +00:00
|
|
|
}
|
2019-08-07 06:02:36 +01:00
|
|
|
|
|
|
|
state = {
|
|
|
|
revealed: !!this.props.children,
|
2020-02-24 21:56:07 +00:00
|
|
|
}
|
2019-08-07 06:02:36 +01:00
|
|
|
|
2020-02-24 21:56:07 +00:00
|
|
|
activeElement = this.state.revealed ? document.activeElement : null
|
2019-08-07 06:02:36 +01:00
|
|
|
|
|
|
|
handleKeyUp = (e) => {
|
|
|
|
if ((e.key === 'Escape' || e.key === 'Esc' || e.keyCode === 27)
|
|
|
|
&& !!this.props.children) {
|
2020-02-24 21:56:07 +00:00
|
|
|
this.handleOnClose()
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleOnClose = () => {
|
2020-02-24 21:56:07 +00:00
|
|
|
const { onOpenModal, composeText, composeId, onClose, intl, type, onCancelReplyCompose } = this.props
|
2019-08-07 06:02:36 +01:00
|
|
|
|
2019-08-23 00:05:20 +01:00
|
|
|
if (!composeId && composeText && type == 'COMPOSE') {
|
2019-08-07 06:02:36 +01:00
|
|
|
onOpenModal('CONFIRM', {
|
2020-02-29 15:42:47 +00:00
|
|
|
message: intl.formatMessage(messages.delete),
|
2019-08-07 06:02:36 +01:00
|
|
|
confirm: intl.formatMessage(messages.confirm),
|
|
|
|
onConfirm: () => onCancelReplyCompose(),
|
|
|
|
onCancel: () => onOpenModal('COMPOSE'),
|
2020-02-24 21:56:07 +00:00
|
|
|
})
|
2019-08-07 06:02:36 +01:00
|
|
|
} else {
|
2020-02-24 21:56:07 +00:00
|
|
|
onClose()
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
2020-02-24 21:56:07 +00:00
|
|
|
}
|
2019-08-07 06:02:36 +01:00
|
|
|
|
|
|
|
componentDidMount () {
|
2020-02-24 21:56:07 +00:00
|
|
|
window.addEventListener('keyup', this.handleKeyUp, false)
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps (nextProps) {
|
|
|
|
if (!!nextProps.children && !this.props.children) {
|
2020-02-24 21:56:07 +00:00
|
|
|
this.activeElement = document.activeElement
|
2019-08-07 06:02:36 +01:00
|
|
|
|
2020-02-24 21:56:07 +00:00
|
|
|
this.getSiblings().forEach(sibling => sibling.setAttribute('inert', true))
|
2019-08-07 06:02:36 +01:00
|
|
|
} else if (!nextProps.children) {
|
2020-02-24 21:56:07 +00:00
|
|
|
this.setState({ revealed: false })
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!nextProps.children && !!this.props.children) {
|
2020-02-24 21:56:07 +00:00
|
|
|
this.activeElement.focus()
|
|
|
|
this.activeElement = null
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate (prevProps) {
|
|
|
|
if (!this.props.children && !!prevProps.children) {
|
2020-02-24 21:56:07 +00:00
|
|
|
this.getSiblings().forEach(sibling => sibling.removeAttribute('inert'))
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.props.children) {
|
|
|
|
requestAnimationFrame(() => {
|
2020-02-24 21:56:07 +00:00
|
|
|
this.setState({ revealed: true })
|
|
|
|
})
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount () {
|
2020-02-24 21:56:07 +00:00
|
|
|
window.removeEventListener('keyup', this.handleKeyUp)
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getSiblings = () => {
|
2020-02-24 21:56:07 +00:00
|
|
|
return Array(...this.node.parentElement.childNodes).filter(node => node !== this.node)
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
setRef = ref => {
|
2020-02-24 21:56:07 +00:00
|
|
|
this.node = ref
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
2020-02-24 21:56:07 +00:00
|
|
|
const { children } = this.props
|
|
|
|
|
|
|
|
const visible = !!children
|
2019-08-07 06:02:36 +01:00
|
|
|
|
2020-02-24 21:56:07 +00:00
|
|
|
const containerClasses = cx({
|
|
|
|
default: 1,
|
|
|
|
z4: 1,
|
2020-03-08 16:46:00 +00:00
|
|
|
height100PC: visible,
|
|
|
|
width100PC: visible,
|
2020-02-24 21:56:07 +00:00
|
|
|
displayNone: !visible,
|
|
|
|
})
|
2019-08-07 06:02:36 +01:00
|
|
|
|
|
|
|
return (
|
2020-02-24 21:56:07 +00:00
|
|
|
<div ref={this.setRef} className={containerClasses}>
|
|
|
|
{
|
|
|
|
!!visible &&
|
|
|
|
<Fragment>
|
|
|
|
<div
|
|
|
|
role='presentation'
|
2020-03-07 04:53:28 +00:00
|
|
|
className={[_s.default, _s.backgroundColorOpaque, _s.positionFixed, _s.z3, _s.top0, _s.right0, _s.bottom0, _s.left0].join(' ')}
|
2020-02-24 21:56:07 +00:00
|
|
|
onClick={this.handleOnClose}
|
|
|
|
/>
|
|
|
|
<div
|
|
|
|
role='dialog'
|
2020-03-08 16:46:00 +00:00
|
|
|
className={[_s.default, _s.positionFixed, _s.alignItemsCenter, _s.justifyContentCenter, _s.z4, _s.width100PC, _s.height100PC, _s.top0, _s.rightAuto, _s.bottomAuto, _s.left0].join(' ')}
|
2020-02-24 21:56:07 +00:00
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
</Fragment>
|
|
|
|
}
|
2019-08-07 06:02:36 +01:00
|
|
|
</div>
|
2020-02-24 21:56:07 +00:00
|
|
|
)
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|