Compare commits

..

No commits in common. "475f7b564e96b829eb67f13628ad5644f7b5636b" and "c1a31e6b3e2377247d2f479a20636b2d5f046ca3" have entirely different histories.

View File

@ -2,40 +2,37 @@ import React, { useEffect, useRef } from "react";
const DateRangePicker = ({ const DateRangePicker = ({
onRangeChange, onRangeChange,
DateDifference = 7, DateDifference = 7,
endDateMode = "yesterday", endDateMode = "yesterday", // "today" or "yesterday"
}) => { }) => {
const inputRef = useRef(null); const inputRef = useRef(null);
useEffect(() => { useEffect(() => {
const endDate = new Date(); const endDate = new Date();
if (endDateMode === "yesterday") { if (endDateMode === "yesterday") {
endDate.setDate(endDate.getDate() - 1); endDate.setDate(endDate.getDate() - 1);
} }
endDate.setHours(0, 0, 0, 0); const startDate = new Date();
startDate.setDate(endDate.getDate() - DateDifference);
const startDate = new Date(endDate);
startDate.setDate(endDate.getDate() - (DateDifference - 1));
startDate.setHours(0, 0, 0, 0);
const fp = flatpickr(inputRef.current, { const fp = flatpickr(inputRef.current, {
mode: "range", mode: "range",
dateFormat: "Y-m-d", dateFormat: "Y-m-d",
altInput: true, altInput: true,
altFormat: "d-m-Y", altFormat: "d-m-Y",
defaultDate: [startDate, endDate], defaultDate: [startDate, endDate],
static: true, static: true,
clickOpens: true, clickOpens: true,
onChange: (selectedDates, dateStr) => { onChange: (selectedDates, dateStr) => {
const [startDateString, endDateString] = dateStr.split(" to "); const [startDate, endDate] = dateStr.split(" to ");
onRangeChange?.({ startDate: startDateString, endDate: endDateString }); onRangeChange?.({ startDate, endDate });
}, },
}); });
onRangeChange?.({ onRangeChange?.({
startDate: startDate.toLocaleDateString("en-CA"), startDate: startDate.toLocaleDateString("en-CA"),
endDate: endDate.toLocaleDateString("en-CA"), endDate: endDate.toLocaleDateString("en-CA"),
}); });
return () => { return () => {