Skip to content

Commit

Permalink
clean logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jdinh8124 committed Nov 16, 2023
1 parent b214352 commit 5d2e7e1
Showing 1 changed file with 7 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useMemo, useEffect } from "react";
import { useState, useMemo } from "react";
import {
format,
isAfter,
Expand All @@ -10,6 +10,7 @@ import {
sub,
getYear,
endOfDay,
startOfDay,
} from "date-fns";
import { Calendar as CalendarIcon } from "lucide-react";
import { DateRange } from "react-day-picker";
Expand Down Expand Up @@ -41,11 +42,6 @@ export function FilterableDateRange({ value, onChange, ...props }: Props) {
value?.lte ? format(new Date(value?.lte), "MM/dd/yyyy") : ""
);

useEffect(() => {
setToValue(value?.lte ? format(new Date(value?.lte), "MM/dd/yyyy") : "");
setFromValue(value?.gte ? format(new Date(value?.gte), "MM/dd/yyyy") : "");
}, [value]);

const handleClose = (updateOpen: boolean) => {
setOpen(updateOpen);
};
Expand Down Expand Up @@ -125,16 +121,16 @@ export function FilterableDateRange({ value, onChange, ...props }: Props) {
};

const setPresetRange = (range: string) => {
const today = new Date();
let startDate = new Date(today.setHours(0, 0, 0, 0));

const today = startOfDay(new Date());
let startDate = today;
if (range === "quarter") {
startDate = startOfQuarter(today);
} else if (range === "month") {
startDate = startOfMonth(today);
} else if (range === "week") {
startDate = sub(today, { days: 6 });
}

const rangeObject = getDateRange(startDate, endOfDay(today));
onChange(rangeObject);
setSelectedDate({ from: startDate, to: today });
Expand Down Expand Up @@ -231,6 +227,8 @@ export function FilterableDateRange({ value, onChange, ...props }: Props) {
onClick={() => {
setSelectedDate({ from: undefined, to: undefined });
onChange({ gte: undefined, lte: undefined });
setToValue("");
setFromValue("");
}}
>
Clear
Expand Down

0 comments on commit 5d2e7e1

Please sign in to comment.