Files
CPLv4.0/components/main/kabelueberwachung/kue705FO/Charts/LoopMeasurementChart/LoopChartActionBar.tsx
2025-02-21 09:34:03 +01:00

121 lines
3.9 KiB
TypeScript

// components/main/kabelueberwachung/kue705FO/Charts/LoopMeasurementChart/LoopChartActionBar.tsx
import React from "react";
import DateRangePicker from "../DateRangePicker";
import { useDispatch, useSelector } from "react-redux";
import { RootState } from "../../../../../../redux/store";
import {
setVonDatum,
setBisDatum,
} from "../../../../../../redux/slices/dateRangeKueChartSlice";
import { setChartData } from "../../../../../../redux/slices/chartDataSlice";
const LoopChartActionBar: React.FC = () => {
const dispatch = useDispatch();
// Datum aus Redux abrufen
const vonDatum = useSelector(
(state: RootState) => state.dateRangeKueChart.vonDatum
);
const bisDatum = useSelector(
(state: RootState) => state.dateRangeKueChart.bisDatum
);
// Zustand für das Dropdown-Menü zur Auswahl von DIA-Modus (Alle, Stunden, Tage)
const [selectedMode, setSelectedMode] = React.useState<
"DIA0" | "DIA1" | "DIA2"
>("DIA0");
// Zustand für das Dropdown-Menü zur Auswahl des Slot-Typs (Isolations- oder Schleifenwiderstand)
const [selectedSlotType, setSelectedSlotType] = React.useState<
"isolationswiderstand" | "schleifenwiderstand"
>("schleifenwiderstand");
// Slot-Werte
const isolationswiderstand = 3;
const schleifenwiderstand = 4;
/**
* Dynamische API-URL-Erstellung für Entwicklung und Produktion
* @param mode - DIA0, DIA1 oder DIA2
* @param type - Slot für die Abfrage (3 = Isolationswiderstand, 4 = Schleifenwiderstand)
*/
const getApiUrl = (mode: "DIA0" | "DIA1" | "DIA2", type: number) => {
return process.env.NODE_ENV === "development"
? `/CPLmockData/kuesChartData/${mode}_${type}.json`
: `/CPL?seite.ACP&${mode}=${vonDatum};${bisDatum};${type}`;
};
/**
* Funktion zum Laden der Messwerte
*/
const handleFetchData = async () => {
const type = selectedSlotType === "schleifenwiderstand" ? 4 : 3; // 4 für Schleifenwiderstand, 3 für Isolationswiderstand
try {
const apiUrl = getApiUrl(selectedMode, type);
console.log("Mock JSON laden von:", apiUrl);
const response = await fetch(apiUrl);
if (!response.ok) throw new Error(`Fehler: ${response.status}`);
const jsonData = await response.json();
console.log("Geladene Daten:", jsonData);
if (Array.isArray(jsonData)) {
dispatch(setChartData(jsonData));
} else {
console.error("Erwartetes Array, aber erhalten:", jsonData);
}
} catch (error) {
console.error("Fehler beim Laden der Mock-Daten:", error);
}
};
return (
<div className="flex justify-end items-center p-2 bg-gray-100 rounded-lg space-x-2">
{/* Datumsauswahl */}
<DateRangePicker
setVonDatum={(date) => dispatch(setVonDatum(date))}
setBisDatum={(date) => dispatch(setBisDatum(date))}
/>
{/* Dropdown für DIA-Modus */}
<select
value={selectedMode}
onChange={(e) =>
setSelectedMode(e.target.value as "DIA0" | "DIA1" | "DIA2")
}
className="px-3 py-1 bg-white border rounded text-sm"
>
<option value="DIA0">Alle Messwerte (DIA0)</option>
<option value="DIA1">Stündliche Werte (DIA1)</option>
<option value="DIA2">Tägliche Werte (DIA2)</option>
</select>
{/* Dropdown für Slot-Typ */}
<select
value={selectedSlotType}
onChange={(e) =>
setSelectedSlotType(
e.target.value as "isolationswiderstand" | "schleifenwiderstand"
)
}
className="px-3 py-1 bg-white border rounded text-sm"
>
<option value="schleifenwiderstand">Schleifenwiderstand</option>
<option value="isolationswiderstand">Isolationswiderstand</option>
</select>
{/* Daten abrufen */}
<button
onClick={handleFetchData}
className="px-3 py-1 bg-green-500 text-white rounded text-sm"
>
Daten Laden
</button>
</div>
);
};
export default LoopChartActionBar;