Skip to content

Commit 856e57f

Browse files
authored
Merge pull request #4061 from dlabrecq/daterange-fix
Date range fix
2 parents e500019 + 2b11ecf commit 856e57f

File tree

5 files changed

+43
-65
lines changed

5 files changed

+43
-65
lines changed

src/routes/components/groupBy/groupBy.tsx

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import { connect } from 'react-redux';
1515
import type { SelectWrapperOption } from 'routes/components/selectWrapper';
1616
import { SelectWrapper } from 'routes/components/selectWrapper';
1717
import type { PerspectiveType } from 'routes/explorer/explorerUtils';
18-
import { getDateRangeFromQuery } from 'routes/utils/dateRange';
1918
import { getTimeScopeValue } from 'routes/utils/timeScope';
2019
import type { FetchStatus } from 'store/common';
2120
import { createMapStateToProps } from 'store/common';
@@ -31,6 +30,7 @@ import { GroupByOrg } from './groupByOrg';
3130
import { GroupBySelect } from './groupBySelect';
3231

3332
interface GroupByOwnProps extends RouterComponentProps, WrappedComponentProps {
33+
endDate?: string;
3434
getIdKeyForGroupBy: (groupBy: Query['group_by']) => string;
3535
groupBy?: string;
3636
isDisabled?: boolean;
@@ -45,6 +45,7 @@ interface GroupByOwnProps extends RouterComponentProps, WrappedComponentProps {
4545
showCostCategories?: boolean;
4646
showOrgs?: boolean;
4747
showTags?: boolean;
48+
startDate?: string;
4849
tagPathsType: TagPathsType;
4950
}
5051

@@ -325,28 +326,25 @@ class GroupByBase extends React.Component<GroupByProps, GroupByState> {
325326
}
326327

327328
const mapStateToProps = createMapStateToProps<GroupByOwnProps, GroupByStateProps>(
328-
(state, { orgPathsType, router, resourcePathsType, tagPathsType }) => {
329+
(state, { endDate, orgPathsType, router, resourcePathsType, startDate, tagPathsType }) => {
329330
const queryFromRoute = parseQuery<Query>(router.location.search);
330331
const timeScopeValue = getTimeScopeValue(queryFromRoute);
331332

333+
// Use start and end dates with Cost Explorer
332334
// Default to current month filter for details pages
333-
let tagFilter: any = {
334-
filter: {
335-
resolution: 'monthly',
336-
time_scope_units: 'month',
337-
time_scope_value: timeScopeValue !== undefined ? timeScopeValue : -1,
338-
},
339-
};
340-
341-
// Replace with start and end dates for Cost Explorer
342-
if (queryFromRoute.dateRangeType) {
343-
const { end_date, start_date } = getDateRangeFromQuery(queryFromRoute);
344-
345-
tagFilter = {
346-
end_date,
347-
start_date,
348-
};
349-
}
335+
const tagFilter =
336+
startDate && endDate
337+
? {
338+
end_date: endDate,
339+
start_date: startDate,
340+
}
341+
: {
342+
filter: {
343+
resolution: 'monthly',
344+
time_scope_units: 'month',
345+
time_scope_value: timeScopeValue !== undefined ? timeScopeValue : -1,
346+
},
347+
};
350348

351349
// Note: Omitting key_only would help to share a single, cached request -- the toolbar requires key values
352350
// However, for better server-side performance, we chose to use key_only here.

src/routes/explorer/explorer.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,18 @@ class Explorer extends React.Component<ExplorerProps, ExplorerState> {
375375
}
376376
};
377377

378+
private handleOnDateRangeSelect = (dateRangeType: string) => {
379+
const { query, router } = this.props;
380+
381+
const newQuery = {
382+
...JSON.parse(JSON.stringify(query)),
383+
dateRangeType,
384+
start_date: undefined,
385+
end_date: undefined,
386+
};
387+
router.navigate(getRouteForQuery(newQuery, router.location, true), { replace: true });
388+
};
389+
378390
private handleOnExportModalClose = (isOpen: boolean) => {
379391
this.setState({ isExportModalOpen: isOpen });
380392
};
@@ -569,6 +581,7 @@ class Explorer extends React.Component<ExplorerProps, ExplorerState> {
569581
onCostDistributionSelect={() => handleOnCostDistributionSelect(query, router)}
570582
onCostTypeSelect={() => handleOnCostTypeSelect(query, router)}
571583
onCurrencySelect={() => handleOnCurrencySelect(query, router)}
584+
onDateRangeSelect={this.handleOnDateRangeSelect}
572585
onFilterAdded={filter => handleOnFilterAdded(query, router, filter)}
573586
onFilterRemoved={filter => handleOnFilterRemoved(query, router, filter)}
574587
onGroupBySelect={this.handleOnGroupBySelect}

src/routes/explorer/explorerDatePicker.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ interface ExplorerDatePickerOwnProps extends RouterComponentProps, WrappedCompon
1616
dateRangeType?: DateRangeType;
1717
endDate?: string;
1818
onSelect(startDate: Date, endDate: Date);
19-
startDate?: Date;
19+
startDate?: string;
2020
}
2121

2222
interface ExplorerDatePickerState {
@@ -39,11 +39,6 @@ class ExplorerDatePickerBase extends React.Component<ExplorerDatePickerProps, Ex
3939

4040
public componentDidMount() {
4141
const { dateRangeType, endDate, startDate } = this.props;
42-
// const queryFromRoute = parseQuery<Query>(router.location.search);
43-
//
44-
// // Query dates are undefined until a selection is made
45-
// const end_date = queryFromRoute.end_date;
46-
// const start_date = queryFromRoute.start_date;
4742

4843
if (this.startDateRef?.current) {
4944
this.startDateRef.current.setCalendarOpen(dateRangeType !== DateRangeType.custom);

src/routes/explorer/explorerFilter.tsx

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import {
4343

4444
interface ExplorerFilterOwnProps extends RouterComponentProps, WrappedComponentProps {
4545
dateRangeType: DateRangeType;
46-
endDate?: boolean;
46+
endDate?: string;
4747
groupBy: string;
4848
isCurrentMonthData?: boolean;
4949
isDisabled?: boolean;
@@ -53,7 +53,7 @@ interface ExplorerFilterOwnProps extends RouterComponentProps, WrappedComponentP
5353
perspective: PerspectiveType;
5454
pagination?: React.ReactNode;
5555
query?: Query;
56-
startDate?: boolean;
56+
startDate?: string;
5757
}
5858

5959
interface ExplorerFilterStateProps {
@@ -80,7 +80,6 @@ interface ExplorerFilterDispatchProps {
8080

8181
interface ExplorerFilterState {
8282
categoryOptions?: ToolbarChipGroup[];
83-
currentDateRange: DateRangeType;
8483
showDatePicker?: boolean;
8584
}
8685

@@ -92,24 +91,20 @@ const tagType = TagType.tag;
9291

9392
export class ExplorerFilterBase extends React.Component<ExplorerFilterProps, ExplorerFilterState> {
9493
protected defaultState: ExplorerFilterState = {
95-
currentDateRange: this.props.dateRangeType,
9694
showDatePicker: false,
9795
};
9896
public state: ExplorerFilterState = { ...this.defaultState };
9997

10098
public componentDidMount() {
101-
const { dateRangeType } = this.props;
102-
10399
this.updateReport();
104100
this.setState({
105-
currentDateRange: dateRangeType,
106101
categoryOptions: this.getCategoryOptions(),
107-
showDatePicker: dateRangeType === DateRangeType.custom,
102+
showDatePicker: this.props.dateRangeType === DateRangeType.custom,
108103
});
109104
}
110105

111106
public componentDidUpdate(prevProps: ExplorerFilterProps) {
112-
const { dateRangeType, orgReport, perspective, query, tagReport } = this.props;
107+
const { dateRangeType, orgReport, query, tagReport } = this.props;
113108

114109
if (query && !isEqual(query, prevProps.query)) {
115110
this.updateReport();
@@ -120,18 +115,10 @@ export class ExplorerFilterBase extends React.Component<ExplorerFilterProps, Exp
120115
prevProps.dateRangeType !== dateRangeType
121116
) {
122117
this.setState({
123-
currentDateRange: dateRangeType,
124118
categoryOptions: this.getCategoryOptions(),
125119
showDatePicker: dateRangeType === DateRangeType.custom,
126120
});
127121
}
128-
// Preserve filter -- see https://issues.redhat.com/browse/COST-1090
129-
if (prevProps.perspective !== perspective) {
130-
const currentDateRange = undefined;
131-
this.setState({ currentDateRange }, () => {
132-
this.updateDateRange(currentDateRange);
133-
});
134-
}
135122
}
136123

137124
private getCategoryOptions = (): ToolbarChipGroup[] => {
@@ -172,12 +159,11 @@ export class ExplorerFilterBase extends React.Component<ExplorerFilterProps, Exp
172159
};
173160

174161
private getDateRangeComponent = () => {
175-
const { isCurrentMonthData, isDisabled } = this.props;
176-
const { currentDateRange } = this.state;
162+
const { dateRangeType, isCurrentMonthData, isDisabled } = this.props;
177163

178164
return (
179165
<DateRange
180-
dateRangeType={currentDateRange}
166+
dateRangeType={dateRangeType}
181167
isCurrentMonthData={isCurrentMonthData}
182168
isDisabled={isDisabled}
183169
isExplorer
@@ -219,30 +205,13 @@ export class ExplorerFilterBase extends React.Component<ExplorerFilterProps, Exp
219205

220206
const currentDateRange = getDateRangeById(value);
221207
const showDatePicker = value === DateRangeType.custom;
222-
this.setState({ currentDateRange, showDatePicker }, () => {
223-
if (!showDatePicker) {
224-
this.updateDateRange(currentDateRange);
225-
226-
// Clear inline alert
227-
if (onDateRangeSelect) {
228-
onDateRangeSelect(currentDateRange);
229-
}
208+
this.setState({ showDatePicker }, () => {
209+
if (onDateRangeSelect && !showDatePicker) {
210+
onDateRangeSelect(currentDateRange);
230211
}
231212
});
232213
};
233214

234-
private updateDateRange = (value: string) => {
235-
const { query, router } = this.props;
236-
237-
const newQuery = {
238-
...JSON.parse(JSON.stringify(query)),
239-
dateRangeType: value,
240-
start_date: undefined,
241-
end_date: undefined,
242-
};
243-
router.navigate(getRouteForQuery(newQuery, router.location, true), { replace: true });
244-
};
245-
246215
private updateReport = () => {
247216
const {
248217
fetchOrg,

src/routes/explorer/explorerHeader.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ class ExplorerHeaderBase extends React.Component<ExplorerHeaderProps, ExplorerHe
175175

176176
const newQuery = {
177177
...JSON.parse(JSON.stringify(query)),
178+
dateRangeType: undefined, // Clear inline alert
178179
exclude: undefined,
179180
filter_by: undefined,
180181
group_by: { [getGroupByDefault(value)]: '*' },
@@ -315,6 +316,7 @@ class ExplorerHeaderBase extends React.Component<ExplorerHeaderProps, ExplorerHe
315316
<FlexItem>{this.getPerspective(noProviders)}</FlexItem>
316317
<FlexItem>
317318
<GroupBy
319+
endDate={endDate}
318320
getIdKeyForGroupBy={getIdKeyForGroupBy}
319321
groupBy={groupBy}
320322
isDisabled={noProviders}
@@ -328,6 +330,7 @@ class ExplorerHeaderBase extends React.Component<ExplorerHeaderProps, ExplorerHe
328330
}
329331
showOrgs={orgPathsType}
330332
showTags={tagPathsType}
333+
startDate={startDate}
331334
tagPathsType={tagPathsType}
332335
/>
333336
</FlexItem>

0 commit comments

Comments
 (0)