Skip to content

Commit

Permalink
Fix: Refresh schedule durations not pluralized sufficiently
Browse files Browse the repository at this point in the history
  • Loading branch information
ranbena committed Jan 10, 2019
1 parent d311527 commit f2eef79
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 36 deletions.
6 changes: 3 additions & 3 deletions client/app/components/queries/ScheduleDialog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Select from 'antd/lib/select';
import Radio from 'antd/lib/radio';
import { capitalize, clone, isEqual } from 'lodash';
import moment from 'moment';
import { secondsToInterval, IntervalEnum, localizeTime } from '@/filters';
import { secondsToInterval, durationHumanize, pluralize, IntervalEnum, localizeTime } from '@/filters';

import './ScheduleDialog.css';

Expand Down Expand Up @@ -182,9 +182,9 @@ class ScheduleDialog extends React.Component {
<Select value={seconds} onChange={this.setInterval} {...selectProps}>
<Option value={null} key="never">Never</Option>
{Object.keys(this.intervals).map(int => (
<OptGroup label={capitalize(int)} key={int}>
<OptGroup label={capitalize(pluralize(int))} key={int}>
{this.intervals[int].map(([cnt, secs]) => (
<Option value={secs} key={cnt}>{cnt} {int}</Option>
<Option value={secs} key={cnt}>{durationHumanize(secs)}</Option>
))}
</OptGroup>
))}
Expand Down
8 changes: 4 additions & 4 deletions client/app/components/queries/SchedulePhrase.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { react2angular } from 'react2angular';
import React from 'react';
import PropTypes from 'prop-types';
import Tooltip from 'antd/lib/tooltip';
import { localizeTime, secondsToInterval } from '@/filters';
import { localizeTime, durationHumanize } from '@/filters';

import './ScheduleDialog.css';

Expand All @@ -23,9 +23,9 @@ class SchedulePhrase extends React.Component {
if (!seconds) {
return ['Never'];
}
const { count, interval } = secondsToInterval(seconds);
const short = `Every ${count} ${interval}`;
let full = `Refreshes every ${count} ${interval}`;
const humanized = durationHumanize(seconds);
const short = `Every ${humanized}`;
let full = `Refreshes every ${humanized}`;

const { time, day_of_week: dayOfWeek } = this.props.schedule;
if (time) {
Expand Down
52 changes: 23 additions & 29 deletions client/app/filters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { capitalize as _capitalize, isEmpty } from 'lodash';

export const IntervalEnum = {
NEVER: 'Never',
MINUTES: 'minute(s)',
HOURS: 'hour(s)',
DAYS: 'day(s)',
WEEKS: 'week(s)',
SECONDS: 'second',
MINUTES: 'minute',
HOURS: 'hour',
DAYS: 'day',
WEEKS: 'week',
};

export function localizeTime(time) {
Expand All @@ -19,12 +20,16 @@ export function localizeTime(time) {
.format('HH:mm');
}

export function secondsToInterval(seconds) {
if (!seconds) {
export function secondsToInterval(count) {
if (!count) {
return { interval: IntervalEnum.NEVER };
}
let interval = IntervalEnum.MINUTES;
let count = seconds / 60;

let interval = IntervalEnum.SECONDS;
if (count >= 60) {
count /= 60;
interval = IntervalEnum.MINUTES;
}
if (count >= 60) {
count /= 60;
interval = IntervalEnum.HOURS;
Expand Down Expand Up @@ -61,29 +66,18 @@ export function intervalToSeconds(count, interval) {
return intervalInSeconds * count;
}

export function pluralize(text, count) {
const should = count !== 1;
return text + (should ? 's' : '');
}

export function durationHumanize(duration) {
let humanized = '';

if (duration === undefined || duration === null) {
humanized = '-';
} else if (duration < 60) {
const seconds = Math.round(duration);
humanized = `${seconds} seconds`;
} else if (duration > 3600 * 24) {
const days = Math.round(parseFloat(duration) / 60.0 / 60.0 / 24.0);
humanized = `${days} days`;
} else if (duration === 3600) {
humanized = '1 hour';
} else if (duration >= 3600) {
const hours = Math.round(parseFloat(duration) / 60.0 / 60.0);
humanized = `${hours} hours`;
} else if (duration === 60) {
humanized = '1 minute';
} else {
const minutes = Math.round(parseFloat(duration) / 60.0);
humanized = `${minutes} minutes`;
if (!duration) {
return '-';
}
return humanized;
const { interval, count } = secondsToInterval(duration);
const rounded = Math.round(count);
return `${rounded} ${pluralize(interval, rounded)}`;
}

export function toHuman(text) {
Expand Down

0 comments on commit f2eef79

Please sign in to comment.