refactor: fetchLocationDevices.js entfernt – Nutzung über Thunk + Service strukturiert neu aufgesetzt

This commit is contained in:
ISA
2025-05-21 11:02:40 +02:00
parent 0b7704935f
commit 71a6aeef1c
8 changed files with 33 additions and 80 deletions

View File

@@ -1,8 +0,0 @@
// /redux/api/fromDB/fetchLocationDevices.js
export const fetchLocationDevices = async () => {
const response = await fetch("/api/talas_v5_DB/locationDevice/locationDevices");
if (!response.ok) {
throw new Error("Geräteliste konnte nicht geladen werden");
}
return await response.json();
};

View File

@@ -1,29 +0,0 @@
// /redux/websocketMiddleware.js
const websocketMiddleware = () => {
let socket;
return ({ dispatch }) =>
(next) =>
(action) => {
if (action.type === "WS_CONNECT") {
socket = new WebSocket(action.payload.url);
socket.onmessage = (event) => {
const data = JSON.parse(event.data);
dispatch({ type: "WS_MESSAGE_RECEIVED", payload: data });
};
socket.onclose = () => {
dispatch({ type: "WS_DISCONNECTED" });
};
}
if (action.type === "WS_DISCONNECT" && socket) {
socket.close();
}
return next(action);
};
};
export default websocketMiddleware;

View File

@@ -1,10 +1,5 @@
// /redux/slices/database/locationDevicesFromDBSlice.js
import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";
import { fetchLocationDevices } from "../../api/fromDB/fetchLocationDevices";
export const fetchLocationDevicesFromDB = createAsyncThunk("locationDevicesFromDB/fetchLocationDevicesFromDB", async () => {
return fetchLocationDevices();
});
import { createSlice } from "@reduxjs/toolkit";
import { fetchLocationDevicesThunk } from "../../thunks/database/fetchLocationDevicesThunk";
const locationDevicesFromDBSlice = createSlice({
name: "locationDevicesFromDB",
@@ -16,14 +11,14 @@ const locationDevicesFromDBSlice = createSlice({
reducers: {},
extraReducers: (builder) => {
builder
.addCase(fetchLocationDevicesFromDB.pending, (state) => {
.addCase(fetchLocationDevicesThunk.pending, (state) => {
state.status = "loading";
})
.addCase(fetchLocationDevicesFromDB.fulfilled, (state, action) => {
.addCase(fetchLocationDevicesThunk.fulfilled, (state, action) => {
state.status = "succeeded";
state.devices = action.payload; // <-- Hier landen die Daten
state.devices = action.payload;
})
.addCase(fetchLocationDevicesFromDB.rejected, (state, action) => {
.addCase(fetchLocationDevicesThunk.rejected, (state, action) => {
state.status = "failed";
state.error = action.error.message;
});
@@ -31,3 +26,4 @@ const locationDevicesFromDBSlice = createSlice({
});
export default locationDevicesFromDBSlice.reducer;
export const selectLocationDevices = (state) => state.locationDevicesFromDB.devices;

View File

@@ -1,4 +1,4 @@
// /redux/thunks/fetchLocationDevicesThunk.js
// /redux/thunks/database/fetchLocationDevicesThunk.js
import { createAsyncThunk } from "@reduxjs/toolkit";
import { fetchLocationDevicesService } from "../../../services/database/fetchLocationDevicesService";