2020-02-08 01:12:01 -05:00
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component'
|
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes'
|
|
|
|
import { defineMessages, injectIntl } from 'react-intl'
|
|
|
|
import { uploadCompose } from '../../../actions/compose'
|
|
|
|
import ComposeExtraButton from './compose_extra_button'
|
|
|
|
|
|
|
|
const messages = defineMessages({
|
|
|
|
upload: { id: 'upload_button.label', defaultMessage: 'Add media (JPEG, PNG, GIF, WebM, MP4, MOV)' },
|
|
|
|
title: { id: 'upload_button.title', defaultMessage: 'Photo/Video' },
|
|
|
|
})
|
|
|
|
|
|
|
|
const makeMapStateToProps = () => {
|
|
|
|
const mapStateToProps = state => ({
|
|
|
|
acceptContentTypes: state.getIn(['media_attachments', 'accept_content_types']),
|
|
|
|
disabled: state.getIn(['compose', 'is_uploading']) || (state.getIn(['compose', 'media_attachments']).size > 3 || state.getIn(['compose', 'media_attachments']).some(m => m.get('type') === 'video')),
|
|
|
|
unavailable: state.getIn(['compose', 'poll']) !== null,
|
|
|
|
resetFileKey: state.getIn(['compose', 'resetFileKey']),
|
|
|
|
})
|
|
|
|
|
|
|
|
return mapStateToProps
|
|
|
|
}
|
|
|
|
|
|
|
|
const mapDispatchToProps = dispatch => ({
|
|
|
|
onSelectFile(files) {
|
|
|
|
dispatch(uploadCompose(files))
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
export default
|
|
|
|
@connect(makeMapStateToProps, mapDispatchToProps)
|
|
|
|
@injectIntl
|
|
|
|
class UploadButton extends ImmutablePureComponent {
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
disabled: PropTypes.bool,
|
|
|
|
unavailable: PropTypes.bool,
|
|
|
|
onSelectFile: PropTypes.func.isRequired,
|
|
|
|
style: PropTypes.object,
|
|
|
|
resetFileKey: PropTypes.number,
|
|
|
|
acceptContentTypes: ImmutablePropTypes.listOf(PropTypes.string).isRequired,
|
|
|
|
intl: PropTypes.object.isRequired,
|
2020-02-28 10:20:47 -05:00
|
|
|
small: PropTypes.bool,
|
2020-02-08 01:12:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
handleChange = (e) => {
|
|
|
|
if (e.target.files.length > 0) {
|
|
|
|
this.props.onSelectFile(e.target.files)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleClick = () => {
|
|
|
|
this.fileElement.click()
|
|
|
|
}
|
|
|
|
|
|
|
|
setRef = (c) => {
|
|
|
|
this.fileElement = c
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2020-02-28 10:20:47 -05:00
|
|
|
const { intl, resetFileKey, unavailable, disabled, acceptContentTypes, small } = this.props
|
2020-02-08 01:12:01 -05:00
|
|
|
|
|
|
|
if (unavailable) return null
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ComposeExtraButton
|
|
|
|
title={intl.formatMessage(messages.title)}
|
|
|
|
disabled={disabled}
|
|
|
|
onClick={this.handleClick}
|
2020-02-13 19:40:04 -05:00
|
|
|
icon='media'
|
2020-02-28 10:20:47 -05:00
|
|
|
small={small}
|
2020-02-08 01:12:01 -05:00
|
|
|
>
|
|
|
|
<label>
|
2020-02-19 18:57:07 -05:00
|
|
|
<span className={_s.displayNone}>{intl.formatMessage(messages.upload)}</span>
|
2020-02-08 01:12:01 -05:00
|
|
|
<input
|
|
|
|
key={resetFileKey}
|
|
|
|
ref={this.setRef}
|
|
|
|
type='file'
|
|
|
|
accept={acceptContentTypes.toArray().join(',')}
|
|
|
|
onChange={this.handleChange}
|
|
|
|
disabled={disabled}
|
2020-02-19 18:57:07 -05:00
|
|
|
className={_s.displayNone}
|
2020-02-08 01:12:01 -05:00
|
|
|
multiple
|
|
|
|
/>
|
|
|
|
</label>
|
|
|
|
</ComposeExtraButton>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|