Commit 51e31a1b by asranov0003

refactor: cache devices

parent e1d03153
import React, { useEffect } from "react"; import React from "react";
import Router from "./routes/Router"; import Router from "./routes/Router";
import useTelegramExpand from "./hooks/useTelegramExpand"; import useTelegramExpand from "./hooks/useTelegramExpand";
import { TG_USER_ID } from "./constants/constants";
import { useAppDispatch } from "./stores/store";
import { fetchDevices } from "./stores/slices/deviceSlice";
const App: React.FC = () => { const App: React.FC = () => {
const dispatch = useAppDispatch();
useTelegramExpand(); useTelegramExpand();
useEffect(() => {
if (localStorage.getItem(`token-${TG_USER_ID}`)) {
dispatch(fetchDevices());
}
}, [dispatch]);
return <Router />; return <Router />;
}; };
......
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit"; 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 {
IAudioList,
interface IDeviceData { IDevice,
bat_level: string; IDeviceData,
location: { } from "../../types/device.types";
lat: string;
lon: string;
speed: string;
};
}
interface IAudioList {
path: string;
url: string;
file_exists: boolean;
file_ext: string;
file_size: string;
date: string;
}
interface IDeviceState { interface IDeviceState {
devices: IDevice[]; devices: IDevice[];
...@@ -28,7 +14,6 @@ interface IDeviceState { ...@@ -28,7 +14,6 @@ interface IDeviceState {
isAudioListLoading: boolean; isAudioListLoading: boolean;
loadingDevices: boolean; loadingDevices: boolean;
errorDevices: string; errorDevices: string;
isFetched: boolean;
isDeletingDevice: boolean; isDeletingDevice: boolean;
} }
...@@ -40,7 +25,6 @@ const initialState: IDeviceState = { ...@@ -40,7 +25,6 @@ const initialState: IDeviceState = {
isAudioListLoading: false, isAudioListLoading: false,
loadingDevices: false, loadingDevices: false,
errorDevices: "", errorDevices: "",
isFetched: false,
isDeletingDevice: false, isDeletingDevice: false,
}; };
...@@ -60,6 +44,13 @@ export const fetchDevices = createAsyncThunk( ...@@ -60,6 +44,13 @@ export const fetchDevices = createAsyncThunk(
return rejectWithValue("Unknown error occurred"); return rejectWithValue("Unknown error occurred");
} }
},
{
condition: (_, { getState }) => {
const state = getState() as { device: IDeviceState };
return state.device.devices.length === 0;
},
} }
); );
...@@ -161,18 +152,15 @@ const deviceSlice = createSlice({ ...@@ -161,18 +152,15 @@ const deviceSlice = createSlice({
builder builder
.addCase(fetchDevices.pending, (state) => { .addCase(fetchDevices.pending, (state) => {
state.loadingDevices = true; state.loadingDevices = true;
state.isFetched = false;
}) })
.addCase(fetchDevices.fulfilled, (state, action) => { .addCase(fetchDevices.fulfilled, (state, action) => {
state.loadingDevices = false; state.loadingDevices = false;
state.devices = action.payload; state.devices = action.payload;
state.selectedDevice = action.payload[0] ?? null; state.selectedDevice = action.payload[0] ?? null;
state.isFetched = true;
}) })
.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;
state.isFetched = false;
}) })
.addCase(fetchDeviceData.fulfilled, (state, action) => { .addCase(fetchDeviceData.fulfilled, (state, action) => {
...@@ -182,11 +170,8 @@ const deviceSlice = createSlice({ ...@@ -182,11 +170,8 @@ const deviceSlice = createSlice({
.addCase(deleteDevice.pending, (state) => { .addCase(deleteDevice.pending, (state) => {
state.isDeletingDevice = true; state.isDeletingDevice = true;
}) })
.addCase(deleteDevice.fulfilled, (state, action) => { .addCase(deleteDevice.fulfilled, (state) => {
state.isDeletingDevice = false; state.isDeletingDevice = false;
if (action.payload.success) {
state.isFetched = false;
}
}) })
.addCase(deleteDevice.rejected, (state, action) => { .addCase(deleteDevice.rejected, (state, action) => {
state.isDeletingDevice = false; state.isDeletingDevice = false;
......
...@@ -12,3 +12,21 @@ export interface IDevice { ...@@ -12,3 +12,21 @@ export interface IDevice {
}[]; }[];
}; };
} }
export interface IDeviceData {
bat_level: string;
location: {
lat: string;
lon: string;
speed: string;
};
}
export interface IAudioList {
path: string;
url: string;
file_exists: boolean;
file_ext: string;
file_size: string;
date: string;
}
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