Commit 52fcdefa by asranov0003

feat: add device slice

parent 0409f918
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { sendRpcRequest } from "../../services/apiClient";
interface IDevice {
id: string;
name: string;
created: string;
lastOnline: string;
osOver: string;
deviceInfo: {
date_updated: string;
permissions: {
isGranted: boolean;
permission: string;
}[];
};
}
interface IDeviceState {
device: IDevice | null;
devices: IDevice[];
loadingDevice: boolean;
loadingDevices: boolean;
errorDevice: string;
errorDevices: string;
}
const initialState: IDeviceState = {
device: null,
devices: [],
loadingDevice: false,
loadingDevices: false,
errorDevice: "",
errorDevices: "",
};
export const fetchDevice = createAsyncThunk(
"device/fetchDevice",
async (deviceId: string, { rejectWithValue }) => {
try {
const response = await sendRpcRequest<IDevice>("devices.getdevicedata", {
deviceId,
});
return response;
} catch (error: unknown) {
if (typeof error === "object" && error !== null && "message" in error) {
return rejectWithValue(error.message);
}
return rejectWithValue("Unknown error occurred");
}
}
);
export const fetchDevices = createAsyncThunk(
"device/fetchDevices",
async (_, { rejectWithValue }) => {
try {
const response = await sendRpcRequest<{ list: IDevice[] }>(
"devices.getlist"
);
return response.list;
} catch (error: unknown) {
if (typeof error === "object" && error !== null && "message" in error) {
return rejectWithValue(error.message);
}
return rejectWithValue("Unknown error occurred");
}
}
);
const deviceSlice = createSlice({
name: "device",
initialState,
reducers: {},
extraReducers: (builder) => {
builder
.addCase(fetchDevice.pending, (state) => {
state.loadingDevice = true;
})
.addCase(fetchDevice.fulfilled, (state, action) => {
state.loadingDevice = false;
state.device = action.payload;
})
.addCase(fetchDevice.rejected, (state, action) => {
state.loadingDevice = false;
state.errorDevice = action.payload as string;
})
.addCase(fetchDevices.pending, (state) => {
state.loadingDevices = true;
})
.addCase(fetchDevices.fulfilled, (state, action) => {
state.loadingDevices = false;
state.devices = action.payload;
})
.addCase(fetchDevices.rejected, (state, action) => {
state.loadingDevices = false;
state.errorDevices = action.payload as string;
});
},
});
export default deviceSlice.reducer;
......@@ -2,6 +2,7 @@ import { configureStore } from "@reduxjs/toolkit";
import authSlice from "./slices/authSlice";
import accountSlice from "./slices/accountSlice";
import notificationSlice from "./slices/notificationSlice";
import deviceSlice from "./slices/deviceSlice";
import {
useDispatch,
useSelector,
......@@ -13,6 +14,7 @@ export const store = configureStore({
auth: authSlice,
account: accountSlice,
notification: notificationSlice,
device: deviceSlice,
},
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment