refactor: fetchLocationDevices.js entfernt – Nutzung über Thunk + Service strukturiert neu aufgesetzt
This commit is contained in:
@@ -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();
|
||||
};
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// /redux/thunks/fetchLocationDevicesThunk.js
|
||||
// /redux/thunks/database/fetchLocationDevicesThunk.js
|
||||
import { createAsyncThunk } from "@reduxjs/toolkit";
|
||||
import { fetchLocationDevicesService } from "../../../services/database/fetchLocationDevicesService";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user