/** @babel */ import _ from 'underscore' import React, {Component, PropTypes} from 'react' import {DateUtils} from 'nylas-exports' import {Popover} from 'nylas-component-kit' import SendLaterActions from './send-later-actions' import SendLaterStore from './send-later-store' import {DATE_FORMAT_SHORT, DATE_FORMAT_LONG} from './send-later-constants' const SendLaterOptions = { 'In 1 hour': DateUtils.in1Hour, 'Later Today': DateUtils.laterToday, 'Tomorrow Morning': DateUtils.tomorrow, 'Tomorrow Evening': DateUtils.tomorrowEvening, 'This Weekend': DateUtils.thisWeekend, 'Next Week': DateUtils.nextWeek, } class SendLaterPopover extends Component { static displayName = 'SendLaterPopover'; static propTypes = { draftClientId: PropTypes.string, }; constructor(props) { super(props) this.state = { inputSendDate: null, isScheduled: SendLaterStore.isScheduled(this.props.draftClientId), } } componentDidMount() { this.unsubscribe = SendLaterStore.listen(this.onScheduledMessagesChanged) } componentWillUnmount() { this.unsubscribe() } onSendLater = (momentDate)=> { const utcDate = momentDate.utc() const formatted = DateUtils.format(utcDate) SendLaterActions.sendLater(this.props.draftClientId, formatted) this.setState({isScheduled: null, inputSendDate: null}) this.refs.popover.close() }; onCancelSendLater = ()=> { SendLaterActions.cancelSendLater(this.props.draftClientId) this.setState({inputSendDate: null}) this.refs.popover.close() }; onScheduledMessagesChanged = ()=> { const isScheduled = SendLaterStore.isScheduled(this.props.draftClientId) if (isScheduled !== this.state.isScheduled) { this.setState({isScheduled}); } }; onInputChange = (event)=> { this.updateInputSendDateValue(event.target.value) }; getButtonLabel = (isScheduled)=> { return isScheduled ? '✅ Scheduled' : 'Send Later'; }; updateInputSendDateValue = _.debounce((dateValue)=> { const inputSendDate = DateUtils.fromString(dateValue) this.setState({inputSendDate}) }, 250); renderItems() { return Object.keys(SendLaterOptions).map((label)=> { const date = SendLaterOptions[label]() const formatted = DateUtils.format(date, DATE_FORMAT_SHORT) return (
{label} {formatted}
); }) } renderEmptyInput() { return (
) } renderLabeledInput(inputSendDate) { const formatted = DateUtils.format(inputSendDate, DATE_FORMAT_LONG) return (
{formatted}
) } render() { const {isScheduled, inputSendDate} = this.state const buttonLabel = isScheduled != null ? this.getButtonLabel(isScheduled) : 'Scheduling...'; const button = ( ) const input = inputSendDate ? this.renderLabeledInput(inputSendDate) : this.renderEmptyInput(); return (
{this.renderItems()}
{input} {isScheduled ?
: void 0} {isScheduled ?
: void 0}
); } } export default SendLaterPopover