Commit a1c2997a by asranov0003

fix: bug with fetching device list

parent 4aeea9b3
...@@ -32,17 +32,19 @@ const Header: React.FC = () => { ...@@ -32,17 +32,19 @@ const Header: React.FC = () => {
const { t } = useTranslation(); const { t } = useTranslation();
const dispatch = useAppDispatch(); const dispatch = useAppDispatch();
const { session } = useAppSelector((state: RootState) => state.account); const { session } = useAppSelector((state: RootState) => state.account);
const { devices, selectedDevice } = useAppSelector( const { devices, selectedDevice, isFetched } = useAppSelector(
(state: RootState) => state.device (state: RootState) => state.device
); );
useEffect(() => { useEffect(() => {
dispatch(accountSession()); dispatch(accountSession());
}, [dispatch]);
if (devices.length === 0) { useEffect(() => {
if (!isFetched) {
dispatch(fetchDevices()); dispatch(fetchDevices());
} }
}, [dispatch, devices]); }, [dispatch, isFetched]);
const handleOutsideClick = (e: MouseEvent) => { const handleOutsideClick = (e: MouseEvent) => {
if (sidebarRef.current && !sidebarRef.current.contains(e.target as Node)) { if (sidebarRef.current && !sidebarRef.current.contains(e.target as Node)) {
......
...@@ -17,6 +17,7 @@ interface IDeviceState { ...@@ -17,6 +17,7 @@ interface IDeviceState {
selectedDevice: IDevice | null; selectedDevice: IDevice | null;
loadingDevices: boolean; loadingDevices: boolean;
errorDevices: string; errorDevices: string;
isFetched: boolean
} }
const initialState: IDeviceState = { const initialState: IDeviceState = {
...@@ -25,6 +26,7 @@ const initialState: IDeviceState = { ...@@ -25,6 +26,7 @@ const initialState: IDeviceState = {
selectedDevice: null, selectedDevice: null,
loadingDevices: false, loadingDevices: false,
errorDevices: "", errorDevices: "",
isFetched: false
}; };
export const fetchDevices = createAsyncThunk( export const fetchDevices = createAsyncThunk(
...@@ -81,15 +83,18 @@ const deviceSlice = createSlice({ ...@@ -81,15 +83,18 @@ 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) => {
......
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