Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced moment with dayjs fixed issue #34 #47

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This configuration file was automatically generated by Gitpod.
Copy link
Contributor

@aquibbaig aquibbaig Feb 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't be a part of this PR

# Please adjust to your needs (see https://www.gitpod.io/docs/config-gitpod-file)
# and commit this file to your remote git repository to share the goodness with others.

tasks:
- init: yarn install && yarn run build
command: yarn run start
vscode:
extensions:
- dbaeumer.vscode-eslint

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
"dependencies": {
"@chakra-ui/icons": "^1.0.13",
"@chakra-ui/react": "^1.0.0",
"@date-io/date-fns": "^1.3.13",
"@emotion/react": "^11.0.0",
"@emotion/styled": "^11.0.0",
"@faker-js/faker": "^5.5.3",
"@material-ui/core": "^4.12.3",
"@material-ui/pickers": "^3.3.10",
"@testing-library/jest-dom": "^5.9.0",
"@testing-library/react": "^10.2.1",
"@testing-library/user-event": "^12.0.2",
Expand All @@ -18,11 +21,12 @@
"@types/react-virtualized-auto-sizer": "^1.0.0",
"@types/react-window": "^1.8.3",
"axios": "^0.21.1",
"date-fns": "^2.28.0",
"dayjs": "^1.10.7",
"framer-motion": "^4.0.0",
"moment": "^2.29.1",
"puppeteer": "^10.1.0",
"react": "^17.0.2",
"react-datetime": "^3.0.4",
"react-dayjs-picker": "^1.3.0",
"react-dom": "^17.0.2",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.3",
Expand Down
7 changes: 5 additions & 2 deletions src/components/GraphWrapper/graphWrapper.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ import { queryEntities } from "../../services/queryEntity";
import constants from "../../utils/constants";
import { useGlobalStore } from "../../store/global";

const { defaultStartTimestamp, defaultStepValue, defaultEndTimestamp } =
constants;
const {
defaultStartTimestamp,
defaultStepValue,
defaultEndTimestamp,
} = constants;

const TestComponent = () => {
const { changeRoute } = useGlobalStore();
Expand Down
7 changes: 5 additions & 2 deletions src/components/GraphWrapper/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ interface PageProps {

const GraphWrapper: React.FC<PageProps> = (props: PageProps) => {
const { selectedRoutePath } = props;
const { selectedStartTimestamp, selectedEndTimestamp, selectedStepValue } =
useTimeQuerierStore();
const {
selectedStartTimestamp,
selectedEndTimestamp,
selectedStepValue,
} = useTimeQuerierStore();

const { data, error, status } = useFetch<apiResponse<queryResponse>>(
queryEntities(
Expand Down
6 changes: 2 additions & 4 deletions src/components/ReusableGraph/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import moment from "moment";
import dayjs from "dayjs";
import {
VStack,
Alert,
Expand Down Expand Up @@ -63,9 +63,7 @@ const ReusableGraph: React.FC<reusableGraphProps> = ({
dataKey="timestamp"
domain={["dataMin", "dataMax"]}
tickMargin={10}
tickFormatter={(unixTime) =>
moment(unixTime).format("D MMM HH:mm:ss")
}
tickFormatter={(unixTime) => dayjs(unixTime).format("D MMM HH:mm:ss")}
interval={0}
type="number"
/>
Expand Down
7 changes: 2 additions & 5 deletions src/components/RouteSelector/routeSelector.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@ import { getRoutes } from "../../services/getRoutes";
import { truncate } from "../../utils/stringManipulation";

jest.mock("axios");
jest.mock(
"react-virtualized-auto-sizer",
() =>
({ children }: any) =>
children({ height: 600, width: 600 })
jest.mock("react-virtualized-auto-sizer", () => ({ children }: any) =>
children({ height: 600, width: 600 })
);

const mockedAxios = axios as jest.Mocked<typeof axios>;
Expand Down
75 changes: 48 additions & 27 deletions src/components/TimeQuerier/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,35 @@ import {
Alert,
AlertIcon,
} from "@chakra-ui/react";
import moment from "moment";
import Datetime from "react-datetime";
import constants from "../../utils/constants";
import { useTimeQuerierStore } from "../../store/timeQuerier";
import dayjs from "dayjs";
import DateFnsUtils from "@date-io/date-fns";
import {
MuiPickersUtilsProvider,
KeyboardDatePicker,
} from "@material-ui/pickers";

const TimeQuerier: React.FC = () => {
const styles = useStyleConfig("DateTime", {});
const { selectedStartTimestamp, selectedEndTimestamp, changeTimeQuerier } =
useTimeQuerierStore();
const {
selectedStartTimestamp,
selectedEndTimestamp,
changeTimeQuerier,
} = useTimeQuerierStore();
const { defaultStepValue, minStepValue, dateFormat, timeFormat } = constants;
const [startTime, setStartTime] = useState(moment(selectedStartTimestamp));
const [endTime, setEndTime] = useState(moment(selectedEndTimestamp));
const [startTime, setStartTime] = useState(dayjs(selectedStartTimestamp));
const [endTime, setEndTime] = useState(dayjs(selectedEndTimestamp));
Comment on lines +33 to +34
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say keep the start and end times in the redux store (and persist them too) so that it is maintained across refreshes (mostly people do not like dates getting reset)

const [stepTime, setStepTime] = useState(defaultStepValue);
const [error, setError] = useState<undefined | string>(undefined);

const handleStartChange = (date: moment.Moment | string) => {
const handleStartChange = (date: dayjs.Dayjs | string) => {
if (typeof date !== "string") {
setStartTime(date);
setError(undefined);
}
};
const handleEndChange = (date: moment.Moment | string) => {
const handleEndChange = (date: dayjs.Dayjs | string) => {
if (typeof date !== "string") {
setEndTime(date);
setError(undefined);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error is either null or Error

Expand All @@ -44,13 +51,13 @@ const TimeQuerier: React.FC = () => {
if (valueAsString !== "" && valueAsNumber >= minStepValue)
setStepTime(valueAsNumber);
};
const valid = (current: moment.Moment) => {
return current.isBefore(moment());
const valid = (current: dayjs.Dayjs) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use the utils/ folder

return current.isBefore(dayjs());
};
const handleFetchTimeSeriesData = () => {
if (
endTime.isBefore(startTime) ||
endTime.isAfter(moment()) ||
endTime.isAfter(dayjs()) ||
stepTime < minStepValue
)
setError("Kindy check your input");
Expand All @@ -74,14 +81,21 @@ const TimeQuerier: React.FC = () => {
>
<Text>Start Time</Text>
<Box sx={styles}>
<Datetime
value={startTime}
dateFormat={dateFormat}
timeFormat={timeFormat}
inputProps={{ className: "custom-datepicker start-time" }}
onChange={handleStartChange}
isValidDate={valid}
/>
<MuiPickersUtilsProvider utils={DateFnsUtils}>
<KeyboardDatePicker
disableToolbar
variant="inline"
format="MM/dd/yyyy"
margin="normal"
id="date-picker-inline"
value={startTime}
isValidDate={valid}
onChange={handleStartChange}
KeyboardButtonProps={{
"aria-label": "change date",
}}
/>
</MuiPickersUtilsProvider>
</Box>
</Box>
<Box
Expand All @@ -93,14 +107,21 @@ const TimeQuerier: React.FC = () => {
>
<Text>End Time</Text>
<Box sx={styles}>
<Datetime
value={endTime}
dateFormat={dateFormat}
timeFormat={timeFormat}
inputProps={{ className: "custom-datepicker end-time" }}
onChange={handleEndChange}
isValidDate={valid}
/>
<MuiPickersUtilsProvider utils={DateFnsUtils}>
<KeyboardDatePicker
disableToolbar
variant="inline"
format="MM/dd/yyyy"
margin="normal"
id="date-picker-inline"
value={endTime}
isValidDate={valid}
onChange={handleEndChange}
KeyboardButtonProps={{
"aria-label": "change date",
}}
/>
</MuiPickersUtilsProvider>
</Box>
</Box>
<Box
Expand Down
6 changes: 3 additions & 3 deletions src/components/TimeQuerier/timeQuerier.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from "react";
import moment from "moment";
import dayjs from "dayjs";
import { fireEvent } from "@testing-library/react";
import TimeQuerier from "./";
import { render } from "../../utils/customRender";
Expand All @@ -21,12 +21,12 @@ describe("TimeQuerier Component", () => {
expect(getByDisplayValue(defaultStepValue.toString(10))).toBeTruthy();
expect(
getByDisplayValue(
moment(defaultStartTimestamp).format(`${dateFormat} ${timeFormat}`)
dayjs(defaultStartTimestamp).format(`${dateFormat} ${timeFormat}`)
)
).toBeTruthy();
expect(
getByDisplayValue(
moment(defaultEndTimestamp).format(`${dateFormat} ${timeFormat}`)
dayjs(defaultEndTimestamp).format(`${dateFormat} ${timeFormat}`)
)
).toBeTruthy();
});
Expand Down
18 changes: 9 additions & 9 deletions src/e2e.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { routeResponse } from "./utils/types";
import puppeteer from "puppeteer";
import moment from "moment";
import dayjs from "dayjs";
import constants from "./utils/constants";
import { getActiveMachines } from "./services/getActiveMachines";
import { getRoutes } from "./services/getRoutes";
Expand Down Expand Up @@ -50,12 +50,12 @@ describe("E2E test for Dashboard", () => {
);
const graph = page.$('[data-testid="graph-info"]');
const startTime = await page.$(
`input[class="custom-datepicker start-time"][value="${moment(
`input[class="custom-datepicker start-time"][value="${dayjs(
defaultStartTimestamp
).format(`${dateFormat} ${timeFormat}"`)}]`
);
const endTime = await page.$(
`input[value="${moment(defaultEndTimestamp).format(
`input[value="${dayjs(defaultEndTimestamp).format(
`${dateFormat} ${timeFormat}"`
)}]`
);
Expand Down Expand Up @@ -165,7 +165,7 @@ describe("E2E test for Dashboard", () => {

await page.goto("http://localhost:5000");
const startTime = await page.$(".start-time");
const newStart = moment().subtract(1, "days").subtract(1, "hours");
const newStart = dayjs().subtract(1, "days").subtract(1, "hours");
if (startTime) {
await startTime.click();
}
Expand All @@ -176,7 +176,7 @@ describe("E2E test for Dashboard", () => {
);
await nextStart?.evaluate((b) => b.click());
const start = await page.$(
`input[class="custom-datepicker start-time"][value="${moment(
`input[class="custom-datepicker start-time"][value="${dayjs(
newStart
).format(`${dateFormat} ${timeFormat}`)}"]`
);
Expand All @@ -189,7 +189,7 @@ describe("E2E test for Dashboard", () => {

await page.goto("http://localhost:5000");
const endTime = await page.$(".end-time");
const newEnd = moment().subtract(2, "days");
const newEnd = dayjs().subtract(2, "days");
if (endTime) {
await endTime.click();
}
Expand All @@ -202,7 +202,7 @@ describe("E2E test for Dashboard", () => {
return b.click();
});
const end = await page.$(
`input[class="custom-datepicker end-time"][value="${moment(newEnd).format(
`input[class="custom-datepicker end-time"][value="${dayjs(newEnd).format(
`${dateFormat} ${timeFormat}"`
)}]`
);
Expand All @@ -226,7 +226,7 @@ describe("E2E test for Dashboard", () => {

await page.goto("http://localhost:5000");
const endTime = await page.$(".end-time");
const newEnd = moment().subtract(2, "days");
const newEnd = dayjs().subtract(2, "days");
if (endTime) {
await endTime.click();
}
Expand All @@ -252,7 +252,7 @@ describe("E2E test for Dashboard", () => {
await entities[0].click();
if (await page.$('[data-testid="graph"]')) {
const startTime = await page.$(".start-time");
const newStart = moment().subtract(1, "days").subtract(1, "hours");
const newStart = dayjs().subtract(1, "days").subtract(1, "hours");
if (startTime) {
await startTime.click();
}
Expand Down
4 changes: 2 additions & 2 deletions src/mock/mockGraph.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import faker from "@faker-js/faker";
import moment from "moment";
import dayjs from "dayjs";

/* Custom Generator
Generate a datapoint with the given keys
with random values using faker
*/
const dataGenerator = () => ({
timestamp: moment(faker.date.past()).unix(),
timestamp: dayjs(faker.date.past()).unix(),
value: faker.datatype.number,
});

Expand Down
7 changes: 3 additions & 4 deletions src/theme/components/DateTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ export const DateTime = {
},
".rdtPicker td.rdtDay:hover,.rdtPicker thead tr:first-of-type th:hover\
,.rdtPicker .rdtTimeToggle:hover,.rdtCounter .rdtBtn:hover\
,td.rdtMonth:hover, td.rdtYear:hover,.rdtSwitch:hover":
{
bg: mode("#eee", "darkPrimary")(props),
},
,td.rdtMonth:hover, td.rdtYear:hover,.rdtSwitch:hover": {
bg: mode("#eee", "darkPrimary")(props),
},
".rdtPicker td.rdtDisabled:hover": {
bg: "none",
},
Expand Down
1 change: 0 additions & 1 deletion src/theme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { Heading } from "./components/Heading";
import { extendTheme } from "@chakra-ui/react";
// Global style overrides
import styles from "./global";
import "react-datetime/css/react-datetime.css";
//Branding style ovverides
import branding from "./branding";

Expand Down
6 changes: 3 additions & 3 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import moment from "moment";
import dayjs from "dayjs";

export default {
defaultSelectedMachine: null,
backendBaseUrl: "http://localhost:9990/api/v1",
defaultStepValue: 15,
minStepValue: 1,
defaultStartTimestamp: moment().subtract(1, "h").toISOString(),
defaultEndTimestamp: moment().toISOString(),
defaultStartTimestamp: dayjs().subtract(1, "h").toISOString(),
defaultEndTimestamp: dayjs().toISOString(),
dateFormat: "DD/MM/yyyy",
timeFormat: "h:mm A",
graphDataLimit: 6000,
Expand Down
Loading