'use strict'; import React from 'react'; import PropTypes from 'prop-types'; import { connect } from 'react-redux'; import { defineMessages, injectIntl, FormattedMessage } from 'react-intl'; 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'; import classNames from 'classnames'; const messages = defineMessages({ schedule: { id: 'schedule.post_time', defaultMessage: 'Post Date/Time' }, remove: { id: 'schedule.remove', defaultMessage: 'Remove schedule' }, }); const mapStateToProps = (state, ownProps) => ({ scheduledAt: state.getIn(['compose', 'schedule']), }); export default @connect(mapStateToProps) @injectIntl class ScheduleForm extends React.Component { static propTypes = { scheduledAt: PropTypes.instanceOf(Date), intl: PropTypes.object.isRequired, onSchedule: PropTypes.func.isRequired, dispatch: PropTypes.func, active: PropTypes.bool, }; state = { initialized: false, } setSchedule = date => { this.props.onSchedule(date); } setRef = c => { this.datePicker = c; } openDatePicker = () => { if (!this.datePicker) return; this.datePicker.setOpen(true); } isCurrentOrFutureDate(date) { return date && new Date().setHours(0, 0, 0, 0) <= new Date(date).setHours(0, 0, 0, 0); } isFiveMinutesFromNow(time) { 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(); } initialize = () => { const { initialized } = this.state; if (!initialized && this.datePicker) { this.openDatePicker(); this.setState({ initialized: true }); } } componentDidUpdate() { this.initialize(); } render() { if (!this.props.active) { return null; } const { intl, scheduledAt } = this.props; return (