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

119 lines
3.4 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'
2020-05-02 07:25:55 +01:00
import {
MODAL_PRO_UPGRADE,
} from '../../constants'
2020-03-27 22:57:03 +00:00
import { isMobile } from '../../utils/is_mobile'
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-02 07:25:55 +01:00
const mapDispatchToProps = (dispatch, { isPro }) => ({
2020-03-27 22:57:03 +00:00
setScheduledAt (date) {
2020-05-02 07:25:55 +01:00
if (!isPro) {
dispatch(closePopover())
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
},
})
export default
@connect(mapStateToProps, mapDispatchToProps)
class DatePickerPopover extends PureComponent {
static propTypes = {
date: PropTypes.instanceOf(Date),
setScheduledAt: PropTypes.func.isRequired,
isPro: PropTypes.bool,
position: PropTypes.string,
small: PropTypes.bool,
}
handleSetDate = (date) => {
this.props.setScheduledAt(date)
}
2020-05-02 07:25:55 +01:00
handleRemoveDate = () => {
this.props.setScheduledAt(null)
}
2020-03-27 22:57:03 +00:00
2020-02-24 21:56:07 +00:00
render() {
2020-05-02 07:25:55 +01:00
const { date, isPro } = this.props
2020-03-27 22:57:03 +00:00
const datePickerDisabled = !isPro
const withPortal = isMobile(window.innerWidth)
2020-02-24 21:56:07 +00:00
return (
2020-04-16 07:00:43 +01:00
<PopoverLayout width={331}>
2020-05-02 07:25:55 +01:00
<div className={[_s.default].join(' ')}>
<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
withPortal={withPortal}
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
)
}
}