Skip to content

Commit

Permalink
fix: default value for time interval filters
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeRego committed Jul 6, 2022
1 parent 3f49727 commit bea123d
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion packages/common/src/index.ts
@@ -1,3 +1,4 @@
import moment from 'moment';
import { v4 as uuidv4 } from 'uuid';
import { Dashboard, DashboardBasicDetails } from './types/dashboard';
import { Explore, SummaryExplore } from './types/explore';
Expand All @@ -16,6 +17,7 @@ import {
isField,
isFilterableDimension,
MetricType,
TimeInterval,
} from './types/field';
import {
DashboardFilterRule,
Expand Down Expand Up @@ -274,8 +276,29 @@ export const getFilterRuleWithDefaultValue = <T extends FilterRule>(
completed: false,
} as DateFilterRule['settings'];
} else {
const defaultTimeIntervalValues: Record<string, Date> = {
[TimeInterval.DAY]: new Date(),
[TimeInterval.WEEK]: moment(value)
.utc(true)
.startOf('week')
.toDate(),
[TimeInterval.MONTH]: moment()
.utc(true)
.startOf('month')
.toDate(),
[TimeInterval.YEAR]: moment(value)
.utc(true)
.startOf('year')
.toDate(),
};
const defaultDate =
isDimension(field) &&
field.timeInterval &&
defaultTimeIntervalValues[field.timeInterval]
? defaultTimeIntervalValues[field.timeInterval]
: new Date();
filterRuleDefaults.values = [
value !== undefined ? value : new Date(),
value !== undefined ? value : defaultDate,
];
}
break;
Expand Down

0 comments on commit bea123d

Please sign in to comment.