2020-08-17 21:07:16 +01:00
|
|
|
import React from 'react'
|
2020-08-17 21:59:29 +01:00
|
|
|
import PropTypes from 'prop-types'
|
2020-08-17 21:39:25 +01:00
|
|
|
import { connect } from 'react-redux'
|
2020-03-07 04:53:28 +00:00
|
|
|
import { defineMessages, injectIntl } from 'react-intl'
|
|
|
|
import { addPoll, removePoll } from '../../../actions/compose'
|
2020-02-08 06:12:01 +00:00
|
|
|
import ComposeExtraButton from './compose_extra_button'
|
|
|
|
|
2020-08-17 23:06:22 +01:00
|
|
|
class PollButton extends React.PureComponent {
|
|
|
|
|
|
|
|
handleClick = () => {
|
|
|
|
this.props.onClick()
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { intl, active, unavailable, disabled, small } = this.props
|
|
|
|
|
|
|
|
if (unavailable) return null
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ComposeExtraButton
|
|
|
|
title={intl.formatMessage(active ? messages.remove_poll : messages.title)}
|
|
|
|
disabled={disabled}
|
|
|
|
onClick={this.handleClick}
|
|
|
|
icon='poll'
|
|
|
|
small={small}
|
|
|
|
active={active}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-02-08 06:12:01 +00:00
|
|
|
const messages = defineMessages({
|
|
|
|
add_poll: { id: 'poll_button.add_poll', defaultMessage: 'Add poll' },
|
|
|
|
title: { id: 'poll_button.title', defaultMessage: 'Poll' },
|
|
|
|
remove_poll: { id: 'poll_button.remove_poll', defaultMessage: 'Remove poll' },
|
2020-03-07 04:53:28 +00:00
|
|
|
})
|
2020-02-08 06:12:01 +00:00
|
|
|
|
2020-04-11 23:29:19 +01:00
|
|
|
const mapStateToProps = (state) => ({
|
2020-02-08 06:12:01 +00:00
|
|
|
unavailable: state.getIn(['compose', 'is_uploading']) || (state.getIn(['compose', 'media_attachments']).size > 0),
|
|
|
|
active: state.getIn(['compose', 'poll']) !== null,
|
2020-03-07 04:53:28 +00:00
|
|
|
})
|
2020-02-08 06:12:01 +00:00
|
|
|
|
2020-04-11 23:29:19 +01:00
|
|
|
const mapDispatchToProps = (dispatch) => ({
|
2020-02-08 06:12:01 +00:00
|
|
|
|
|
|
|
onClick() {
|
|
|
|
dispatch((_, getState) => {
|
|
|
|
if (getState().getIn(['compose', 'poll'])) {
|
2020-03-07 04:53:28 +00:00
|
|
|
dispatch(removePoll())
|
2020-02-08 06:12:01 +00:00
|
|
|
} else {
|
2020-03-07 04:53:28 +00:00
|
|
|
dispatch(addPoll())
|
2020-02-08 06:12:01 +00:00
|
|
|
}
|
2020-03-07 04:53:28 +00:00
|
|
|
})
|
2020-02-08 06:12:01 +00:00
|
|
|
},
|
|
|
|
|
2020-03-07 04:53:28 +00:00
|
|
|
})
|
2020-02-08 06:12:01 +00:00
|
|
|
|
2020-08-17 23:06:22 +01:00
|
|
|
PollButton.propTypes = {
|
|
|
|
disabled: PropTypes.bool,
|
|
|
|
unavailable: PropTypes.bool,
|
|
|
|
active: PropTypes.bool,
|
|
|
|
onClick: PropTypes.func.isRequired,
|
|
|
|
intl: PropTypes.object.isRequired,
|
|
|
|
small: PropTypes.bool,
|
2020-02-08 06:12:01 +00:00
|
|
|
}
|
2020-08-17 23:06:22 +01:00
|
|
|
|
|
|
|
export default injectIntl(connect(mapStateToProps, mapDispatchToProps)(PollButton))
|