2016-02-19 02:00:11 +08:00
|
|
|
/** @babel */
|
|
|
|
import _ from 'underscore'
|
2016-02-24 05:52:08 +08:00
|
|
|
import Rx from 'rx-lite'
|
2016-02-19 02:00:11 +08:00
|
|
|
import React, {Component, PropTypes} from 'react'
|
2016-02-24 05:52:08 +08:00
|
|
|
import {DateUtils, Message, DatabaseStore} from 'nylas-exports'
|
|
|
|
import {Popover, RetinaImg, Menu} from 'nylas-component-kit'
|
2016-02-19 02:00:11 +08:00
|
|
|
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,
|
2016-02-24 05:52:08 +08:00
|
|
|
'In 2 hours': DateUtils.in2Hours,
|
2016-02-19 02:00:11 +08:00
|
|
|
'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 = {
|
2016-02-25 03:19:03 +08:00
|
|
|
draftClientId: PropTypes.string.isRequired,
|
2016-02-19 02:00:11 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
|
|
|
this.state = {
|
2016-02-24 05:52:08 +08:00
|
|
|
inputDate: null,
|
|
|
|
scheduledDate: null,
|
2016-02-19 02:00:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2016-02-24 05:52:08 +08:00
|
|
|
this._subscription = Rx.Observable.fromQuery(
|
|
|
|
DatabaseStore.findBy(Message, {clientId: this.props.draftClientId})
|
|
|
|
).subscribe((draft)=> {
|
2016-02-25 03:19:03 +08:00
|
|
|
const nextScheduledDate = SendLaterStore.getScheduledDateForMessage(draft);
|
|
|
|
|
|
|
|
if (nextScheduledDate !== this.state.scheduledDate) {
|
|
|
|
const isPopout = (NylasEnv.getWindowType() === "composer");
|
|
|
|
const isFinishedSelecting = ((this.state.scheduledDate === 'saving') && (nextScheduledDate !== null));
|
|
|
|
if (isPopout && isFinishedSelecting) {
|
|
|
|
NylasEnv.close();
|
|
|
|
}
|
|
|
|
this.setState({scheduledDate: nextScheduledDate});
|
2016-02-24 05:52:08 +08:00
|
|
|
}
|
|
|
|
});
|
2016-02-19 02:00:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2016-02-24 05:52:08 +08:00
|
|
|
this._subscription.dispose();
|
2016-02-19 02:00:11 +08:00
|
|
|
}
|
|
|
|
|
2016-02-24 05:52:08 +08:00
|
|
|
onSelectMenuOption = (optionKey)=> {
|
|
|
|
const date = SendLaterOptions[optionKey]();
|
2016-02-25 03:19:03 +08:00
|
|
|
const formatted = DateUtils.format(date.utc());
|
|
|
|
this.onSelectDate(date);
|
2016-02-19 02:00:11 +08:00
|
|
|
};
|
|
|
|
|
2016-02-25 03:19:03 +08:00
|
|
|
onSelectCustomOption = (value)=> {
|
|
|
|
const date = DateUtils.fromString(value);
|
|
|
|
if (date) {
|
|
|
|
this.onSelectDate(date);
|
|
|
|
} else {
|
|
|
|
NylasEnv.showErrorDialog(`Sorry, we can't parse ${value} as a valid date.`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onSelectDate = (date)=> {
|
|
|
|
const formatted = DateUtils.format(date.utc());
|
|
|
|
SendLaterActions.sendLater(this.props.draftClientId, formatted);
|
|
|
|
this.setState({scheduledDate: 'saving', inputDate: null});
|
|
|
|
this.refs.popover.close();
|
|
|
|
}
|
|
|
|
|
2016-02-19 02:00:11 +08:00
|
|
|
onCancelSendLater = ()=> {
|
2016-02-25 03:19:03 +08:00
|
|
|
SendLaterActions.cancelSendLater(this.props.draftClientId);
|
|
|
|
this.setState({inputDate: null});
|
|
|
|
this.refs.popover.close();
|
2016-02-19 02:00:11 +08:00
|
|
|
};
|
|
|
|
|
2016-02-24 05:52:08 +08:00
|
|
|
renderCustomTimeSection() {
|
2016-02-25 03:19:03 +08:00
|
|
|
const onChange = (event)=> {
|
|
|
|
this.setState({inputDate: DateUtils.fromString(event.target.value)});
|
|
|
|
}
|
|
|
|
|
|
|
|
const onKeyDown = (event)=> {
|
|
|
|
// we need to swallow these events so they don't reach the menu
|
|
|
|
// containing the text input, but only when you've typed something.
|
|
|
|
const val = event.target.value;
|
|
|
|
if ((val.length > 0) && ((event.keyCode === 13) || (event.keyCode === 39))) {
|
|
|
|
this.onSelectCustomOption(val);
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
|
|
|
};
|
2016-02-19 02:00:11 +08:00
|
|
|
|
2016-02-24 05:52:08 +08:00
|
|
|
let dateInterpretation = false;
|
|
|
|
if (this.state.inputDate) {
|
2016-02-25 03:19:03 +08:00
|
|
|
dateInterpretation = (<span className="time">
|
2016-02-24 05:52:08 +08:00
|
|
|
{DateUtils.format(this.state.inputDate, DATE_FORMAT_LONG)}
|
2016-02-25 03:19:03 +08:00
|
|
|
</span>);
|
2016-02-24 05:52:08 +08:00
|
|
|
}
|
2016-02-19 02:00:11 +08:00
|
|
|
|
|
|
|
return (
|
2016-02-24 05:52:08 +08:00
|
|
|
<div key="custom" className="custom-time-section">
|
2016-02-19 02:00:11 +08:00
|
|
|
<input
|
2016-02-25 03:22:54 +08:00
|
|
|
tabIndex="1"
|
2016-02-19 02:00:11 +08:00
|
|
|
type="text"
|
2016-02-25 03:19:03 +08:00
|
|
|
placeholder="Or, 'next Monday at 2PM'"
|
|
|
|
onKeyDown={onKeyDown}
|
|
|
|
onChange={onChange}/>
|
2016-02-24 05:52:08 +08:00
|
|
|
{dateInterpretation}
|
2016-02-19 02:00:11 +08:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-02-24 05:52:08 +08:00
|
|
|
renderMenuOption(optionKey) {
|
|
|
|
const date = SendLaterOptions[optionKey]();
|
|
|
|
const formatted = DateUtils.format(date, DATE_FORMAT_SHORT);
|
2016-02-19 02:00:11 +08:00
|
|
|
return (
|
2016-02-25 03:19:03 +08:00
|
|
|
<div className="send-later-option">{optionKey}<span className="time">{formatted}</span></div>
|
2016-02-24 05:52:08 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderButton() {
|
|
|
|
const {scheduledDate} = this.state;
|
2016-02-24 07:40:44 +08:00
|
|
|
let className = 'btn btn-toolbar btn-send-later';
|
2016-02-24 05:52:08 +08:00
|
|
|
|
|
|
|
if (scheduledDate === 'saving') {
|
|
|
|
return (
|
2016-02-24 09:51:10 +08:00
|
|
|
<button className={className} title="Send later...">
|
2016-02-24 06:20:26 +08:00
|
|
|
<RetinaImg
|
|
|
|
name="inline-loading-spinner.gif"
|
|
|
|
mode={RetinaImg.Mode.ContentDark}
|
|
|
|
style={{width: 14, height: 14}}/>
|
|
|
|
</button>
|
2016-02-24 05:52:08 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let dateInterpretation = false;
|
|
|
|
if (scheduledDate) {
|
2016-02-24 07:40:44 +08:00
|
|
|
className += ' btn-enabled';
|
2016-02-24 05:52:08 +08:00
|
|
|
const momentDate = DateUtils.fromString(scheduledDate);
|
|
|
|
if (momentDate) {
|
|
|
|
dateInterpretation = <span className="at">Sending in {momentDate.fromNow(true)}</span>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<button className={className}>
|
|
|
|
<RetinaImg name="icon-composer-sendlater.png" mode={RetinaImg.Mode.ContentIsMask}/>
|
|
|
|
{dateInterpretation}
|
2016-02-24 10:35:24 +08:00
|
|
|
<span> </span>
|
2016-02-24 06:20:26 +08:00
|
|
|
<RetinaImg name="icon-composer-dropdown.png" mode={RetinaImg.Mode.ContentIsMask}/>
|
2016-02-24 05:52:08 +08:00
|
|
|
</button>
|
|
|
|
);
|
2016-02-19 02:00:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2016-02-24 05:52:08 +08:00
|
|
|
const footerComponents = [
|
|
|
|
<div key="divider" className="divider" />,
|
|
|
|
this.renderCustomTimeSection(),
|
|
|
|
];
|
|
|
|
|
|
|
|
if (this.state.scheduledDate) {
|
|
|
|
footerComponents.push(<div key="divider-unschedule" className="divider" />)
|
|
|
|
footerComponents.push(
|
|
|
|
<div className="cancel-section" key="cancel-section">
|
|
|
|
<button className="btn" onClick={this.onCancelSendLater}>
|
|
|
|
Unschedule Send
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
2016-02-19 02:00:11 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Popover
|
|
|
|
ref="popover"
|
|
|
|
style={{order: -103}}
|
|
|
|
className="send-later"
|
2016-02-24 05:52:08 +08:00
|
|
|
buttonComponent={this.renderButton()}>
|
2016-02-25 03:19:03 +08:00
|
|
|
<Menu ref="menu"
|
|
|
|
items={ Object.keys(SendLaterOptions) }
|
2016-02-24 05:52:08 +08:00
|
|
|
itemKey={ (item)=> item }
|
|
|
|
itemContent={this.renderMenuOption}
|
2016-02-25 03:19:03 +08:00
|
|
|
defaultSelectedIndex={-1}
|
2016-02-24 05:52:08 +08:00
|
|
|
footerComponents={footerComponents}
|
|
|
|
onSelect={this.onSelectMenuOption}
|
|
|
|
/>
|
2016-02-19 02:00:11 +08:00
|
|
|
</Popover>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-02-24 07:40:44 +08:00
|
|
|
SendLaterPopover.containerStyles = {
|
|
|
|
order: -99,
|
|
|
|
};
|
|
|
|
|
2016-02-19 02:00:11 +08:00
|
|
|
export default SendLaterPopover
|