/** @babel */ import Rx from 'rx-lite' import React, {Component, PropTypes} from 'react' import {DateUtils, Message, DatabaseStore} from 'nylas-exports' import {Popover, RetinaImg, Menu, DateInput} from 'nylas-component-kit' import SendLaterActions from './send-later-actions' import {PLUGIN_ID} from './send-later-constants' const {DATE_FORMAT_SHORT, DATE_FORMAT_LONG} = DateUtils 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.isRequired, }; constructor(props) { super(props) this.state = { scheduledDate: null, } } componentDidMount() { this._subscription = Rx.Observable.fromQuery( DatabaseStore.findBy(Message, {clientId: this.props.draftClientId}) ).subscribe(this.onMessageChanged); } componentWillUnmount() { this._subscription.dispose(); } onMessageChanged = (message)=> { if (!message) return; const messageMetadata = message.metadataForPluginId(PLUGIN_ID) || {} const nextScheduledDate = messageMetadata.sendLaterDate if (nextScheduledDate !== this.state.scheduledDate) { const isComposer = NylasEnv.isComposerWindow() const isFinishedSelecting = ((this.state.scheduledDate === 'saving') && (nextScheduledDate !== null)); if (isComposer && isFinishedSelecting) { NylasEnv.close(); } this.setState({scheduledDate: nextScheduledDate}); } }; onSelectMenuOption = (optionKey)=> { const date = SendLaterOptions[optionKey](); this.selectDate(date, optionKey); }; onSelectCustomOption = (date, inputValue)=> { if (date) { this.selectDate(date, "Custom"); } else { NylasEnv.showErrorDialog(`Sorry, we can't parse ${inputValue} as a valid date.`); } }; onCancelSendLater = ()=> { SendLaterActions.cancelSendLater(this.props.draftClientId); this.refs.popover.close(); }; selectDate = (date, dateLabel)=> { const formatted = DateUtils.format(date.utc()); SendLaterActions.sendLater(this.props.draftClientId, formatted, dateLabel); this.setState({scheduledDate: 'saving'}); this.refs.popover.close(); }; 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; if (scheduledDate) { className += ' btn-enabled'; const momentDate = DateUtils.futureDateFromString(scheduledDate); if (momentDate) { dateInterpretation = Sending in {momentDate.fromNow(true)}; } } return ( ); } render() { const headerComponents = [ Send later:, ] const footerComponents = [
, , ]; if (this.state.scheduledDate) { footerComponents.push(
) footerComponents.push(
) } return ( item } itemContent={this.renderMenuOption} defaultSelectedIndex={-1} headerComponents={headerComponents} footerComponents={footerComponents} onSelect={this.onSelectMenuOption} /> ); } } SendLaterPopover.containerStyles = { order: -99, }; export default SendLaterPopover