Skip to content

Commit bb31df9

Browse files
authored
Custom date range time (#101)
* checking in work before downloading vue-functional-date dependency * adding vue2-datepicker library with customisations for screen. Need to add disabled days * got custom date range working. fixed bug with relative range not filtering correctly. * removing dead code and making input look like rest of inputs on filter bar * removing old date range styles. adjusting styling * moving date range picker into folder to make it easier to test. added shortcuts and replaced relative range dropdown with it. * matching logic for shortcut in date range picker * fixing route logic on refresh to allow custom ranges if set. Will reset back to previous valid range otherwise max retention time. * Adding more shortcuts for finer control over time. * Adding more shortcuts for finer control over time. * general cleanup and supporting clear date range better * fixing clearable. adding placeholder for case of invalid range. parsing date range to display to allow custom ranges input in browser url. * changing mouse cursor. * moving constants out * lint. removed unused lib. fixing lint resolver. * adding getRange to helpers * moving isDayDisabled to helpers * removing clear handler as date input no longer is clearable * moving shortcuts to helpers * moving maxStartDate to helpers * moving timePanelLabel to helpers * moving rangeDisplayText to helpers * small refactoring * adding tests for getTimePanelLabel * adding skeleton for tests * Adding tests and mocking moment * testing isDayDisabled * fix for when maxDay is 1. added tests for getShortcuts * adding tests for getRange * adding tests for getRangeDisplayText * lint fixes * removing integration tests as the functionality is tested in unit test * 3.10.0-beta.0 * PR comments * PR comments * refactoring based on user feedback. needs more work * got most of the new changes working. Need to add filter by * hook up filter by. adding in validation to not allow dates before retention period * making code simpler * fixing breaking unit tests * fixing lint * adding unit tests * making sure limits refresh when time ticks over to next day. * lint * 3.10.0-beta.1 * minor style changes. Changing order so date picker is last. * 3.10.0
1 parent 164d36c commit bb31df9

File tree

17 files changed

+1159
-467
lines changed

17 files changed

+1159
-467
lines changed

.eslintrc.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,7 @@ module.exports = {
6262
],
6363
'space-before-blocks': 'error',
6464
},
65+
settings: {
66+
'import/resolver': 'webpack',
67+
},
6568
};

client/App.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ export default {
121121
</template>
122122

123123
<style src="vue-virtual-scroller/dist/vue-virtual-scroller.css"></style>
124+
<style src="vue2-datepicker/index.css"></style>
124125
<style lang="stylus">
125126
@import "https://d1a3f4spazzrp4.cloudfront.net/uber-fonts/4.0.0/superfine.css"
126127
@import "https://d1a3f4spazzrp4.cloudfront.net/uber-icons/3.14.0/uber-icons.css"
@@ -224,7 +225,7 @@ main
224225
margin-bottom layout-spacing-small
225226
> header
226227
display flex
227-
align-items center
228+
align-items: start;
228229
flex 0 0 auto
229230
> *
230231
margin inline-spacing-small

client/components/date-range-picker.vue

Lines changed: 0 additions & 224 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export const ALLOWED_PERIOD_TYPES = [
2+
'second',
3+
'seconds',
4+
'minute',
5+
'minutes',
6+
'hour',
7+
'hours',
8+
'day',
9+
'days',
10+
'month',
11+
'months',
12+
];
13+
14+
export const DATETIME_FORMAT = 'YYYY-MM-DD HH:mm:ss';
15+
16+
export const RANGE_OPTIONS = [
17+
{ text: 'Last 10 minutes', value: 'last-10-minutes', daysAgo: 0.007 },
18+
{ text: 'Last 60 minutes', value: 'last-60-minutes', daysAgo: 0.041 },
19+
{ text: 'Last 3 hours', value: 'last-3-hours', daysAgo: 0.125 },
20+
{ text: 'Last 24 hours', value: 'last-24-hours', daysAgo: 1 },
21+
{ text: 'Last 3 days', value: 'last-3-days', daysAgo: 3 },
22+
{ text: 'Last 7 days', value: 'last-7-days', daysAgo: 7 },
23+
{ text: 'Last 30 days', value: 'last-30-days', daysAgo: 30 },
24+
{ text: 'Last 3 months', value: 'last-3-months', daysAgo: 90 },
25+
];
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import moment from 'moment';
2+
import {
3+
ALLOWED_PERIOD_TYPES,
4+
DATETIME_FORMAT,
5+
RANGE_OPTIONS,
6+
} from './constants';
7+
8+
export const getDateString = date => moment(date).format(DATETIME_FORMAT);
9+
10+
export const getMaxEndDate = now => moment(now).endOf('day');
11+
12+
export const getMinStartDate = (maxDays, now) =>
13+
moment(now)
14+
.startOf('day')
15+
.subtract(maxDays, 'days');
16+
17+
export const getRange = dateRange => {
18+
if (!dateRange) {
19+
return [];
20+
}
21+
22+
if (typeof dateRange !== 'string') {
23+
return [dateRange.startTime.toDate(), dateRange.endTime.toDate()];
24+
}
25+
26+
const [, count, unit] = dateRange.split('-');
27+
28+
const startTime = moment()
29+
.subtract(count, unit)
30+
.startOf(unit)
31+
.toDate();
32+
33+
const endTime = moment()
34+
.endOf(unit)
35+
.toDate();
36+
37+
return [startTime, endTime];
38+
};
39+
40+
export const getRangeDisplayText = dateRange => {
41+
if (!dateRange) {
42+
return '';
43+
}
44+
45+
if (typeof dateRange !== 'string') {
46+
return `${dateRange.startTime.format(
47+
DATETIME_FORMAT
48+
)} - ${dateRange.endTime.format(DATETIME_FORMAT)}`;
49+
}
50+
51+
const [, count, unit] = dateRange.split('-');
52+
53+
const parsedCount = parseInt(count);
54+
55+
if (!parsedCount) {
56+
return '';
57+
}
58+
59+
if (!ALLOWED_PERIOD_TYPES.includes(unit)) {
60+
return '';
61+
}
62+
63+
return `Last ${parsedCount} ${unit}`;
64+
};
65+
66+
export const getShortcuts = (maxDays, onClickHandler) => {
67+
let options = RANGE_OPTIONS;
68+
69+
if (maxDays && maxDays < 90) {
70+
options = options.filter(o => o.daysAgo < maxDays);
71+
const periodType = maxDays === 1 ? 'day' : 'days';
72+
73+
const option = {
74+
daysAgo: maxDays,
75+
text: `Last ${maxDays} ${periodType}`,
76+
value: `last-${maxDays}-${periodType}`,
77+
};
78+
79+
options.push(option);
80+
options.sort((a, b) => a.daysAgo - b.daysAgo);
81+
}
82+
83+
options = options.map(option => ({
84+
...option,
85+
onClick: () => onClickHandler(option),
86+
}));
87+
88+
return options;
89+
};
90+
91+
export const getTimePanelLabel = showTimePanel =>
92+
showTimePanel ? 'Select date' : 'Select time';
93+
94+
export const isDateValid = (date, minStartDate, maxEndDate) =>
95+
date._isValid &&
96+
date.isSameOrAfter(minStartDate) &&
97+
date.isSameOrBefore(maxEndDate);
98+
99+
export const isDayDisabled = minStartDate => date => {
100+
const momentDate = moment(date);
101+
102+
if (minStartDate) {
103+
if (momentDate.isBefore(minStartDate)) {
104+
return true;
105+
}
106+
}
107+
108+
return momentDate.isAfter(moment().endOf('day'));
109+
};

0 commit comments

Comments
 (0)