Skip to content

Commit 7b52a1f

Browse files
authored
Merge pull request #67 from abaali/main
Deleted duplicated code
2 parents 4907ade + 874941b commit 7b52a1f

File tree

2 files changed

+20
-24
lines changed

2 files changed

+20
-24
lines changed

webapp/javascript/components/CustomDatePicker.jsx

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,17 @@ import React, { useState, useEffect } from "react";
22
import moment from "moment";
33
import { useSelector } from "react-redux";
44
import DatePicker from "react-datepicker";
5-
import {
6-
readableRange,
7-
fromConverter,
8-
untilConverter,
9-
} from "../util/formatDate";
5+
import { readableRange, formatAsOBject } from "../util/formatDate";
106

117
function CustomDatePicker({ setRange, dispatch, setDateRange }) {
128
const from = useSelector((state) => state.from);
139
const until = useSelector((state) => state.until);
10+
const [warning, setWarning] = useState(false);
1411
const [selectedDate, setSelectedDate] = useState({
15-
from: fromConverter(from).from,
16-
until: untilConverter(until).until,
12+
from: formatAsOBject(from),
13+
until: formatAsOBject(until),
1714
});
18-
const [warning, setWarning] = useState(false);
15+
1916
const updateDateRange = () => {
2017
if (moment(selectedDate.from).isSameOrAfter(selectedDate.until)) {
2118
return setWarning(true);
@@ -32,11 +29,12 @@ function CustomDatePicker({ setRange, dispatch, setDateRange }) {
3229
useEffect(() => {
3330
setSelectedDate({
3431
...selectedDate,
35-
from: fromConverter(from).from,
36-
until: untilConverter(until).until,
32+
from: formatAsOBject(from),
33+
until: formatAsOBject(until),
3734
});
3835
setRange(readableRange(from, until));
3936
}, [from, until]);
37+
4038
return (
4139
<div>
4240
<h4>Custom Date Range</h4>

webapp/javascript/util/formatDate.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,22 @@ export function readableRange(from, until) {
4747
}
4848

4949
/**
50-
* taking state.from and state.until values
51-
* and converting it to a date object
50+
* formateAsOBject() returns a Date object
51+
* based on the passed-in parameter value
52+
* which is either a Number repsenting a date
53+
* or a default preset(example: "now-1h")
5254
* this is neccessary because the DatePicker component
5355
* from react-datepicker package has a property (selected)
5456
* that requires an input of type Date if we passed another
55-
* type the Component will give back an error and the app will crash
57+
* type the Component will throw an error and the app will crash
5658
*/
57-
export function fromConverter(from) {
58-
if (/^now-/.test(from)) {
59-
const { _from } = convertPresetsToDate(from);
60-
return { from: _from * 1000 };
59+
export function formatAsOBject(value) {
60+
if (/^now-/.test(value)) {
61+
const { _from } = convertPresetsToDate(value);
62+
return _from * 1000;
6163
}
62-
return { from: moment(from * 1000).toDate() };
63-
}
64-
65-
export function untilConverter(until) {
66-
if (until === "now") {
67-
return { until: moment().toDate() };
64+
if (value === "now") {
65+
return moment().toDate();
6866
}
69-
return { until: moment(until * 1000).toDate() };
67+
return moment(value * 1000).toDate();
7068
}

0 commit comments

Comments
 (0)