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';
|
2016-02-24 06:20:26 +08:00
|
|
|
static containerStyles = {
|
|
|
|
order: -99,
|
|
|
|
};
|
2016-02-19 02:00:11 +08:00
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
draftClientId: PropTypes.string,
|
|
|
|
};
|
|
|
|
|
|
|
|
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)=> {
|
|
|
|
const scheduledDate = SendLaterStore.getScheduledDateForMessage(draft);
|
|
|
|
if (scheduledDate !== this.state.scheduledDate) {
|
|
|
|
this.setState({scheduledDate});
|
|
|
|
}
|
|
|
|
});
|
2016-02-19 02:00:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2016-02-24 05:52:08 +08:00
|
|
|
this._subscription.dispose();
|
|
|
|
this.unsubscribe();
|
2016-02-19 02:00:11 +08:00
|
|
|
}
|
|
|
|
|
2016-02-24 05:52:08 +08:00
|
|
|
onSelectMenuOption = (optionKey)=> {
|
|
|
|
const date = SendLaterOptions[optionKey]();
|
|
|
|
const formatted = DateUtils.format(date.utc())
|
2016-02-19 02:00:11 +08:00
|
|
|
|
2016-02-24 05:52:08 +08:00
|
|
|
SendLaterActions.sendLater(this.props.draftClientId, formatted)
|
|
|
|
this.setState({scheduledDate: 'saving', inputDate: null})
|
2016-02-19 02:00:11 +08:00
|
|
|
this.refs.popover.close()
|
|
|
|
};
|
|
|
|
|
|
|
|
onCancelSendLater = ()=> {
|
|
|
|
SendLaterActions.cancelSendLater(this.props.draftClientId)
|
2016-02-24 05:52:08 +08:00
|
|
|
this.setState({inputDate: null})
|
2016-02-19 02:00:11 +08:00
|
|
|
this.refs.popover.close()
|
|
|
|
};
|
|
|
|
|
2016-02-24 05:52:08 +08:00
|
|
|
renderCustomTimeSection() {
|
|
|
|
const updateInputDateValue = _.debounce((value)=> {
|
|
|
|
this.setState({inputDate: DateUtils.fromString(value)})
|
|
|
|
}, 250);
|
2016-02-19 02:00:11 +08:00
|
|
|
|
2016-02-24 05:52:08 +08:00
|
|
|
let dateInterpretation = false;
|
|
|
|
if (this.state.inputDate) {
|
|
|
|
dateInterpretation = (<em>
|
|
|
|
{DateUtils.format(this.state.inputDate, DATE_FORMAT_LONG)}
|
|
|
|
</em>);
|
|
|
|
}
|
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-24 05:52:08 +08:00
|
|
|
tabIndex={1}
|
2016-02-19 02:00:11 +08:00
|
|
|
type="text"
|
2016-02-24 05:52:08 +08:00
|
|
|
placeholder="Or type a time"
|
|
|
|
onChange={event=> updateInputDateValue(event.target.value)}/>
|
|
|
|
{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-24 05:52:08 +08:00
|
|
|
<div className="send-later-option">{optionKey}<em>{formatted}</em></div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderButton() {
|
|
|
|
const {scheduledDate} = this.state;
|
|
|
|
|
|
|
|
if (scheduledDate === 'saving') {
|
|
|
|
return (
|
2016-02-24 06:20:26 +08:00
|
|
|
<button className={className}>
|
|
|
|
<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 className = 'btn btn-toolbar btn-send-later';
|
|
|
|
let dateInterpretation = false;
|
|
|
|
if (scheduledDate) {
|
|
|
|
className += ' scheduled';
|
|
|
|
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 06:20:26 +08:00
|
|
|
<span data-reactid=".s.0.0.1"> </span>
|
|
|
|
<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()}>
|
|
|
|
<Menu items={ Object.keys(SendLaterOptions) }
|
|
|
|
itemKey={ (item)=> item }
|
|
|
|
itemContent={this.renderMenuOption}
|
|
|
|
footerComponents={footerComponents}
|
|
|
|
onSelect={this.onSelectMenuOption}
|
|
|
|
/>
|
2016-02-19 02:00:11 +08:00
|
|
|
</Popover>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export default SendLaterPopover
|