/** @babel */ import _ from 'underscore' import Rx from 'rx-lite' import React, {Component, PropTypes} from 'react' import {DateUtils, Message, DatabaseStore} from 'nylas-exports' import {Popover, RetinaImg, Menu} 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, 'In 2 hours': DateUtils.in2Hours, '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 = { inputDate: null, scheduledDate: null, } } componentDidMount() { this._subscription = Rx.Observable.fromQuery( DatabaseStore.findBy(Message, {clientId: this.props.draftClientId}) ).subscribe((draft)=> { const scheduledDate = SendLaterStore.getScheduledDateForMessage(draft); if (scheduledDate !== this.state.scheduledDate) { this.setState({scheduledDate}); } }); } componentWillUnmount() { this._subscription.dispose(); } onSelectMenuOption = (optionKey)=> { const date = SendLaterOptions[optionKey](); const formatted = DateUtils.format(date.utc()) SendLaterActions.sendLater(this.props.draftClientId, formatted) this.setState({scheduledDate: 'saving', inputDate: null}) this.refs.popover.close() }; onCancelSendLater = ()=> { SendLaterActions.cancelSendLater(this.props.draftClientId) this.setState({inputDate: null}) this.refs.popover.close() }; renderCustomTimeSection() { const updateInputDateValue = _.debounce((value)=> { this.setState({inputDate: DateUtils.fromString(value)}) }, 250); let dateInterpretation = false; if (this.state.inputDate) { dateInterpretation = ( {DateUtils.format(this.state.inputDate, DATE_FORMAT_LONG)} ); } return (