/** @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 (
updateInputDateValue(event.target.value)}/> {dateInterpretation}
) } renderMenuOption(optionKey) { const date = SendLaterOptions[optionKey](); const formatted = DateUtils.format(date, DATE_FORMAT_SHORT); return (
{optionKey}{formatted}
); } renderButton() { const {scheduledDate} = this.state; let className = 'btn btn-toolbar btn-send-later'; if (scheduledDate === 'saving') { return ( ); } let dateInterpretation = false; if (scheduledDate) { className += ' btn-enabled'; const momentDate = DateUtils.fromString(scheduledDate); if (momentDate) { dateInterpretation = Sending in {momentDate.fromNow(true)}; } } return ( ); } render() { const footerComponents = [
, this.renderCustomTimeSection(), ]; if (this.state.scheduledDate) { footerComponents.push(
) footerComponents.push(
) } return ( item } itemContent={this.renderMenuOption} footerComponents={footerComponents} onSelect={this.onSelectMenuOption} /> ); } } SendLaterPopover.containerStyles = { order: -99, }; export default SendLaterPopover