66 lines
1.6 KiB
JavaScript
Raw Normal View History

2020-03-06 23:53:28 -05:00
import { defineMessages, injectIntl } from 'react-intl'
import { addPoll, removePoll } from '../../../actions/compose'
2020-02-08 01:12:01 -05:00
import ComposeExtraButton from './compose_extra_button'
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-06 23:53:28 -05:00
})
2020-02-08 01:12:01 -05:00
2020-04-11 18:29:19 -04:00
const mapStateToProps = (state) => ({
2020-02-08 01:12:01 -05:00
unavailable: state.getIn(['compose', 'is_uploading']) || (state.getIn(['compose', 'media_attachments']).size > 0),
active: state.getIn(['compose', 'poll']) !== null,
2020-03-06 23:53:28 -05:00
})
2020-02-08 01:12:01 -05:00
2020-04-11 18:29:19 -04:00
const mapDispatchToProps = (dispatch) => ({
2020-02-08 01:12:01 -05:00
onClick() {
dispatch((_, getState) => {
if (getState().getIn(['compose', 'poll'])) {
2020-03-06 23:53:28 -05:00
dispatch(removePoll())
2020-02-08 01:12:01 -05:00
} else {
2020-03-06 23:53:28 -05:00
dispatch(addPoll())
2020-02-08 01:12:01 -05:00
}
2020-03-06 23:53:28 -05:00
})
2020-02-08 01:12:01 -05:00
},
2020-03-06 23:53:28 -05:00
})
2020-02-08 01:12:01 -05:00
export default
@connect(mapStateToProps, mapDispatchToProps)
@injectIntl
class PollButton extends PureComponent {
static propTypes = {
disabled: PropTypes.bool,
unavailable: PropTypes.bool,
active: PropTypes.bool,
onClick: PropTypes.func.isRequired,
intl: PropTypes.object.isRequired,
2020-02-28 10:20:47 -05:00
small: PropTypes.bool,
2020-03-06 23:53:28 -05:00
}
2020-02-08 01:12:01 -05:00
handleClick = () => {
2020-03-06 23:53:28 -05:00
this.props.onClick()
2020-02-08 01:12:01 -05:00
}
render() {
2020-03-06 23:53:28 -05:00
const { intl, active, unavailable, disabled, small } = this.props
2020-02-08 01:12:01 -05:00
2020-03-06 23:53:28 -05:00
if (unavailable) return null
2020-02-08 01:12:01 -05:00
return (
<ComposeExtraButton
title={intl.formatMessage(active ? messages.remove_poll : messages.title)}
disabled={disabled}
onClick={this.handleClick}
2020-02-13 19:40:04 -05:00
icon='poll'
2020-02-28 10:20:47 -05:00
small={small}
2020-03-06 23:53:28 -05:00
active={active}
2020-02-08 01:12:01 -05:00
/>
)
}
}