fix: Dynamische Initialisierung von "vonDatum" und "bisDatum" im Redux-Store
- vonDatum auf „heute minus 30 Tage“ gesetzt (statt festem Datum). - bisDatum auf heutiges Datum gesetzt. - Behebt Initialisierungsproblem im DateRangePicker.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import React from "react";
|
||||
import React, { useEffect } from "react";
|
||||
import DatePicker from "react-datepicker";
|
||||
import { useSelector, useDispatch } from "react-redux";
|
||||
import { RootState } from "../../../../../../redux/store";
|
||||
@@ -8,20 +8,17 @@ import {
|
||||
} from "../../../../../../redux/slices/kabelueberwachungChartSlice";
|
||||
import "react-datepicker/dist/react-datepicker.css";
|
||||
|
||||
// ✅ Props definieren
|
||||
interface DateRangePickerProps {
|
||||
setVonDatum: (date: Date) => void;
|
||||
setBisDatum: (date: Date) => void;
|
||||
minDate: string;
|
||||
maxDate: string;
|
||||
}
|
||||
|
||||
const DateRangePicker: React.FC<DateRangePickerProps> = ({
|
||||
setVonDatum,
|
||||
setBisDatum,
|
||||
minDate,
|
||||
maxDate,
|
||||
}) => {
|
||||
const dispatch = useDispatch();
|
||||
|
||||
const reduxVonDatum = useSelector(
|
||||
(state: RootState) => state.kabelueberwachungChart.vonDatum
|
||||
);
|
||||
@@ -29,20 +26,39 @@ const DateRangePicker: React.FC<DateRangePickerProps> = ({
|
||||
(state: RootState) => state.kabelueberwachungChart.bisDatum
|
||||
);
|
||||
|
||||
const today = new Date();
|
||||
const thirtyDaysAgo = new Date();
|
||||
thirtyDaysAgo.setDate(today.getDate() - 30);
|
||||
|
||||
// Redux speichert ISO ("YYYY-MM-DD") => Für DatePicker geeignet
|
||||
const parseISODate = (isoDate: string) => {
|
||||
const [year, month, day] = isoDate.split("-").map(Number);
|
||||
return new Date(year, month - 1, day);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (!reduxVonDatum)
|
||||
dispatch(setVonDatum(thirtyDaysAgo.toISOString().split("T")[0]));
|
||||
if (!reduxBisDatum)
|
||||
dispatch(setBisDatum(today.toISOString().split("T")[0]));
|
||||
}, [dispatch, reduxVonDatum, reduxBisDatum]);
|
||||
|
||||
return (
|
||||
<div className="flex space-x-4 items-center">
|
||||
<div className="flex items-center space-x-2">
|
||||
<label className="block text-sm font-semibold">Von</label>
|
||||
<DatePicker
|
||||
selected={reduxVonDatum ? new Date(reduxVonDatum) : new Date()}
|
||||
selected={reduxVonDatum ? parseISODate(reduxVonDatum) : thirtyDaysAgo}
|
||||
onChange={(date) => {
|
||||
if (date) {
|
||||
setVonDatum(date);
|
||||
dispatch(setVonDatum(date.toISOString().split("T")[0]));
|
||||
}
|
||||
}}
|
||||
selectsStart
|
||||
startDate={reduxVonDatum ? new Date(reduxVonDatum) : new Date()}
|
||||
endDate={reduxBisDatum ? new Date(reduxBisDatum) : new Date()}
|
||||
startDate={
|
||||
reduxVonDatum ? parseISODate(reduxVonDatum) : thirtyDaysAgo
|
||||
}
|
||||
endDate={reduxBisDatum ? parseISODate(reduxBisDatum) : today}
|
||||
minDate={new Date(minDate)}
|
||||
maxDate={new Date(maxDate)}
|
||||
dateFormat="dd.MM.yyyy"
|
||||
@@ -53,15 +69,17 @@ const DateRangePicker: React.FC<DateRangePickerProps> = ({
|
||||
<div className="flex items-center space-x-2">
|
||||
<label className="block text-sm font-semibold">Bis</label>
|
||||
<DatePicker
|
||||
selected={reduxBisDatum ? new Date(reduxBisDatum) : new Date()}
|
||||
selected={reduxBisDatum ? parseISODate(reduxBisDatum) : today}
|
||||
onChange={(date) => {
|
||||
if (date) {
|
||||
setBisDatum(date);
|
||||
dispatch(setBisDatum(date.toISOString().split("T")[0]));
|
||||
}
|
||||
}}
|
||||
selectsEnd
|
||||
startDate={reduxVonDatum ? new Date(reduxVonDatum) : new Date()}
|
||||
endDate={reduxBisDatum ? new Date(reduxBisDatum) : new Date()}
|
||||
startDate={
|
||||
reduxVonDatum ? parseISODate(reduxVonDatum) : thirtyDaysAgo
|
||||
}
|
||||
endDate={reduxBisDatum ? parseISODate(reduxBisDatum) : today}
|
||||
minDate={new Date(minDate)}
|
||||
maxDate={new Date(maxDate)}
|
||||
dateFormat="dd.MM.yyyy"
|
||||
|
||||
Reference in New Issue
Block a user