2016-03-10 02:01:18 +08:00
|
|
|
/** @babel */
|
|
|
|
import Rx from 'rx-lite'
|
|
|
|
import React, {Component, PropTypes} from 'react'
|
|
|
|
import {Actions, DateUtils, Message, DatabaseStore} from 'nylas-exports'
|
|
|
|
import {RetinaImg} from 'nylas-component-kit'
|
|
|
|
import SendLaterPopover from './send-later-popover'
|
|
|
|
import SendLaterActions from './send-later-actions'
|
|
|
|
import {PLUGIN_ID} from './send-later-constants'
|
|
|
|
|
|
|
|
|
|
|
|
class SendLaterButton extends Component {
|
|
|
|
static displayName = 'SendLaterButton';
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
draftClientId: PropTypes.string.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super()
|
|
|
|
this.state = {
|
|
|
|
scheduledDate: null,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this._subscription = Rx.Observable.fromQuery(
|
|
|
|
DatabaseStore.findBy(Message, {clientId: this.props.draftClientId})
|
|
|
|
).subscribe(this.onMessageChanged);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this._subscription.dispose();
|
|
|
|
}
|
|
|
|
|
2016-03-17 10:27:12 +08:00
|
|
|
onSendLater = (formattedDate, dateLabel) => {
|
2016-03-10 02:01:18 +08:00
|
|
|
SendLaterActions.sendLater(this.props.draftClientId, formattedDate, dateLabel);
|
|
|
|
this.setState({scheduledDate: 'saving'});
|
|
|
|
};
|
|
|
|
|
2016-03-17 10:27:12 +08:00
|
|
|
onCancelSendLater = () => {
|
2016-03-10 02:01:18 +08:00
|
|
|
SendLaterActions.cancelSendLater(this.props.draftClientId);
|
|
|
|
};
|
|
|
|
|
2016-03-17 10:27:12 +08:00
|
|
|
onClick = () => {
|
2016-03-10 02:01:18 +08:00
|
|
|
const buttonRect = React.findDOMNode(this).getBoundingClientRect()
|
|
|
|
Actions.openPopover(
|
|
|
|
<SendLaterPopover
|
|
|
|
scheduledDate={this.state.scheduledDate}
|
|
|
|
onSendLater={this.onSendLater}
|
2016-03-17 10:27:12 +08:00
|
|
|
onCancelSendLater={this.onCancelSendLater}
|
|
|
|
/>,
|
2016-03-10 02:01:18 +08:00
|
|
|
{originRect: buttonRect, direction: 'up'}
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
2016-03-17 10:27:12 +08:00
|
|
|
onMessageChanged = (message) => {
|
2016-03-10 02:01:18 +08:00
|
|
|
if (!message) return;
|
2016-03-17 10:27:12 +08:00
|
|
|
const {scheduledDate} = this.state;
|
2016-03-10 02:01:18 +08:00
|
|
|
const messageMetadata = message.metadataForPluginId(PLUGIN_ID) || {}
|
|
|
|
const nextScheduledDate = messageMetadata.sendLaterDate
|
|
|
|
|
2016-03-17 10:27:12 +08:00
|
|
|
if (nextScheduledDate !== scheduledDate) {
|
2016-03-10 02:01:18 +08:00
|
|
|
const isComposer = NylasEnv.isComposerWindow()
|
2016-03-17 10:27:12 +08:00
|
|
|
const isFinishedSelecting = ((scheduledDate === 'saving') && (nextScheduledDate !== null));
|
2016-03-10 02:01:18 +08:00
|
|
|
if (isComposer && isFinishedSelecting) {
|
|
|
|
NylasEnv.close();
|
|
|
|
}
|
|
|
|
this.setState({scheduledDate: nextScheduledDate});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const {scheduledDate} = this.state;
|
|
|
|
let className = 'btn btn-toolbar btn-send-later';
|
|
|
|
|
|
|
|
if (scheduledDate === 'saving') {
|
|
|
|
return (
|
|
|
|
<button className={className} title="Saving send date...">
|
|
|
|
<RetinaImg
|
|
|
|
name="inline-loading-spinner.gif"
|
|
|
|
mode={RetinaImg.Mode.ContentDark}
|
2016-03-17 10:27:12 +08:00
|
|
|
style={{width: 14, height: 14}}
|
|
|
|
/>
|
2016-03-10 02:01:18 +08:00
|
|
|
</button>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let dateInterpretation;
|
|
|
|
if (scheduledDate) {
|
|
|
|
className += ' btn-enabled';
|
|
|
|
const momentDate = DateUtils.futureDateFromString(scheduledDate);
|
|
|
|
if (momentDate) {
|
|
|
|
dateInterpretation = <span className="at">Sending in {momentDate.fromNow(true)}</span>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<button className={className} title="Send later…" onClick={this.onClick}>
|
2016-03-17 10:27:12 +08:00
|
|
|
<RetinaImg name="icon-composer-sendlater.png" mode={RetinaImg.Mode.ContentIsMask} />
|
2016-03-10 02:01:18 +08:00
|
|
|
{dateInterpretation}
|
|
|
|
<span> </span>
|
2016-03-17 10:27:12 +08:00
|
|
|
<RetinaImg name="icon-composer-dropdown.png" mode={RetinaImg.Mode.ContentIsMask} />
|
2016-03-10 02:01:18 +08:00
|
|
|
</button>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SendLaterButton.containerStyles = {
|
|
|
|
order: -99,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default SendLaterButton;
|