bigbuffet-rw/app/soapbox/features/compose/components/schedule_form.js

95 lines
2.7 KiB
JavaScript
Raw Normal View History

2021-06-18 09:04:31 -07:00
'use strict';
import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
2021-06-18 09:04:31 -07:00
import DatePicker from 'react-datepicker';
import 'react-datepicker/dist/react-datepicker.css';
import IconButton from 'soapbox/components/icon_button';
import { removeSchedule } from 'soapbox/actions/compose';
2021-06-18 09:04:31 -07:00
const messages = defineMessages({
schedule: { id: 'schedule.post_time', defaultMessage: 'Post Date/Time' },
remove: { id: 'schedule.remove', defaultMessage: 'Remove schedule' },
2021-06-18 09:04:31 -07:00
});
const mapStateToProps = (state, ownProps) => ({
scheduledAt: state.getIn(['compose', 'schedule']),
});
export default @connect(mapStateToProps)
@injectIntl
2021-06-18 09:04:31 -07:00
class ScheduleForm extends React.Component {
static propTypes = {
scheduledAt: PropTypes.instanceOf(Date),
2021-06-18 09:04:31 -07:00
intl: PropTypes.object.isRequired,
onSchedule: PropTypes.func.isRequired,
dispatch: PropTypes.func,
2021-06-18 09:04:31 -07:00
active: PropTypes.bool,
};
setSchedule = date => {
2021-06-18 09:04:31 -07:00
this.props.onSchedule(date);
}
openDatePicker(datePicker) {
if (!datePicker) {
2021-06-18 09:04:31 -07:00
return;
}
datePicker.setOpen(true);
}
isCurrentOrFutureDate(date) {
2021-06-18 14:51:14 -07:00
return date && new Date().setHours(0, 0, 0, 0) <= new Date(date).setHours(0, 0, 0, 0);
2021-06-18 09:04:31 -07:00
}
isFiveMinutesFromNow(time) {
2021-06-18 09:04:31 -07:00
const fiveMinutesFromNow = new Date(new Date().getTime() + 300000); // now, plus five minutes (Pleroma won't schedule posts )
const selectedDate = new Date(time);
return fiveMinutesFromNow.getTime() < selectedDate.getTime();
};
handleRemove = e => {
this.props.dispatch(removeSchedule());
e.preventDefault();
}
2021-06-18 09:04:31 -07:00
render() {
if (!this.props.active) {
2021-06-18 09:04:31 -07:00
return null;
}
const { intl, scheduledAt } = this.props;
2021-06-18 09:04:31 -07:00
return (
<div className='datepicker'>
<div className='datepicker__hint'>
<FormattedMessage id='datepicker.hint' defaultMessage='Scheduled to post at…' />
</div>
<div className='datepicker__input'>
<DatePicker
selected={scheduledAt}
showTimeSelect
dateFormat='MMMM d, yyyy h:mm aa'
timeIntervals={15}
wrapperClassName='react-datepicker-wrapper'
onChange={this.setSchedule}
placeholderText={this.props.intl.formatMessage(messages.schedule)}
filterDate={this.isCurrentOrFutureDate}
filterTime={this.isFiveMinutesFromNow}
ref={this.isCurrentOrFutureDate(scheduledAt) ? null : this.openDatePicker}
/>
<div className='datepicker__cancel'>
<IconButton size={20} title={intl.formatMessage(messages.remove)} icon='times' onClick={this.handleRemove} />
</div>
</div>
</div>
2021-06-18 09:04:31 -07:00
);
}
}