Skip to content
This repository was archived by the owner on Nov 2, 2024. It is now read-only.

Commit e9e7d0a

Browse files
carellamartinaMichalsus
authored andcommitted
Frontend - Replaced the time picker with a date picker (intelowlproject#2413)
* created TimePicker component + useTimePickerSore * test * fix
1 parent ecc6ccc commit e9e7d0a

File tree

5 files changed

+134
-27
lines changed

5 files changed

+134
-27
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import React from "react";
2+
import { Input, Label } from "reactstrap";
3+
import { useTimePickerStore } from "../../stores/useTimePickerStore";
4+
5+
export function TimePicker() {
6+
const [toDateValue, fromDateValue, updateToDate, updateFromDate] =
7+
useTimePickerStore((state) => [
8+
state.toDateValue,
9+
state.fromDateValue,
10+
state.updateToDate,
11+
state.updateFromDate,
12+
]);
13+
14+
return (
15+
<div className="d-flex float-end">
16+
<div className="d-flex align-items-center">
17+
<Label className="me-2 mb-0" for="DatePicker__gte">
18+
From:
19+
</Label>
20+
<Input
21+
id="DatePicker__gte"
22+
type="date"
23+
name="received_request_time__gte"
24+
autoComplete="off"
25+
value={fromDateValue.toISOString().split("T")[0]}
26+
onChange={(event) => {
27+
updateFromDate(new Date(event.target.value));
28+
}}
29+
/>
30+
</div>
31+
<div className="d-flex align-items-center">
32+
<Label className="mx-2 mb-0" for="DatePicker__lte">
33+
To:
34+
</Label>
35+
<Input
36+
id="DatePicker__lte"
37+
type="date"
38+
name="received_request_time__lte"
39+
autoComplete="off"
40+
value={toDateValue.toISOString().split("T")[0]}
41+
onChange={(event) => {
42+
updateToDate(new Date(event.target.value));
43+
}}
44+
/>
45+
</div>
46+
</div>
47+
);
48+
}

frontend/src/components/investigations/table/InvestigationsTable.jsx

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,14 @@ import React from "react";
33
import { Container, Row, Col, UncontrolledTooltip } from "reactstrap";
44
import { MdInfoOutline } from "react-icons/md";
55

6-
import {
7-
ElasticTimePicker,
8-
SyncButton,
9-
TableHintIcon,
10-
useDataTable,
11-
useTimePickerStore,
12-
} from "@certego/certego-ui";
6+
import { SyncButton, TableHintIcon, useDataTable } from "@certego/certego-ui";
137

148
import useTitle from "react-use/lib/useTitle";
159

1610
import { INVESTIGATION_BASE_URI } from "../../../constants/apiURLs";
1711
import { investigationTableColumns } from "./investigationTableColumns";
12+
import { TimePicker } from "../../common/TimePicker";
13+
import { useTimePickerStore } from "../../../stores/useTimePickerStore";
1814

1915
// constants
2016
const toPassTableProps = {
@@ -34,8 +30,11 @@ export default function InvestigationsTable() {
3430
// page title
3531
useTitle("IntelOwl | Investigation History", { restoreOnUnmount: true });
3632

37-
// consume zustand store
38-
const { range, fromTimeIsoStr, onTimeIntervalChange } = useTimePickerStore();
33+
// store
34+
const [toDateValue, fromDateValue] = useTimePickerStore((state) => [
35+
state.toDateValue,
36+
state.fromDateValue,
37+
]);
3938

4039
// state
4140
const [initialLoading, setInitialLoading] = React.useState(true);
@@ -45,7 +44,8 @@ export default function InvestigationsTable() {
4544
{
4645
url: INVESTIGATION_BASE_URI,
4746
params: {
48-
start_time__gte: fromTimeIsoStr,
47+
start_time__gte: fromDateValue,
48+
start_time__lte: toDateValue,
4949
},
5050
initialParams: {
5151
ordering: "-start_time",
@@ -88,12 +88,7 @@ export default function InvestigationsTable() {
8888
</div>
8989
</Col>
9090
<Col className="align-self-center">
91-
<ElasticTimePicker
92-
className="float-end"
93-
size="sm"
94-
defaultSelected={range}
95-
onChange={onTimeIntervalChange}
96-
/>
91+
<TimePicker />
9792
</Col>
9893
</Row>
9994
{/* Actions */}

frontend/src/components/jobs/table/JobsTable.jsx

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ import { MdInfoOutline } from "react-icons/md";
55

66
import {
77
Loader,
8-
ElasticTimePicker,
98
SyncButton,
109
TableHintIcon,
1110
useDataTable,
12-
useTimePickerStore,
1311
} from "@certego/certego-ui";
1412

1513
import useTitle from "react-use/lib/useTitle";
1614
import { jobTableColumns } from "./jobTableColumns";
15+
import { TimePicker } from "../../common/TimePicker";
1716

1817
import { JOB_BASE_URI } from "../../../constants/apiURLs";
1918
import { usePluginConfigurationStore } from "../../../stores/usePluginConfigurationStore";
19+
import { useTimePickerStore } from "../../../stores/useTimePickerStore";
2020

2121
// constants
2222
const toPassTableProps = {
@@ -40,8 +40,10 @@ export default function JobsTable() {
4040
// page title
4141
useTitle("IntelOwl | Jobs History", { restoreOnUnmount: true });
4242

43-
// consume zustand store
44-
const { range, fromTimeIsoStr, onTimeIntervalChange } = useTimePickerStore();
43+
const [toDateValue, fromDateValue] = useTimePickerStore((state) => [
44+
state.toDateValue,
45+
state.fromDateValue,
46+
]);
4547

4648
// state
4749
const [initialLoading, setInitialLoading] = React.useState(true);
@@ -51,7 +53,8 @@ export default function JobsTable() {
5153
{
5254
url: JOB_BASE_URI,
5355
params: {
54-
received_request_time__gte: fromTimeIsoStr,
56+
received_request_time__gte: fromDateValue,
57+
received_request_time__lte: toDateValue,
5558
},
5659
initialParams: {
5760
ordering: "-received_request_time",
@@ -98,12 +101,7 @@ export default function JobsTable() {
98101
</div>
99102
</Col>
100103
<Col className="align-self-center">
101-
<ElasticTimePicker
102-
className="float-end"
103-
size="sm"
104-
defaultSelected={range}
105-
onChange={onTimeIntervalChange}
106-
/>
104+
<TimePicker />
107105
</Col>
108106
</Row>
109107
{/* Actions */}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { create } from "zustand";
2+
3+
// default: 24h
4+
const defaultFromDate = new Date();
5+
defaultFromDate.setDate(defaultFromDate.getDate() - 1);
6+
7+
export const useTimePickerStore = create((set, _get) => ({
8+
toDateValue: new Date(),
9+
fromDateValue: defaultFromDate,
10+
updateFromDate: (date) => {
11+
set({ fromDateValue: date });
12+
},
13+
updateToDate: (date) => {
14+
set({ toDateValue: date });
15+
},
16+
}));
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import React from "react";
2+
import "@testing-library/jest-dom";
3+
import { render, screen, waitFor } from "@testing-library/react";
4+
import { BrowserRouter } from "react-router-dom";
5+
import userEvent from "@testing-library/user-event";
6+
import { TimePicker } from "../../../src/components/common/TimePicker";
7+
8+
describe("test TimePicker component", () => {
9+
test("time picker", async () => {
10+
const defaultFromDate = new Date();
11+
defaultFromDate.setDate(defaultFromDate.getDate() - 1);
12+
const toDateValue = new Date().toISOString().split("T")[0];
13+
const fromDateValue = defaultFromDate.toISOString().split("T")[0];
14+
15+
const user = userEvent.setup();
16+
const { container } = render(
17+
<BrowserRouter>
18+
<TimePicker />
19+
</BrowserRouter>,
20+
);
21+
22+
// labels
23+
const fromLabel = screen.getByText("From:");
24+
expect(fromLabel).toBeInTheDocument();
25+
const toLabel = screen.getByText("To:");
26+
expect(toLabel).toBeInTheDocument();
27+
28+
// time picker input
29+
const firstDateInput = container.querySelector("#DatePicker__gte");
30+
expect(firstDateInput).toBeInTheDocument();
31+
const secondDateInput = container.querySelector("#DatePicker__lte");
32+
expect(secondDateInput).toBeInTheDocument();
33+
expect(firstDateInput).toHaveValue(fromDateValue);
34+
expect(secondDateInput).toHaveValue(toDateValue);
35+
36+
// clear the input by highlighting all the current text and then replacing it with the new value
37+
await user.type(firstDateInput, "2024-02-05", {
38+
initialSelectionStart: 0,
39+
initialSelectionEnd: firstDateInput.value.length,
40+
});
41+
await user.type(secondDateInput, "2024-05-13", {
42+
initialSelectionStart: 0,
43+
initialSelectionEnd: secondDateInput.value.length,
44+
});
45+
await waitFor(() => {
46+
expect(firstDateInput).toHaveValue("2024-02-05");
47+
expect(secondDateInput).toHaveValue("2024-05-13");
48+
});
49+
});
50+
});

0 commit comments

Comments
 (0)