Skip to content

Commit 5d5dac4

Browse files
authored
FIX: Date range filter fix (#1998)
* fix: change modal title * fix: show initial selected dates on activity log results page * fix: fixed failing tests * task: added comment regarding test
1 parent 4f29309 commit 5d5dac4

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

cypress/e2e/reports/activity-log/activity-log.spec.js

+22-8
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,19 @@ describe("Reports > Activity Log > Home", () => {
9090
);
9191
});
9292

93-
const from = moment()
93+
/**
94+
* Note: Date needs to be hard-coded here to make sure that when the datepicker opens,
95+
* the cypress test runner can easily pick the date from the datepicker modal
96+
* instead of having to determine how many times to click the next month arrow everytime
97+
* to get to the current date.
98+
*/
99+
const from = moment("2022-07-14")
94100
.hours(0)
95101
.minute(0)
96102
.second(0)
97103
.millisecond(0)
98104
.format("x");
99-
const to = moment()
105+
const to = moment("2022-07-14")
100106
.add(1, "day")
101107
.hours(0)
102108
.minute(0)
@@ -105,7 +111,7 @@ describe("Reports > Activity Log > Home", () => {
105111
.format("x");
106112

107113
// Set daterange filter
108-
cy.getBySelector("dateRange_default").should("exist").click();
114+
cy.getBySelector("dateRange_selected").should("exist").click();
109115
cy.getBySelector("dateRange_picker").should("exist");
110116
cy.get(`[data-timestamp=${from}]`).should("exist").click();
111117
cy.get(`[data-timestamp=${to}]`).should("exist").click();
@@ -118,8 +124,10 @@ describe("Reports > Activity Log > Home", () => {
118124
cy.getBySelector("resourceType_default").should("exist").click();
119125
cy.getBySelector("filter_value_content").should("exist").click();
120126

121-
const expectedFromDate = moment().format("YYYY-MM-DD");
122-
const expectedToDate = moment().add(1, "day").format("YYYY-MM-DD");
127+
const expectedFromDate = moment("2022-07-14").format("YYYY-MM-DD");
128+
const expectedToDate = moment("2022-07-14")
129+
.add(1, "day")
130+
.format("YYYY-MM-DD");
123131

124132
cy.location("search").should(
125133
"eq",
@@ -180,13 +188,19 @@ describe("Reports > Activity Log > Home", () => {
180188
});
181189
}).as("request");
182190

183-
const from = moment()
191+
/**
192+
* Note: Date needs to be hard-coded here to make sure that when the datepicker opens,
193+
* the cypress test runner can easily pick the date from the datepicker modal
194+
* instead of having to determine how many times to click the next month arrow everytime
195+
* to get to the current date.
196+
*/
197+
const from = moment("2022-07-14")
184198
.hours(0)
185199
.minute(0)
186200
.second(0)
187201
.millisecond(0)
188202
.format("x");
189-
const to = moment()
203+
const to = moment("2022-07-14")
190204
.add(1, "day")
191205
.hours(0)
192206
.minute(0)
@@ -195,7 +209,7 @@ describe("Reports > Activity Log > Home", () => {
195209
.format("x");
196210

197211
// Set daterange filter
198-
cy.getBySelector("dateRange_default").should("exist").click();
212+
cy.getBySelector("dateRange_selected").should("exist").click();
199213
cy.getBySelector("dateRange_picker").should("exist");
200214
cy.get(`[data-timestamp=${from}]`).should("exist").click();
201215
cy.get(`[data-timestamp=${to}]`).should("exist").click();

src/apps/reports/src/app/views/ActivityLog/components/Filters.tsx

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { useMemo, FC, useEffect, useState } from "react";
22
import { Box, FormControl, Skeleton } from "@mui/material";
33
import moment from "moment";
44
import { uniqBy, isEqual } from "lodash";
5+
import { useLocation } from "react-router-dom";
56
import { useParams } from "../../../../../../../shell/hooks/useParams";
67
import { accountsApi } from "../../../../../../../shell/services/accounts";
78
import { Audit } from "../../../../../../../shell/services/types";
@@ -99,6 +100,7 @@ export const Filters: FC<FiltersProps> = ({
99100
showSkeletons,
100101
}) => {
101102
const [params, setParams] = useParams();
103+
const location = useLocation();
102104
const { data: usersRoles } = accountsApi.useGetUsersRolesQuery();
103105
const [defaultDateRange, setDefaultDateRange] =
104106
useState<DateRangeFilterValue>({ from: null, to: null });
@@ -132,7 +134,10 @@ export const Filters: FC<FiltersProps> = ({
132134
to: moment(params.get("to")).format("YYYY-MM-DD"),
133135
};
134136

135-
if (isEqual(currentDateRange, defaultDateRange)) {
137+
if (
138+
location?.pathname.includes("schema") &&
139+
isEqual(currentDateRange, defaultDateRange)
140+
) {
136141
// Don't show the date on the date range filter if it's the same with the default date
137142
return { from: null, to: null };
138143
}
@@ -269,7 +274,7 @@ export const Filters: FC<FiltersProps> = ({
269274
value={dateRange}
270275
onChange={handleDateRangeFilterChanged}
271276
inactiveButtonText="Date"
272-
headerTitle="Date"
277+
headerTitle="Select Date Range"
273278
/>
274279
)}
275280
</Box>

src/shell/components/Filters/DateRangeFilter.tsx

+8-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ export const DateRangeFilter: FC<DateRangeFilterProps> = ({
3636
}) => {
3737
const [isModalOpen, setIsModalOpen] = useState(false);
3838
const [selectedDateRange, setSelectedDateRange] = useState<DateRange<any>>([
39-
value.from ? new Date(value.from) : null,
40-
value.to ? new Date(value.to) : null,
39+
null,
40+
null,
4141
]);
4242
const [dateRangeState, setDateRangeState] = useState("");
4343

@@ -57,6 +57,12 @@ export const DateRangeFilter: FC<DateRangeFilterProps> = ({
5757
}
5858
}, [dateRangeState]);
5959

60+
useEffect(() => {
61+
if (value.from && value.to) {
62+
setSelectedDateRange([new Date(value.from), new Date(value.to)]);
63+
}
64+
}, [value]);
65+
6066
const isFilterActive = Boolean(value?.from && value?.to);
6167
const buttonText = isFilterActive
6268
? `${moment(value.from).format("MMM D, YYYY")} to ${moment(value.to).format(

0 commit comments

Comments
 (0)