Redux Slice erstllen für Chart Data
This commit is contained in:
@@ -1,17 +1,21 @@
|
|||||||
import React, { useState, useEffect } from "react";
|
import React, { useState } from "react";
|
||||||
import DateRangePicker from "./DateRangePicker";
|
import DateRangePicker from "./DateRangePicker";
|
||||||
|
import ChartModal from "./ChartModal"; // Importiere das Chart-Modal
|
||||||
|
import { useDispatch, useSelector } from "react-redux";
|
||||||
|
import { setChartData } from "../../../redux/slices/chartDataSlice";
|
||||||
|
import { RootState } from "../../../redux/store";
|
||||||
|
|
||||||
const LoopTDRChartActionBar: React.FC = () => {
|
const LoopTDRChartActionBar: React.FC = () => {
|
||||||
|
const dispatch = useDispatch();
|
||||||
const BASE_URL = "http://localhost:3002";
|
const BASE_URL = "http://localhost:3002";
|
||||||
|
|
||||||
const [vonDatum, setVonDatum] = useState<Date>(new Date());
|
const [vonDatum, setVonDatum] = useState<Date>(new Date());
|
||||||
const [bisDatum, setBisDatum] = useState<Date>(new Date());
|
const [bisDatum, setBisDatum] = useState<Date>(new Date());
|
||||||
const [filteredData, setFilteredData] = useState<any[]>([]);
|
|
||||||
const [errorMessage, setErrorMessage] = useState<string | null>(null);
|
const [errorMessage, setErrorMessage] = useState<string | null>(null);
|
||||||
|
const [showChartModal, setShowChartModal] = useState(false);
|
||||||
|
|
||||||
useEffect(() => {
|
// Redux-Daten holen (damit sich das ChartModal darauf bezieht)
|
||||||
handleAktualisieren();
|
const chartData = useSelector((state: RootState) => state.chartData.data);
|
||||||
}, []);
|
|
||||||
|
|
||||||
const fetchAllData = async () => {
|
const fetchAllData = async () => {
|
||||||
let index = 0;
|
let index = 0;
|
||||||
@@ -34,24 +38,23 @@ const LoopTDRChartActionBar: React.FC = () => {
|
|||||||
|
|
||||||
const jsonData = await response.json();
|
const jsonData = await response.json();
|
||||||
allData.push(jsonData);
|
allData.push(jsonData);
|
||||||
index++; // Nächste URL abrufen
|
index++;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`❌ Fehler bei ${url}:`, error);
|
console.error(`❌ Fehler bei ${url}:`, error);
|
||||||
break; // Falls ein Fehler auftritt, Abbruch der Schleife
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
console.log("📥 Alle abgerufenen JSON-Daten:", allData);
|
||||||
|
dispatch(setChartData(allData)); // 🔥 Daten in Redux speichern
|
||||||
return allData;
|
return allData;
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleAktualisieren = async () => {
|
const handleAktualisieren = async () => {
|
||||||
try {
|
try {
|
||||||
setErrorMessage(null);
|
setErrorMessage(null);
|
||||||
setFilteredData([]);
|
|
||||||
|
|
||||||
console.log("📡 API Haupt-URL:", BASE_URL);
|
console.log("📡 API Haupt-URL:", BASE_URL);
|
||||||
|
|
||||||
// **Alle verfügbaren JSON-Objekte abrufen**
|
|
||||||
const allData = await fetchAllData();
|
const allData = await fetchAllData();
|
||||||
|
|
||||||
if (allData.length === 0) {
|
if (allData.length === 0) {
|
||||||
@@ -62,26 +65,6 @@ const LoopTDRChartActionBar: React.FC = () => {
|
|||||||
|
|
||||||
console.log("📥 Alle abgerufenen JSON-Daten:");
|
console.log("📥 Alle abgerufenen JSON-Daten:");
|
||||||
console.table(allData);
|
console.table(allData);
|
||||||
|
|
||||||
// **Daten filtern nach Datum**
|
|
||||||
const vonTimestamp = new Date(vonDatum).setHours(0, 0, 0, 0);
|
|
||||||
const bisTimestamp = new Date(bisDatum).setHours(23, 59, 59, 999);
|
|
||||||
|
|
||||||
const filtered = allData.filter((item: any) => {
|
|
||||||
const itemDateParts = item.t.split(" ")[0].split("-");
|
|
||||||
const itemTimestamp = new Date(
|
|
||||||
Number(itemDateParts[2]), // Jahr
|
|
||||||
Number(itemDateParts[1]) - 1, // Monat (JS zählt ab 0)
|
|
||||||
Number(itemDateParts[0]) // Tag
|
|
||||||
).setHours(0, 0, 0, 0);
|
|
||||||
|
|
||||||
return itemTimestamp >= vonTimestamp && itemTimestamp <= bisTimestamp;
|
|
||||||
});
|
|
||||||
|
|
||||||
console.log("🔍 Gefilterte Daten:");
|
|
||||||
console.table(filtered);
|
|
||||||
|
|
||||||
setFilteredData(filtered);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("❌ Fehler beim Abrufen der Daten:", error);
|
console.error("❌ Fehler beim Abrufen der Daten:", error);
|
||||||
setErrorMessage("❌ Fehler beim Laden der Daten.");
|
setErrorMessage("❌ Fehler beim Laden der Daten.");
|
||||||
@@ -98,6 +81,20 @@ const LoopTDRChartActionBar: React.FC = () => {
|
|||||||
>
|
>
|
||||||
Aktualisieren
|
Aktualisieren
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
|
<button
|
||||||
|
onClick={() => setShowChartModal(true)}
|
||||||
|
className="px-3 py-1 bg-blue-500 text-white rounded text-sm"
|
||||||
|
>
|
||||||
|
Diagramm anzeigen
|
||||||
|
</button>
|
||||||
|
|
||||||
|
{showChartModal && (
|
||||||
|
<ChartModal
|
||||||
|
isOpen={showChartModal}
|
||||||
|
onClose={() => setShowChartModal(false)}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -5,5 +5,5 @@
|
|||||||
2: Patch oder Hotfix (Bugfixes oder kleine Änderungen).
|
2: Patch oder Hotfix (Bugfixes oder kleine Änderungen).
|
||||||
|
|
||||||
*/
|
*/
|
||||||
const webVersion = "1.0.6.8";
|
const webVersion = "1.0.6.9";
|
||||||
export default webVersion;
|
export default webVersion;
|
||||||
|
|||||||
25
redux/slices/chartDataSlice.ts
Normal file
25
redux/slices/chartDataSlice.ts
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
|
||||||
|
|
||||||
|
interface ChartDataState {
|
||||||
|
data: any[];
|
||||||
|
}
|
||||||
|
|
||||||
|
const initialState: ChartDataState = {
|
||||||
|
data: [],
|
||||||
|
};
|
||||||
|
|
||||||
|
export const chartDataSlice = createSlice({
|
||||||
|
name: "chartData",
|
||||||
|
initialState,
|
||||||
|
reducers: {
|
||||||
|
setChartData: (state, action: PayloadAction<any[]>) => {
|
||||||
|
state.data = action.payload;
|
||||||
|
},
|
||||||
|
clearChartData: (state) => {
|
||||||
|
state.data = [];
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export const { setChartData, clearChartData } = chartDataSlice.actions;
|
||||||
|
export default chartDataSlice.reducer;
|
||||||
@@ -2,11 +2,13 @@
|
|||||||
import { configureStore } from "@reduxjs/toolkit";
|
import { configureStore } from "@reduxjs/toolkit";
|
||||||
import authReducer from "./slices/authSlice";
|
import authReducer from "./slices/authSlice";
|
||||||
import variablesReducer from "./slices/variablesSlice";
|
import variablesReducer from "./slices/variablesSlice";
|
||||||
|
import chartDataReducer from "./slices/chartDataSlice";
|
||||||
|
|
||||||
const store = configureStore({
|
const store = configureStore({
|
||||||
reducer: {
|
reducer: {
|
||||||
auth: authReducer,
|
auth: authReducer,
|
||||||
variables: variablesReducer,
|
variables: variablesReducer,
|
||||||
|
chartData: chartDataReducer,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user