Commit 099f3349 by asranov0003

feat: fetch device data

parent c2f610c4
import React from "react"; import React, { useEffect } from "react";
import "./Home.css"; import "./Home.css";
import { Sheet, type SheetRef } from "react-modal-sheet"; import { Sheet, type SheetRef } from "react-modal-sheet";
import { useRef } from "react"; import { useRef } from "react";
...@@ -17,10 +17,24 @@ import { IoMdInformationCircleOutline } from "react-icons/io"; ...@@ -17,10 +17,24 @@ import { IoMdInformationCircleOutline } from "react-icons/io";
import { RiDeleteBinLine } from "react-icons/ri"; import { RiDeleteBinLine } from "react-icons/ri";
import { BsPersonStanding } from "react-icons/bs"; import { BsPersonStanding } from "react-icons/bs";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import {
useAppDispatch,
useAppSelector,
type RootState,
} from "../../stores/store";
import { fetchDeviceData } from "../../stores/slices/deviceSlice";
const Home: React.FC = () => { const Home: React.FC = () => {
const ref = useRef<SheetRef>(null); const ref = useRef<SheetRef>(null);
const { t } = useTranslation(); const { t } = useTranslation();
const { deviceData, selectedDevice } = useAppSelector(
(state: RootState) => state.device
);
const dispatch = useAppDispatch();
useEffect(() => {
dispatch(fetchDeviceData(selectedDevice?.id || ""));
}, [selectedDevice, dispatch]);
return ( return (
<> <>
...@@ -42,17 +56,19 @@ const Home: React.FC = () => { ...@@ -42,17 +56,19 @@ const Home: React.FC = () => {
<div className="home__sheet__status"> <div className="home__sheet__status">
<div> <div>
<IoBatteryHalfOutline className="home__sheet__status__icon" /> <IoBatteryHalfOutline className="home__sheet__status__icon" />
<p>100%</p> <p>{deviceData?.bat_level}%</p>
</div> </div>
<div> <div>
<BsPersonStanding className="home__sheet__status__icon" /> <BsPersonStanding className="home__sheet__status__icon" />
<p>0 {t("home.kmh")}</p> <p>
{deviceData?.location.speed} {t("home.kmh")}
</p>
</div> </div>
</div> </div>
<div className="home__sheet__device"> <div className="home__sheet__device">
<IoPhonePortraitOutline className="home__sheet__device__icon" /> <IoPhonePortraitOutline className="home__sheet__device__icon" />
<p>Tecno</p> <p>{selectedDevice?.name}</p>
</div> </div>
</div> </div>
......
...@@ -2,8 +2,18 @@ import { createAsyncThunk, createSlice } from "@reduxjs/toolkit"; ...@@ -2,8 +2,18 @@ import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { sendRpcRequest } from "../../services/apiClient"; import { sendRpcRequest } from "../../services/apiClient";
import type { IDevice } from "../../types/device.types"; import type { IDevice } from "../../types/device.types";
interface IDeviceData {
bat_level: string;
location: {
lat: string;
lon: string;
speed: string;
};
}
interface IDeviceState { interface IDeviceState {
devices: IDevice[]; devices: IDevice[];
deviceData: IDeviceData | null;
selectedDevice: IDevice | null; selectedDevice: IDevice | null;
loadingDevices: boolean; loadingDevices: boolean;
errorDevices: string; errorDevices: string;
...@@ -11,6 +21,7 @@ interface IDeviceState { ...@@ -11,6 +21,7 @@ interface IDeviceState {
const initialState: IDeviceState = { const initialState: IDeviceState = {
devices: [], devices: [],
deviceData: null,
selectedDevice: null, selectedDevice: null,
loadingDevices: false, loadingDevices: false,
errorDevices: "", errorDevices: "",
...@@ -35,6 +46,28 @@ export const fetchDevices = createAsyncThunk( ...@@ -35,6 +46,28 @@ export const fetchDevices = createAsyncThunk(
} }
); );
export const fetchDeviceData = createAsyncThunk(
"device/fetchDeviceData",
async (deviceId: string, { rejectWithValue }) => {
try {
const response = await sendRpcRequest<IDeviceData>(
"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");
}
}
);
const deviceSlice = createSlice({ const deviceSlice = createSlice({
name: "device", name: "device",
initialState, initialState,
...@@ -57,6 +90,10 @@ const deviceSlice = createSlice({ ...@@ -57,6 +90,10 @@ const deviceSlice = createSlice({
.addCase(fetchDevices.rejected, (state, action) => { .addCase(fetchDevices.rejected, (state, action) => {
state.loadingDevices = false; state.loadingDevices = false;
state.errorDevices = action.payload as string; state.errorDevices = action.payload as string;
})
.addCase(fetchDeviceData.fulfilled, (state, action) => {
state.deviceData = action.payload;
}); });
}, },
}); });
......
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