Fix: Always show vonDatum and bisDatum in fetch URL for analog inputs chart
- Ensure local date state is never empty by falling back to default date if Redux is empty - Prevent missing date values in fetch URL after multiple dropdown or button interactions - Improves reliability of
This commit is contained in:
@@ -70,15 +70,28 @@ export default function AnalogInputsChart() {
|
||||
(state: RootState) => state.dateRangePicker.bisDatum
|
||||
);
|
||||
|
||||
// Hilfsfunktion für Default-Datum
|
||||
const getDefaultDate = (type: "from" | "to") => {
|
||||
const today = new Date();
|
||||
if (type === "to") return today.toISOString().slice(0, 10);
|
||||
const fromDateObj = new Date(today);
|
||||
fromDateObj.setDate(today.getDate() - 30);
|
||||
return fromDateObj.toISOString().slice(0, 10);
|
||||
};
|
||||
|
||||
// ✅ Lokale States für Picker + Zeitraum
|
||||
const [localVonDatum, setLocalVonDatum] = React.useState(vonDatumRedux || "");
|
||||
const [localBisDatum, setLocalBisDatum] = React.useState(bisDatumRedux || "");
|
||||
const [localVonDatum, setLocalVonDatum] = React.useState(
|
||||
vonDatumRedux || getDefaultDate("from")
|
||||
);
|
||||
const [localBisDatum, setLocalBisDatum] = React.useState(
|
||||
bisDatumRedux || getDefaultDate("to")
|
||||
);
|
||||
const [localZeitraum, setLocalZeitraum] = React.useState(zeitraum);
|
||||
|
||||
// Synchronisiere lokale Werte mit Redux (z.B. nach AutoLoad Reset)
|
||||
useEffect(() => {
|
||||
setLocalVonDatum(vonDatumRedux || "");
|
||||
setLocalBisDatum(bisDatumRedux || "");
|
||||
setLocalVonDatum(vonDatumRedux || getDefaultDate("from"));
|
||||
setLocalBisDatum(bisDatumRedux || getDefaultDate("to"));
|
||||
setLocalZeitraum(zeitraum);
|
||||
}, [vonDatumRedux, bisDatumRedux, zeitraum]);
|
||||
|
||||
@@ -105,24 +118,41 @@ export default function AnalogInputsChart() {
|
||||
const handleFetchData = () => {
|
||||
if (!selectedAnalogInput?.id) return;
|
||||
|
||||
// Fallback auf Redux-Werte, falls lokale Werte leer sind
|
||||
const from = localVonDatum || vonDatumRedux || "";
|
||||
const to = localBisDatum || bisDatumRedux || "";
|
||||
|
||||
// Redux aktualisieren
|
||||
dispatch(setVonDatum(localVonDatum));
|
||||
dispatch(setBisDatum(localBisDatum));
|
||||
dispatch(setVonDatum(from));
|
||||
dispatch(setBisDatum(to));
|
||||
dispatch(setZeitraum(localZeitraum));
|
||||
|
||||
// Debug anzeigen
|
||||
console.log(
|
||||
"Fetch-URL:",
|
||||
`/api/cpl/getAnalogInputsHistory?eingang=${selectedAnalogInput.id}&zeitraum=${localZeitraum}&von=${localVonDatum}&bis=${localBisDatum}`
|
||||
);
|
||||
// Umgebung erkennen und URL generieren
|
||||
const isDev =
|
||||
window.location.hostname === "localhost" ||
|
||||
window.location.hostname === "127.0.0.1";
|
||||
let fetchUrl = "";
|
||||
if (isDev) {
|
||||
fetchUrl = `/api/cpl/getAnalogInputsHistory?eingang=${selectedAnalogInput.id}&zeitraum=${localZeitraum}&von=${from}&bis=${to}`;
|
||||
} else {
|
||||
// Produktion: CPL-Webserver direkt abfragen
|
||||
const [vonJahr, vonMonat, vonTag] = from.split("-");
|
||||
const [bisJahr, bisMonat, bisTag] = to.split("-");
|
||||
const aeEingang = 100 + (selectedAnalogInput.id - 1);
|
||||
let diaType = "DIA1";
|
||||
if (localZeitraum === "DIA0") diaType = "DIA0";
|
||||
if (localZeitraum === "DIA2") diaType = "DIA2";
|
||||
fetchUrl = `${window.location.origin}/CPL?seite.ACP&${diaType}=${vonJahr};${vonMonat};${vonTag};${bisJahr};${bisMonat};${bisTag};${aeEingang};1`;
|
||||
}
|
||||
console.log("Fetch-URL:", fetchUrl);
|
||||
|
||||
// Thunk-Fetch mit neuen Werten
|
||||
dispatch(
|
||||
getAnalogInputsHistoryThunk({
|
||||
eingang: selectedAnalogInput.id,
|
||||
zeitraum: localZeitraum,
|
||||
vonDatum: localVonDatum,
|
||||
bisDatum: localBisDatum,
|
||||
vonDatum: from,
|
||||
bisDatum: to,
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user