import { createSlice } from "@reduxjs/toolkit";
import { fetchLocationDevicesThunk } from "../../thunks/database/fetchLocationDevicesThunk";

const locationDevicesFromDBSlice = createSlice({
  name: "locationDevicesFromDB",
  initialState: {
    devices: [],
    status: "idle",
    error: null,
  },
  reducers: {},
  extraReducers: (builder) => {
    builder
      .addCase(fetchLocationDevicesThunk.pending, (state) => {
        state.status = "loading";
      })
      .addCase(fetchLocationDevicesThunk.fulfilled, (state, action) => {
        state.status = "succeeded";
        state.devices = action.payload;
      })
      .addCase(fetchLocationDevicesThunk.rejected, (state, action) => {
        state.status = "failed";
        state.error = action.error.message;
      });
  },
});

export default locationDevicesFromDBSlice.reducer;
export const selectLocationDevices = (state) => state.locationDevicesFromDB.devices;
