fix(snooze/send-later): Change chrono config to always prefer dates in future

This commit is contained in:
Ben Gotow 2016-02-24 13:16:06 -08:00
parent d04ee7ef3a
commit 9d52cb1124
3 changed files with 53 additions and 8 deletions

View file

@ -60,7 +60,7 @@ class SendLaterPopover extends Component {
};
onSelectCustomOption = (value)=> {
const date = DateUtils.fromString(value);
const date = DateUtils.futureDateFromString(value);
if (date) {
this.onSelectDate(date);
} else {
@ -83,7 +83,7 @@ class SendLaterPopover extends Component {
renderCustomTimeSection() {
const onChange = (event)=> {
this.setState({inputDate: DateUtils.fromString(event.target.value)});
this.setState({inputDate: DateUtils.futureDateFromString(event.target.value)});
}
const onKeyDown = (event)=> {
@ -130,7 +130,7 @@ class SendLaterPopover extends Component {
if (scheduledDate === 'saving') {
return (
<button className={className} title="Send later...">
<button className={className} title="Saving send date...">
<RetinaImg
name="inline-loading-spinner.gif"
mode={RetinaImg.Mode.ContentDark}
@ -142,13 +142,13 @@ class SendLaterPopover extends Component {
let dateInterpretation = false;
if (scheduledDate) {
className += ' btn-enabled';
const momentDate = DateUtils.fromString(scheduledDate);
const momentDate = DateUtils.futureDateFromString(scheduledDate);
if (momentDate) {
dateInterpretation = <span className="at">Sending in {momentDate.fromNow(true)}</span>;
}
}
return (
<button className={className}>
<button className={className} title="Send later...">
<RetinaImg name="icon-composer-sendlater.png" mode={RetinaImg.Mode.ContentIsMask}/>
{dateInterpretation}
<span>&nbsp;</span>

View file

@ -99,7 +99,7 @@ class SnoozePopoverBody extends Component {
};
updateInputDateValue = _.debounce((dateValue)=> {
const inputDate = DateUtils.fromString(dateValue)
const inputDate = DateUtils.futureDateFromString(dateValue)
this.setState({inputDate})
}, 250);

View file

@ -1,6 +1,51 @@
/** @babel */
import moment from 'moment'
import chrono from 'chrono-node'
import _ from 'underscore'
function isPastDate({year, month, day}, ref) {
const refDay = ref.getDate();
const refMonth = ref.getMonth() + 1;
const refYear = ref.getFullYear();
if (refYear > year) {
return true;
}
if (refMonth > month) {
return true;
}
if (refDay > day) {
return true;
}
return false;
}
const EnforceFutureDate = new chrono.Refiner();
EnforceFutureDate.refine = (text, results)=> {
results.forEach((result)=> {
const current = _.extend({}, result.start.knownValues, result.start.impliedValues);
if (result.start.isCertain('weekday') && !result.start.isCertain('day')) {
if (isPastDate(current, result.ref)) {
result.start.imply('day', result.start.impliedValues.day + 7);
}
}
if (result.start.isCertain('day') && !result.start.isCertain('month')) {
if (isPastDate(current, result.ref)) {
result.start.imply('month', result.start.impliedValues.month + 1);
}
}
if (result.start.isCertain('month') && !result.start.isCertain('year')) {
if (isPastDate(current, result.ref)) {
result.start.imply('year', result.start.impliedValues.year + 1);
}
}
});
return results;
};
const chronoFuture = new chrono.Chrono(chrono.options.casualOption());
chronoFuture.refiners.push(EnforceFutureDate);
const Hours = {
Morning: 9,
@ -86,8 +131,8 @@ const DateUtils = {
* @param {string} dateLikeString - a string representing a date.
* @return {moment} - moment object representing date
*/
fromString(dateLikeString) {
const date = chrono.parseDate(dateLikeString)
futureDateFromString(dateLikeString) {
const date = chronoFuture.parseDate(dateLikeString)
if (!date) {
return null
}