gab-social/app/javascript/gabsocial/features/compose/components/schedule_post_dropdown.js

128 lines
3.3 KiB
JavaScript
Raw Normal View History

2020-02-08 06:12:01 +00:00
import { injectIntl, defineMessages } from 'react-intl'
import DatePicker from 'react-datepicker'
import IconButton from '../../../components/icon_button'
import { changeScheduledAt } from '../../../actions/compose'
import { openModal } from '../../../actions/modal'
import { me } from '../../../initial_state'
import { isMobile } from '../../../utils/is_mobile'
import ComposeExtraButton from './compose_extra_button'
2020-02-08 06:12:01 +00:00
import 'react-datepicker/dist/react-datepicker.css'
const messages = defineMessages({
2020-02-08 06:12:01 +00:00
schedule_status: { id: 'schedule_status.title', defaultMessage: 'Schedule' },
})
const mapStateToProps = state => ({
date: state.getIn(['compose', 'scheduled_at']),
isPro: state.getIn(['accounts', me, 'is_pro']),
})
const mapDispatchToProps = dispatch => ({
setScheduledAt (date) {
dispatch(changeScheduledAt(date))
},
onOpenProUpgradeModal() {
dispatch(openModal('PRO_UPGRADE'))
},
})
2020-01-29 21:54:39 +00:00
class DatePickerWrapper extends PureComponent {
2019-09-19 03:52:59 +01:00
static propTypes = {
value: PropTypes.string,
onClick: PropTypes.func,
2020-02-08 06:12:01 +00:00
}
2019-09-19 03:52:59 +01:00
render() {
2020-02-08 06:12:01 +00:00
const { value, onClick } = this.props
2019-09-19 03:52:59 +01:00
return (
2020-02-08 06:12:01 +00:00
<button className='schedule-post-dropdown-wrapper' onClick={onClick}>
2019-09-19 03:52:59 +01:00
{value}
</button>
)
}
}
2019-09-19 03:04:49 +01:00
2020-02-08 06:12:01 +00:00
export default
@injectIntl
@connect(mapStateToProps, mapDispatchToProps)
2020-01-29 21:54:39 +00:00
class SchedulePostDropdown extends PureComponent {
static propTypes = {
date: PropTypes.instanceOf(Date),
setScheduledAt: PropTypes.func.isRequired,
intl: PropTypes.object.isRequired,
isPro: PropTypes.bool,
onOpenProUpgradeModal: PropTypes.func.isRequired,
position: PropTypes.string,
2020-02-08 06:12:01 +00:00
}
handleToggle = () => {
if (!this.props.isPro) {
2020-02-08 06:12:01 +00:00
return this.props.onOpenProUpgradeModal()
}
2020-02-08 06:12:01 +00:00
const { date } = this.props
const value = date ? null : new Date()
this.handleSetDate(value)
}
handleSetDate = (date) => {
2020-02-08 06:12:01 +00:00
this.props.setScheduledAt(date)
}
render () {
2020-02-08 06:12:01 +00:00
const { intl, date, isPro, position } = this.props
2019-09-19 03:52:59 +01:00
2020-02-08 06:12:01 +00:00
const open = !!date
const datePickerDisabled = !isPro
const withPortal = isMobile(window.innerWidth)
const popperPlacement = position || undefined
return (
2020-02-08 06:12:01 +00:00
<div className={[].join(' ')}>
<div className={[].join(' ')}>
<ComposeExtraButton
icon='calendar'
2019-09-19 02:15:29 +01:00
title={intl.formatMessage(messages.schedule_status)}
onClick={this.handleToggle}
/>
</div>
{
open &&
<DatePicker
target={this}
className='schedule-post-dropdown__datepicker'
minDate={new Date()}
selected={date}
onChange={date => this.handleSetDate(date)}
2020-02-08 06:12:01 +00:00
timeFormat='p'
timeIntervals={15}
2020-02-08 06:12:01 +00:00
timeCaption='Time'
dateFormat='MMM d, yyyy h:mm aa'
disabled={datePickerDisabled}
showTimeSelect
2019-09-19 03:04:49 +01:00
customInput={<DatePickerWrapper />}
withPortal={withPortal}
popperPlacement={popperPlacement}
popperModifiers={{
offset: {
enabled: true,
2020-02-08 06:12:01 +00:00
offset: '0px, 5px'
},
preventOverflow: {
enabled: true,
escapeWithReference: false,
2020-02-08 06:12:01 +00:00
boundariesElement: 'viewport'
}
}}
/>
}
</div>
2020-02-08 06:12:01 +00:00
)
}
}