gab-social/app/javascript/gabsocial/components/popover/date_picker_popover.js

127 lines
3.6 KiB
JavaScript
Raw Normal View History

2020-03-27 22:57:03 +00:00
import DatePicker from 'react-datepicker'
2020-05-02 07:25:55 +01:00
import { FormattedMessage } from 'react-intl'
import moment from 'moment-mini'
2020-03-27 22:57:03 +00:00
import { changeScheduledAt } from '../../actions/compose'
2020-05-02 07:25:55 +01:00
import { openModal } from '../../actions/modal'
import { closePopover } from '../../actions/popover'
2020-03-27 22:57:03 +00:00
import { me } from '../../initial_state'
import { MODAL_PRO_UPGRADE } from '../../constants'
2020-03-27 22:57:03 +00:00
import PopoverLayout from './popover_layout'
2020-05-02 07:25:55 +01:00
import Button from '../button'
import Text from '../text'
2020-03-27 22:57:03 +00:00
2020-04-16 07:00:43 +01:00
import '!style-loader!css-loader!react-datepicker/dist/react-datepicker.css'
2020-03-27 22:57:03 +00:00
2020-04-11 23:29:19 +01:00
const mapStateToProps = (state) => ({
2020-03-27 22:57:03 +00:00
date: state.getIn(['compose', 'scheduled_at']),
isPro: state.getIn(['accounts', me, 'is_pro']),
})
2020-05-15 04:46:10 +01:00
const mapDispatchToProps = (dispatch) => ({
setScheduledAt (date, isPro) {
2020-05-02 07:25:55 +01:00
if (!isPro) {
dispatch(closePopover())
2020-05-02 07:25:55 +01:00
return dispatch(openModal(MODAL_PRO_UPGRADE))
}
2020-03-27 22:57:03 +00:00
dispatch(changeScheduledAt(date))
2020-05-02 07:25:55 +01:00
if (!date) {
dispatch(closePopover())
}
2020-03-27 22:57:03 +00:00
},
onClosePopover: () => dispatch(closePopover())
2020-03-27 22:57:03 +00:00
})
export default
@connect(mapStateToProps, mapDispatchToProps)
class DatePickerPopover extends PureComponent {
2020-03-27 22:57:03 +00:00
static propTypes = {
date: PropTypes.instanceOf(Date),
setScheduledAt: PropTypes.func.isRequired,
isPro: PropTypes.bool,
position: PropTypes.string,
small: PropTypes.bool,
isXS: PropTypes.bool,
onClosePopover: PropTypes.func.isRequired,
2020-03-27 22:57:03 +00:00
}
handleSetDate = (date) => {
2020-05-15 04:46:10 +01:00
this.props.setScheduledAt(date, this.props.isPro)
2020-03-27 22:57:03 +00:00
}
2020-05-02 07:25:55 +01:00
handleRemoveDate = () => {
2020-05-15 04:46:10 +01:00
this.props.setScheduledAt(null, this.props.isPro)
2020-05-02 07:25:55 +01:00
}
2020-03-27 22:57:03 +00:00
handleOnClosePopover = () => {
this.props.onClosePopover()
}
2020-02-24 21:56:07 +00:00
render() {
const { date, isPro, isXS } = this.props
2020-03-27 22:57:03 +00:00
const datePickerDisabled = !isPro
2020-02-24 21:56:07 +00:00
return (
<PopoverLayout
width={360}
isXS={isXS}
onClose={this.handleOnClosePopover}
>
<div className={[_s.default, _s.bgSubtle].join(' ')}>
2020-05-02 07:25:55 +01:00
<DatePicker
inline
target={this}
className='schedule-post-dropdown__datepicker'
minDate={new Date()}
selected={date}
onChange={date => this.handleSetDate(date)}
timeFormat='p'
timeIntervals={15}
timeCaption='Time'
dateFormat='MMM d, yyyy h:mm aa'
disabled={datePickerDisabled}
showTimeSelect
popperModifiers={{
offset: {
enabled: true,
offset: '0px, 5px'
},
preventOverflow: {
enabled: true,
escapeWithReference: false,
boundariesElement: 'viewport'
}
}}
/>
</div>
{
date &&
<div className={[_s.default, _s.alignItemsCenter, _s.flexRow, _s.px10, _s.py10, _s.borderTop1PX, _s.borderColorSecondary].join(' ')}>
<Text size='extraSmall' color='secondary'>
<FormattedMessage id='scheduled_for_datetime' defaultMessage='Scheduled for {datetime}' values={{
datetime: moment.utc(date).format('lll'),
}}/>
</Text>
<div className={_s.mlAuto}>
<Button
isNarrow
radiusSmall
color='primary'
backgroundColor='tertiary'
onClick={this.handleRemoveDate}
>
<Text color='inherit' size='small'>
<FormattedMessage id='remove' defaultMessage='Remove' />
</Text>
</Button>
</div>
</div>
}
2020-03-27 22:57:03 +00:00
</PopoverLayout>
2020-02-24 21:56:07 +00:00
)
}
2020-02-24 21:56:07 +00:00
}